Uhli
Uhli

Reputation: 333

Understanding Arm Assembler Branch Offset Calculation

I do not understand the offset calcualted for the branch instructions (b and bl at addresses 0x00011004 and 0x00011010) related to the disassembled code listed below. I'm wondering that in the hex code listing the offsets seem to be 0x000001 and 0x000002. The Opcodes for the conditions b (EA) and bl (EB) were what I expected.

Thanks for every hint in advance

MyAssemblerFunc:
00011000  stmdb       sp!, {r0 - r3, lr} 
00011004  b           00011010 
00011008  mov         r0, r0 
0001100C  mov         r0, r0 
00011010  bl          |PrintHelloWorld ( 11020h )| 
00011014  ldmia       sp!, {r0 - r3, lr} 

Related Hex Code

0x00011000  0f 40 2d e9  .@-é
0x00011004  01 00 00 ea  ...ê
0x00011008  00 00 a0 e1  .. á
0x0001100C  00 00 a0 e1  .. á
0x00011010  02 00 00 eb  ...ë
0x00011014  0f 40 bd e8  .@.è
0x00011018  00 00 a0 e1  .. á
0x0001101C  00 00 a0 e1  .. á

Upvotes: 2

Views: 5722

Answers (2)

Moshe Kravchik
Moshe Kravchik

Reputation: 2341

The 8 byte offset is constant due to ARM prefetch.

Upvotes: 1

Igor Skochinsky
Igor Skochinsky

Reputation: 25278

Since in ARM mode instructions can be placed only on word boundaries, there is no need to encode the two low bits of the address (they will be 0). Thus, the immediate value in the B instruction is the delta shifted by 2 bits. For the first branch, delta is (target - PC) >> 2. target is 00011010 and PC is 00011004+8 = 0001100C. So delta = (00011010-0001100C) >> 2 = 4 >> 2 = 1. You can do the math for the second one yourself.

Upvotes: 8

Related Questions