Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters and return values for a subroutine in assembly

I am working with ARM assembly, where I have to write one subroutine for which I am following the ARM calling convention(this will have to be integrated with some separate higher level implementation somewhere else ) for passing parameters and return values.

Now here is something I am not sure in general when working with assembly.

So from the convention if I understand well the arguments are passed in order starting from registers r0 - r4 and then for other arguments stacks are used. Return values are passed to r0.

Now here is what I am confused with. If I am supposed to save the context of r0 and pop it off after then there is no way to return the result, the only way it can be done is by corrupting the first argument.

Is there a workaround somehow?

like image 521
as3rdaccount Avatar asked Sep 19 '12 00:09

as3rdaccount


2 Answers

When you pass back the return value in r0, the caller expects that you will do this. The caller does not expect that r0 will still contain the same value as the original first parameter, because r0 is specifically where the return value is.

Normally the ARM calling convention requires that the subroutine preserves r4 through r11, not r0 through r3. So there is no contradiction anyway.

like image 93
Greg Hewgill Avatar answered Nov 10 '22 19:11

Greg Hewgill


Why not just try this yourself and see what the compiler does?

unsigned int fun ( unsigned int a, unsigned int b )
{
    return(a+b);
}

compile to object and disassemble

arm-none-eabi-gcc -O2 -c fun.c -o fun.o
arm-none-eabi-objdump -D fun.o 

and the result is

00000000 <fun>:
   0:   e0810000    add r0, r1, r0
   4:   e12fff1e    bx  lr

The two inputs a and b are passed in using r0 and r1. r0-r4 do not have to be preserved, in particular r0 because it is the return value cannot be preserved. So as the C code required the two operands are added together, and as the calling convention requires the result is returned in r0. r0 = r0 + r1.

The compiler has to conform to the convention otherwise the code it produces wont work so you can simply compile code and disassemble to find out quite a bit about the calling convention for a particular compiler and target.

like image 27
old_timer Avatar answered Nov 10 '22 19:11

old_timer