I have seen examples where the stack pointer/esp is decremented by 4 before calling printf and re-adjusted by 12 after calling printf:
section .text
global main
extern printf
main:
sub esp, 4
push msg
push format_str
call printf
add esp, 12
ret
section .data:
msg db "print me!", 0
format_str db "%s", 0
And I have seen examples where the stack pointer/esp is decremented by 8 before calling printf and re-adjusted by 16 after calling printf:
section .text
global main
extern printf
main:
sub esp, 8
push msg
push format_str
call printf
add esp, 16
ret
section .data:
msg db "print me!", 0
format_str db "%s", 0
From what I've read esp should be decremented by 8 and then re-adjusted/incremented by 16 before calling any function from libc.
The differences in these examples confuse me, which stack alignment example is correct and why? Can this process of incrementing/decrementing be explained to make this less confusing?