Is this what bl does?

Started by doomkaiber001, December 04, 2010, 07:11:12 PM

Previous topic - Next topic

doomkaiber001

If you had the following part of a code;

cmpwi r5, 200
bl Ammo

Does this link to the ammo label, and store the address in r5 into the Link Register?

dragonboy269


mflr r0    //preserve the previous return
bl Label  // branch to "Label" and save the address of the return in the lr
Return:  //you don't need this here, but this is where you'll return from the bl
mtlr r0   //restore the previous return
//code here

Label:
//code here
blr    //return to the address in the lr


QuoteIf you had the following part of a code;

cmpwi r5, 200
bl Ammo

Does this link to the ammo label, and store the address in r5 into the Link Register?
It wouldn't store r5 in the link register. The location right after you use the bl operation will be stored in the link register. When you use "blr" you'll return to where you left off.
If you have any requests for AC:WW/AC:CF codes, send me a message. :D

doomkaiber001

Ok, thanks for that! So what does la do? It's description is Load Address based upon offset value, but the explanation of operation is rD ← Label

dragonboy269

Quote from: doomkaiber001 on December 04, 2010, 08:47:22 PM
Ok, thanks for that! So what does la do? It's description is Load Address based upon offset value, but the explanation of operation is rD ← Label
I've never used that. I found an example of it being used though.
la r5,0x5(r11)
If you have any requests for AC:WW/AC:CF codes, send me a message. :D

dcx2

la is a mnemonic.  It's actually an addi.

la rD, d(rA)

is the same as

addi rD, rA, d

dcx2

Expanding on bl, here is a snippet from a Gecko.NET log file made while stepping through some ASM one instruction at a time.

803A2E24:  801F000C   lwz   r0,12(r31)   r0 = 803A2DFC   r31 = 81276610   [8127661C] = 64400000
803A2E28:  54005FFF   rlwinm.   r0,r0,11,31,31   r0 = 64400000   r0 = 64400000
803A2E2C:  41820040   beq-   0x803a2e6c
   ...   ...   ...   ...
803A2E6C:  7FE3FB78   mr   r3,r31     r3 = 00000001   r31 = 81276610
803A2E70:  3880001F   li   r4,31      r4 = 00000000
803A2E74:  48007D5D   bl   0x803aabd0
803AABD0:  80630980   lwz   r3,2432(r3)   r3 = 81276610   r3 = 81276610   [81276F90] = 00000000
|  803AABD4:  2C030000   cmpwi   r3,0       r3 = 00000000
|  803AABD8:  40820028   bne-   0x803aac00
|  803AABDC:  38600000   li   r3,0       r3 = 00000000
|  803AABE0:  4E800020   blr              LR = 803A2E78
803A2E78:  2C030000   cmpwi   r3,0       r3 = 00000000
803A2E7C:  4182001C   beq-   0x803a2e98
   ...   ...   ...   ...
803A2E98:  A01F0432   lhz   r0,1074(r31)   r0 = 00000000   r31 = 81276610   [81276A42] = 00000000
803A2E9C:  2C000000   cmpwi   r0,0       r0 = 00000000
803A2EA0:  41820024   beq-   0x803a2ec4



At 803A2E74, there is a bl.  The bl will branch to the address 803AABD0, but it will *also* put the address 803A2E78 (the instruction *after* the bl) into the LR.

When the function at 803AABD0 is done, it will use blr.  This will branch to the link register, which will bring execution back to the next instruction after the bl at 803A2E74

doomkaiber001

Ok, I understand a bit of that... Where I'm confused is how 803A2E78 is after the address bl to.

dcx2

The bl lives at address 803A2E74.  Every instruction for the PowerPC CPU in the Wii is four bytes long.  Therefore, the instruction after bl will live at 803A2E74 + 4 = 803A2E78.

The snippet does not show code as it is laid out in memory (like how Disassembler shows it).  It shows code "as the CPU executes it".  That's why you see the ...'s - those are branches that were taken; notice how the address changes by a value other than 4 at those points.  That's also why I indent the function call and add |'s to it, so you can see the original 803A2E74 connecting with the 803A2E78 after it.

doomkaiber001

Oh... Yeah. I get it. Anything else I should know?