views:

3424

answers:

2

It's been a while since I last coded arm assembler and I'm a little rusty on the details. If I call a C function from arm, I only have to worry about saving r0-r3 and lr, right? If the C function uses any other registers, is it responsible for saving those on the stack and restoring them? In other words, the compiler would generate code to do this for C functions. For example if I use r10 in an assembler function, I don't have to push its value on the stack, or to memory, and pop/restore it after a C call, do I?

This is for arm-eabi-gcc 4.3.0.

I realise I could read the whole EABI, but then shortcutting RTFM is what SO is for, right? :-)

A: 

I believe this depends on your compiler - the easiest way would be to build a simple C file using the ARM compiler you're using and look at the function prologue and epilogue generated

Paul Betts
No-there's an ARM ABI.
MSalters
Could be right - I'll clear up the question
rq
Ah, and it's pretty tricky to create a C function that uses enough registers to possibly stomp on one I'm using from assembler.
rq
+12  A: 

It depends on the ABI for the platform you are compiling for. On Linux, there are two ARM ABIs; the old one and the new one. AFAIK, the new one (EABI) is in fact ARM's AAPCS. I have a bookmark pointing to http://www.arm.com/pdfs/bsabi.zip as a place to get the ARM ABI specification, but that link seems to be stale.

Briefly:

  • r0-r3 are the argument and scratch registers; r0-r1 are also the result registers
  • r4-r8 are callee-save registers
  • r9 might be a callee-save register or not (on some variants of AAPCS it is a special register)
  • r10-r11 are callee-save registers
  • r12-r15 are special registers

A callee-save register must be saved by the callee (in opposition to a caller-save register, where the caller saves the register); so, if this is the ABI you are using, you do not have to save r10 before calling another function (the other function is responsible for saving it).

Edit: Which compiler you are using makes no difference; gcc in particular can be configured for several different ABIs, and it can even be changed on the command line. Looking at the prologue/epilogue code it generates is not that useful, since it is tailored for each function and the compiler can use other ways of saving a register (for instance, saving it in the middle of a function).

CesarB
Thanks, this seems to ring some bells. I think the first "r0-r4" in your list is a typo, right? +1 (and probably best answer unless there's a radical turn around)
rq
Yes, it was a typo (and not the only one, but I fixed the other ones before hitting submit the first time - or so I hope).
CesarB