Passing parameters and return values for a subroutine in assembly

as3rdaccount picture as3rdaccount · Sep 19, 2012 · Viewed 23.9k times · Source

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?

Answer

Greg Hewgill picture Greg Hewgill · Sep 19, 2012

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.