0

I see arm 32 bits assembly code.

Usually each function do push { r4-r9,lr} ( save 6 registers on stack ). And in the end of function pop them .

I see function that save push {r0,r1,r4-r9,lr} but in the end of function pop only r4-r9 Why is that? R0 and r1 suppost to be args, why to push them to stack?

By the way, what lr meam?

2

The compiler generates this code depending on which registers it needs to be preserved in the caller function. So usually r4-r9 are saved and then restored in the end of the function. r0 and r1 may be saved to stack in order to reuse them later.

Ex:

void main(argc, argv)
{
  ParseStringArguments(&stringArguments, argc, argv);
  ParseNumericArguments(&numericArguments, argc, argv);
}

main will get argc and argv through r0 and r1. Then it will be necessary to call ParseStringArguments. stringArguments will be passed through r0, argc through r1 and argv through r2. So the compiler will have to rewrite r0 and r1 with the new values. Where will it get the values of argc and argv when it comes to the call of ParseNumericArguments? So compiler generates the code to store argc and argv to stack, then load them back right before the call.

LR holds return address. https://en.wikipedia.org/wiki/Link_register function uses it to return to the caller function via

MOV PC, LR 

or

BX LR

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.