x86_64 Assembly Command Line Arguments

Posted by Brandon oubiub on Stack Overflow See other posts from Stack Overflow or by Brandon oubiub
Published on 2012-04-04T03:14:00Z Indexed on 2012/04/04 5:29 UTC
Read the original article Hit count: 382

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?

© Stack Overflow or respective owner

Related posts about assembly

Related posts about stack