Assembly Parameter Passing in Reverse Order?

Viewed 167

I found the following from an online book:

If the procedure has more than 6 parameters, they are pushed on the stack in reverse order (last parameter first). Pushing parameters in reverse order (as in 32bit) allows functions with a variable number of arguments (varargs)

I don't get the last sentence, pushing parameters in reverse order allows us to read parameter1 first then parameter2 etc... But how this really helps?

If parameter1 contains the total number of parameters I would understand, but that's not the case as functions like printf can take many parameters without us providing the number of parameters we passed.

1 Answers

Pushing the right-most first means that the left-most (of the non-register args) is right above the return address, i.e. at a known position, with later args above that. In a stack, the last thing you pushed is closest / at the top.

Functions like printf need their parameters in order, and the caller might have passed too many args; the ones not referenced by the format string need to be silently ignored. This would be impossible if the lowest-address arg (right above the return address) was the right-most, unless the calling convention also passed a count or pointer to the left-most stack-arg somewhere.

Also, you only find out their widths one at a time, as you parse the format string, in left-to-right order. Wide args like long double, __m128, __int128, or a large structs, can take multiple stack slots.

Related