0

if i move a value to a memory under the stack pointer, will it vanish? Like:

mov [rsp-8], 9

will 9 gone even if i don't use call or push or any other instruction that affects the stack?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 2
    This depends on your ABI, OS, etc. Under traditional conventions, it won't "vanish" but it is subject to being overwritten asynchronously by things like signal handlers, so it's effectively not usable as storage. But under the SysV ABI, the first 128 bytes below the stack pointer are a "red zone" and guaranteed to be preserved. – Nate Eldredge Jul 13 '21 at 03:42
  • can i use it for passing function parameters? (this is for my compiler actually) @NateEldredge – blend_smile Jul 13 '21 at 04:03
  • Again, what ABI / OS are you trying to conform to? Are you calling your own code (you can do whatever you want) or existing ABI-conformant code? – Nate Eldredge Jul 13 '21 at 04:10
  • 2
    @NateEldredge - note that Windows does not have a red zone. The OP hasn't specified what OS is involved. – rcgldr Jul 13 '21 at 06:06
  • This isn't very useful for passing function parameters; `call` will overwrite the bytes from `rsp-8` to `rsp-1` with the return address it pushes. When a calling convention runs out of registers for args, it's normal to put values on the stack so that they'll be *above* the return address on entry to the function. (You could store args below that, but depending on a red-zone as part of your calling convention will make it unsafe to use on Windows, or in kernel code. And since `push` is generally cheap, it's not better.) – Peter Cordes Jul 13 '21 at 20:21
  • If you want feedback on a proposed calling-convention you're designing, you could ask a question about that. (But note that avoiding `call` / `ret` is normally a bad idea: hardware has a special predictor for return addresses that works much better than using normal indirect-branch prediction on a `jmp`, if you're thinking of use RIP-relative LEA to pass a link register.) – Peter Cordes Jul 13 '21 at 20:23
  • thank you guys, i ended up redesigning my calling convention. I use linux, but i'll target many OS later so i think it's better to stay away from taking risk – blend_smile Jul 14 '21 at 07:37

0 Answers0