简体   繁体   中英

ARMv6 Best Practices for Register Use in Function

Total n00b at Assembly, but I feel like I'm getting the hang of it. However I have a question about best practices for using registers in a function.

As I understand it: of the 13 available general purpose registers on the ARM11, by convention registers 0-3 are meant to be used for passing in arguments (with 0 & 1 also being used for return values) while 4-12 are meant to be used for storing working values for the duration of the function.

However I've also seen code examples where people use registers 0-3 for working values as well, so long as any of them are available, since they do not require a push & pop of the previous value onto the stack.

While I can understand why someone might want to avoid the extra push & pop steps, it seems that using r0-r3 for anything aside from passing values in and out of a function could lead to problems down the road (since you have no guarantee that any function you call will preserve their values).

So what then is the best practice here? When should I (if ever) use registers 0-3 for working values and when should I dip into registers 4-12?

it seems that using r0-r3 for anything aside from passing values in and out of a function could lead to problems down the road (since you have no guarantee that any function you call will preserve their values).

That's exactly when you could use r4-r11 since the ABI specifies that the callee must preserve these values :)

Registers r0-r3 are caller saved so the caller must ensure that any important values stored in those registers are saved before a function call. As the callee, you can do whatever you want on these registers.

... it seems that using r0-r3 for anything aside from passing values in and out of a function could lead to problems down the road (since you have no guarantee that any function you call will preserve their values).

Registers are faster than memory, registers are faster than L2 cache, registers are faster than L1 cache, registers are fast. By using R4-R8, you are creating extra store and loads. In hand-coded assembler, this will create extra instructions. For the ARM leaf assembler function, there is NO prologue and the epilogue is bx lr . How simple.

Your statement it seems that using r0-r3 for anything aside from passing values is not correct for a great many algorithms and functions. Consider a GCD implementation,

int gcd(int a, int b)
{
   while(a!=b)
     if(a>b)
       a = a - b;
     else
       b = b - a;
   return a;
}

The parameters a and b are constantly updated during the algorithm. The original a and b values are never needed after the first iteration. This fact is well known in compiler optimization as Static single assignment form . The registers are renamed as a 0 , a 1 , etc.

So the input parameters often do not need to be keep around. There is no need to copy them to an r4-r8 and force a generation of a stack frame. ARM compilers will strive to not do this. There is no need for a human to hand code this. If you have to, you are probably better off letting a compiler produce code, unless you are learning. An ARM gcd algorithm from David Seal's ARM ARM is,

gcd: cmp r0, r1
     subgt r0, r0, r1
     sublt r1, r1, r0
     bne gcd
     bx  lr

The routine is five instructions. If you saved the input parameters you would double the size of the routine.

gcd:
   stmfd sp!, {r4, r5}   ; extra code plus two data
   mov r4, r0            ; extra code
   mov r5, r1            ; extra code

1: cmp r4, r5
   subgt r4, r4, r5
   sublt r5, r5, r4
   bne 1b

   mov r0, r4            ; extra code to setup return
   ldmfd sp!, {r4, r5}   ; extra code plus two data
   bx  lr

For small inputs, you could triple the execution time. It is also arguably easier to understand the assembler without the extra code. You should never save registers you don't use. For production quality, professional code it always makes sense to use r1 to r3 in place and fore-go storing them on the stack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM