turboscrew
Posts: 174
Joined: Sat Jan 18, 2014 1:50 pm
Location: Nokia (town), Finland

ARM assembly encodings?

Sat Apr 18, 2015 7:59 pm

What's the idea of different encodings, like T1, T2 and T3?
I need to figure out the instruction from the binary assembly code, so
how can I tell which encoding applies? Or where should I start the decoding?
I need to be able to decode both ARM and Thumb instructions.
What I really need is the address(es) the execution may go to - I'm trying to put together
a gdb stub that uses SW interrupt, and I need the addresses for single stepping,
but knowing how to make full disassembly wouldn't hurt either.

I tried writing it for immediate Single Data Transfer (= Load/store word and unsigned byte) for
only ARM A1 (affects program flow if load and Rd = PC, or both load and store if Rn is PC and indexing is used) and I was scared to think about what kind of job it is to do all instructions, when just LDR immediate has one ARM encoding (A1) but 4 Thumb encodings. Didn't even check the other instructions of the same group for different encodings.

Or am I taking up 'building Cologne Cathedral'? :)
( http://www.colognecathedral.net/Cologne ... Facts.html )
De-bugging is for sissies - real men do de-monstrations.

mrvn
Posts: 58
Joined: Wed Jan 09, 2013 6:50 pm

Re: ARM assembly encodings?

Sun Apr 19, 2015 11:53 am

Just use binutils objdump -d.

turboscrew
Posts: 174
Joined: Sat Jan 18, 2014 1:50 pm
Location: Nokia (town), Finland

Re: ARM assembly encodings?

Sun Apr 19, 2015 1:06 pm

Disassembler doesn't really help. My program (gdb stub) should be able to figure out possible branch address of any instruction including arithmetic instructions whose destination is PC and indexed instructions whose Rn is PC.
De-bugging is for sissies - real men do de-monstrations.

mrvn
Posts: 58
Joined: Wed Jan 09, 2013 6:50 pm

Re: ARM assembly encodings?

Sun Apr 19, 2015 3:19 pm

I'm not sure what you are doing but the gdb stubs that you put in your OS should be able to look at the threads mode to figure out if it is running thumb or not. And the gdb side should already know how do decode opcodes.

turboscrew
Posts: 174
Joined: Sat Jan 18, 2014 1:50 pm
Location: Nokia (town), Finland

Re: ARM assembly encodings?

Sun Apr 19, 2015 3:30 pm

Mare metal - no OS, and single stepping needs to be implemented in the stub. That means the next
addresses (two for conditional branch) needs to be found out. It's the gdb serial protocol I'm implementing.

The thumb/arm mode is clear (T-bit in cpsr), but what are those A1 or A2 (arm) and T1 - T4 (thumb) and do they change the way the instructions can be decoded (at least far enough to decide if it affects PC and if it does, how).

I'm using BKPT instead of debug-coprocessor and HW breakpoints. Maybe some day...
De-bugging is for sissies - real men do de-monstrations.

kriss
Posts: 66
Joined: Thu Apr 02, 2015 8:53 pm
Location: france for now ...

Re: ARM assembly encodings?

Sun Apr 19, 2015 3:52 pm

you put "debugging is for sissies"
and you want gdb-bare-metal
you are not consistent
less hard is to begin by a bare-metal-assembler ;

turboscrew
Posts: 174
Joined: Sat Jan 18, 2014 1:50 pm
Location: Nokia (town), Finland

Re: ARM assembly encodings?

Sun Apr 19, 2015 9:57 pm

Well, I'm trying to write a (kind of) debugger, not to use one :D
Maybe I use 'heavier artillery' to remove monsters from code. :D
Or maybe I'm just a sissie. :D
Also, gdb client handles most of the debugging. There is just something a stub must implement.
Stub is so much smaller and simpler than gdb server.
De-bugging is for sissies - real men do de-monstrations.

turboscrew
Posts: 174
Joined: Sat Jan 18, 2014 1:50 pm
Location: Nokia (town), Finland

Re: ARM assembly encodings?

Mon Apr 20, 2015 8:21 am

It looks like figuring out the ARM ISA on bit level is becoming by far the most tedious and time consuming task.
When (if?)I get it figured out, I hope I still remember there was a project it was done for.

I think I just have to go through the instructions in the ARMv7-A ARM one by one and manually list all instructions and the bit patterns of all encodings in a text file for easier manipulation and sort them out there. The HTML-pages are slow for that, and the copying works funny with PDFs.
De-bugging is for sissies - real men do de-monstrations.

kriss
Posts: 66
Joined: Thu Apr 02, 2015 8:53 pm
Location: france for now ...

Re: ARM assembly encodings?

Mon Apr 20, 2015 8:42 am

binutils source have all the opcodes (use gawk to order)

colinh
Posts: 95
Joined: Tue Dec 03, 2013 11:59 pm
Location: Munich

Re: ARM assembly encodings?

Mon Apr 20, 2015 10:25 am

I can't really figure out what/why you're trying to do either :-)

I use OpenOCD (with or without gdb) to debug my bare metal code. Is your stub supposed to be running on the target that you're trying to debug? Or are you trying to implement something like OpenOCD? (In which case looking at the open source hard & software might help).

Anyway, the various encodings, T1, T2, ... A1, A2, ... are explained in the ARM ARM. They're simply alternative ways of encoding the instruction depending on, for instance, the size of an immediate, or a specific register being used. So yes, you need to implement all of them.

You might find flex/bison (lex/yacc) useful.

turboscrew
Posts: 174
Joined: Sat Jan 18, 2014 1:50 pm
Location: Nokia (town), Finland

Re: ARM assembly encodings?

Mon Apr 20, 2015 10:42 am

@kriss: "binutils source have all the opcodes (use gawk to order)"
Which program? I looked into gdb clent and gdb-server sources, but I only found hard-to-follow algorithm for disassembly. At least hard enough so that reading the ARM ARM and making a text file is faster. The actual debugging seems to relay on Linux kernel (processes, threads, ptrace, ...).

@colinh: I don't have any jtag, st-link, j-link or such HW.
I'm trying to write the "board end" of the remote serial gdb protocol.
"They're simply alternative ways of encoding the instruction depending on, for instance, the size of an immediate, or a specific register being used."
So they don't overlap. Thanks!
De-bugging is for sissies - real men do de-monstrations.


turboscrew
Posts: 174
Joined: Sat Jan 18, 2014 1:50 pm
Location: Nokia (town), Finland

Re: ARM assembly encodings?

Mon Apr 20, 2015 6:26 pm

Hey, kriss, you are right! Thanks!
I also found the routines I'm after, in much more readable form than before in the file gdb/arm-tdep.c
That probably saves me a month of work!
De-bugging is for sissies - real men do de-monstrations.

kriss
Posts: 66
Joined: Thu Apr 02, 2015 8:53 pm
Location: france for now ...

Re: ARM assembly encodings?

Tue Apr 21, 2015 9:58 am

;)

Return to “Bare metal, Assembly language”