Valhalla Legends Forums Archive | Assembly Language (any cpu) | Interesting problem for me

AuthorMessageTime
tA-Kane
When running a breakpointed test app, I'd decided to see what I knew about PPC ASM. So, I'd set my IDE to display the breakpointed code as mixed C and Assembly.

I've figured out what most of the ASM code does, but (obviously?) I'm unsure of what some does. So, I'd decided to write a small function in ASM to call a few functions, store the results, return, and have the calling function call printf() on the result.

But, when I tried doing so, my compiler says "Unknown assembler instruction mnemomic" pointing to the mnemomic which I took from the mixed code. Interesting, I think.

I've gathered that it, and a corresponding mnemomic save and restore the return address, but I don't know what would happen if I were to call them subsequently (would the stored return address be overwritten? Are the return addresses stored in some sort of LIFO (Last In First Out) array?) on my own.

Here's the code which brought up the whole situtation:[code]void EventTest(void){
//beginning of routine; set up registers
3F75FC84: 7C0802A6 mflr r0            // ?? "Save link register"
3F75FC88: 93E1FFFC stw r31,-4(sp)      // Store Mask into r31
3F75FC8C: 93C1FFF8 stw r30,-8(sp)      // Store Wait into r30
3F75FC90: 93A1FFF4 stw r29,-12(sp)   // Store rVal into r29
3F75FC94: 90010008 stw r0,8(sp)      // ??
3F75FC98: 9421FFA0 stwu sp,-96(sp)      // Move stack pointer for local params

...

}//end of routine, restore registers
3F75FD1C: 80010068 lwz r0,104(sp)      // ??
3F75FD20: 38210060 addi sp,sp,96      // Move stack pointer back to where it was
3F75FD24: 7C0803A6 mtlr r0            // ?? "Restore link register"
3F75FD28: 83E1FFFC lwz r31,-4(sp)      // Reset register 31
3F75FD2C: 83C1FFF8 lwz r30,-8(sp)      // Reset register 30
3F75FD30: 83A1FFF4 lwz r29,-12(sp)   // Reset register 29
3F75FD34: 4E800020 blr                  // Return to caller[/code]
...mflr and mtlr being the mnemomics I'm referring to.

I've done some searching on google, and the results simply say "Save link register" and "Restore link register", but none of the references say what would happen if you were to use them subsequently (for example, call mflr or mtlr twice in a row).

And here's the code which my IDE is giving me a problem with...[code]void EventTest(void){
...
   DoSomething();
3E70ABC0: 480000C1 bl *+192 ; $3E70AC80 //break to DoSomething
3E70ABC4: 60000000 nop
   printf("%i",Wait);
3E70ABC8: 386203B4 addi r3,rtoc,948//move address of the string to r3
3E70ABCC: 7FE4FB78 mr r4,r30 //move Wait to r4
3E70ABD0: 48003569 bl *+13672 ; $3E70E138 //break to printf()
...
}[/code]That runs fine, when DoSomething() doesn't have the mflr and mtlr mnemomics, but when I add them, like so, my IDE doesn't like it, and so I'm unable to just see the value of the link register, let alone see what happens if I were to try to call mflr again...[code]asm void DoSomething(void){
mtlr r0 //restore link register; IDE doesn't like this mnemomic
mr r30,r0 //move r0 into r30 (Wait is stored in r30 in EventTest())
mflr r0 //save link register to what it was; nor does it like this one
}[/code]Note that the assembly for EventTest() is what the compiler created, and the assembly for DoSomething() is what I created.

Anyways, my question is this... since google seems not to be of much help, where might I find more thorough documentation on mflr and mtlr?
May 23, 2003, 11:31 PM
iago
I would suggest finding the official website for that architecture.

I don't know anything about that exact architecture, but in 68k (which is old mac stuff) before and after a function we used an operand called movem (move many) to store registers and movem again to get them back, and that was the only special thing we had to do at the top and bottom of functions. I could be wrong, though, it's been awhile :)
May 23, 2003, 11:57 PM
tA-Kane
Oh, silly me... I had transposed the l and the r in mtlr and mflr..., so it came out to mtrl and mfrl.

I'll be more careful about typos before I think about asking for help again :-\

But after some additional searching on the IBM site, I found out that mtrl and mfrl are actually mtspr and mfspr, with lr as the source special purpose register (eg, extended mnemomics). And yes, consecutive writing to the lr would indeed overwrite the previous lr value.
May 24, 2003, 1:01 AM

Search