Search code examples
assemblystackcalling-convention

BSD Stack Clean Up


I am learning Assembly language and have a question about calling conventions, and the stack clean up.

Since I am using OSX I need to use the BSD calling convention for system calls which looks like this.

SECTION .text
    push StringLen ; Push string length 
    push MyString  ; Push the string
    push 0x1       ; Push the file descriptor (stdout)
    mov eax, 4     ; Push the system call (sys_write)
    int 0x80       ; Call kernel dispatcher
    add esp, 0x10  ; Clean up stack 16 bytes for 4DWORDS

    mov eax, 1
    mov ebx, 0
    int 0x80       ; System exit

My question, is add esp, 0x10 considered good practice for cleaning up the stack? I've experimented and after the clean up it would appear the values are still on the stack, but are overwritten when another value is pushed e.g.

    add esp, 0x10  ; Clean up stack 16 bytes for 4DWORDS
    push 0x1A      ; New push overwrites the previous stack values

I'm sure this doesn't make a great deal of difference in a small program, but in a large one is it not wasted space if it never gets overwritten?


Solution

  • but in a large one is it not wasted space if it never gets overwritten?

    No, it's not wasted space. The sixteen bytes of the stack space will get reused over and over again.