2 ms·
> And indeed, inside main we can see push rax - saving the value in the register rax to the stack, then a call to our add function, then we pop rax off the stac
by saagarjha 6y ago
> And indeed, inside main we can see push rax - saving the value in the register rax to the stack, then a call to our add function, then we pop rax off the stack. The push call pop sequence is to preserve whatever values are in the registers used in add.
There's really no need to save rax here; I know that optimizations are off but I'm still curious why rustc thinks this is necessary…
- sunfish 6y agoThe compiler is using push and pop here just for their side effect of adjusting the stack pointer, to keep the stack pointer 16-byte aligned.
- saagarjha 6y agoShouldn't the C runtime do that before calling main?
- sunfish 6y agoIt does; the stack pointer starts out aligned, but then the function does a call, and the call instruction adjusts the stack pointer by 8 bytes to push the return address, which would cause it to be misaligned. The push pushes an extra 8 bytes so that the stack pointer is aligned in the callee.
- SilasX 6y agoAny reason not to just do some other “increment stack pointer” option?
- glandium 6y agoThe instruction is smaller. Push rax is encoded into 1 bytes, while an increment stack pointer would be 4.