4

I'm wondering if it makes sense to push the return value of a function in its the stack-frame.

I know return values are mostly stored in registers (eax for gcc), but is it for performance only?

Thanks!

ms123
  • 581
  • 4
  • 12

1 Answers1

9

it makes sense, but it must be manually inserted (and not a simple push). the 'space' for it should be 'allocated' by the caller (it must decrease sp before calling the function) because when you return from the function - the return address must be at the top of the stack, so the return value should be below the return address. [same principle as passing arguments on stack]

amit
  • 175,853
  • 27
  • 231
  • 333
  • Thank you for the clear explanation. So in this case, the caller would have to assign the return value to some variable (if needed), then pop it, and pop the parameters? – ms123 Aug 08 '11 at 10:56
  • 1
    yes. before calling the function, the caller will decrease the sp, to provide enough space for the return value. when the function will end - the caller will have his return value on top of the stack [the return address was already popped], he will have to assign it somewhere and pop it from the stack. – amit Aug 08 '11 at 11:00