Reputation: 255
I am trying to create a disassembler for ARM as I want to learn ARM assembly language. I have read from another StackOverflow thread that this is the best way to do it. So one thing I am having trouble is getting the decode of multiply correctly.
Here is an example of a disassembled object file.
00008054 <_start>: ==============ommitted irrelevant instructions ====
8064: e0010092 mul r1, r2, r0
From the reference manual (http://www.nyx.net/~troddis/ARM.html), a multiply instruction should have a value of "1001" in bits [4-7] inclusive. However, e0010092 in binary is "11100010000000000001000000000010" which have 0000 in bits 4-7.
Is there something obvious that I am missing? I am using CodeSourcery's ARM 2012.09-64 cross compiler...
Upvotes: 0
Views: 371
Reputation: 18227
Converting hex to binary can largely be done "in your head" if you remember:
8421|8421...
===========
1011|1001...
8 21|8 1...
The upper line are the powers of two which make up a hexadecimal digit - 8+4+2+1 == 0xf
. The lower line is some arbitrary binary; to convert that to hex, add the powers-of-two set in the 4-binary-digit unit (a "nibble"), third line. Every nibble is one hex digit. So do that for every nibble and you've got the hex-from-binary. In the case pf the above, 8+2+1 == 0xb
and 8+1==9
so you have 10111001 == 0xB9
(assuming you count bit zero as the rightmost one; if you consider bit zero the leftmost one, it'd be 0x9B
, welcome to the world of endianness ... but that's another question)
The reverse is also simple; for your number:
e |0 |0 |1 |0 |0 |9 |2 |
842 | | | 1| | |8 1| 2 |
1110|0000|0000|0001|0000|0000|1001|0010|
Again, the top line is the hex digits, the middle line the powers of two which summed up give the hex digit, and the bottom are the bit sequence.
I tend to use the calculator to convert decimal-to-hex and vice versa, but binary-to/from-hex, do in my head as per above.
If you really want to use a calculator, UN*X bc
is helpful for all sorts of numerical conversions:
$ echo "ibase=16;obase=2;B8F7D335" | bc
10111000111101111101001100110101
$ echo "ibase=16;B8F7D335" | bc
3103249205
$ echo "obase=16;ibase=2;10111000111101111101001100110101" | bc
B8F7D335
The default "base" is ten, so no need to explicitly specify input / output number base if you're converting to/from ten. Introductions to bc
are all over the web; like this one.
Upvotes: 0
Reputation: 61378
How'd you convert it to binary? Hex 9 is exactly binary 1001.
Upvotes: 2