x86_64 Assembly Command Line Arguments
- by Brandon oubiub
I'm new to assembly, and I just got familiar with the call stack, so bare with me. To get the command line arguments in x86_64 on Mac OS X, I can do the following:
_main:
sub rsp, 8 ; 16 bit stack alignment
mov rax, 0
mov rdi, format
mov rsi, [rsp + 32]
call _printf
Where format is "%s". rsi gets set to argv[0].
So, from this, I drew out what (I think) the stack looks like initially:
top of stack
<- rsp after alignment
return address <- rsp at beginning (aligned rsp + 8)
[something] <- rsp + 16
argc <- rsp + 24
argv[0] <- rsp + 32
argv[1] <- rsp + 40
... ...
bottom of stack
And so on. Sorry if that's hard to read. I'm wondering what [something] is. After a few tests, I find that it is usually just 0. However, occasionally, it is some (seemingly) random number.
EDIT: Also, could you tell me if the rest of my stack drawing is correct?