assemblyx86cdeclstack-pointer

Incrementing %esp and CDECL


I've been reading up on the x86 stack and the CDECL convention and read something that confused me.

Among the caller's responsibilities listed were popping the parameters, using them or simply incrementing %esp to remove them.

How does that last part work?

For example, say the initial value of %esp is 0x105000 and you decrement it by $0x1c for your current stack frame. You allocate some data, then increment it back - in that case wouldn't that data still float around in memory? How is the memory cleared? Would accessing 0x104FF4 lead to a segmentation fault and if so, what cleared the data there?


Solution

  • POP just moves data to a register and adjusts the stack pointer. It doesn't erase the data or have any other side effects.

    So, if you don't need the data moved back to a register, then adjusting the stack pointer with ADD is all you need to do. You get the stack pointer back where you want it, only without having to clobber a register with a POP instruction.

    It's also potentially more efficient. You will only ever need one ADD instruction rather than a series of POPs which might be slower or result in larger code.

    The data does still exist in memory, but will be overwritten when you next push data to the stack.