Question

I am fairly new to PPC assembly, and I am interested in how the compiler encodes the branch link.

Example:

00000000 fnc_1()
00000000 std r1 , 0x7FFFF
00000004 li r2 , 0x0
00000008 mr r3 , r2
*** -> 0000000C bl fnc_2 -> | 10010 | ?? 24bits ?? | 0 | 1 | ****
00000010 blr

00000014 fnc_2()
00000014 ..
00000018 ..
0000001C ..
00000020 blr

So, my question boils down to: How does the compiler take the offset 0x14, and transform it into the 24bits in the bl opcode. I have looked at programs in hex but still cant figure it out. I have read this: http://www.eecs.umich.edu/eecs/courses/eecs373/GreenBook/ch4-bran.pdf Any clarification would help!

Was it helpful?

Solution

From the PPC ISA (PDF Link), bl is:

bits    value
0-5     18
6-29    LI
30      AA (0)
31      LK (1)

The LK bit being set indicates that the next instruction address should be placed into the link register. When AA is 0, the branch target address is the sum of LI << 2, sign extended, with the address of the current instruction. So for you, since you want to jump to 0x14 and the current location is 0xC, you just want to put 0x14 - 0xC == 0x8 into the register. Chopping off the two bottom zeroes (to account for the instruction doing the opposite operation at runtime) leaves you with an encoding LI == 0x2. So finally, the encoding is:

  18               LI            AA  LK
 10010 000000000000000000000010  0    1     
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top