This
is what the stack looks like when you start your program:
Argument
Count (integer contained in RDI)
Argument
List (list address pointed to by RSI) The next address (+8) will point to the first Argument. The following address (+16) will point to the second Argument. And so on, for the count of arguments.
This is what the stack looks like during a subroutine call:
|
Contents |
off rsp |
caller's variables |
[rsp+24] |
Argument 2 |
[rsp+16] |
Argument 1 |
[rsp+8] |
Caller Return Address |
[rsp] |
my_sub:
; Returns first argument
mov
rax,[rsp+8]
ret
(example
when using rbp and two local variables)
Contents |
off rbp |
off rsp |
Argument N |
[rbp+(N+1)*8] |
[rsp+24+(N+1*8)] |
Argument 2 |
[rbp+24] |
[rsp+40] |
Argument 1 |
[rbp+16] |
[rsp+32] |
Caller Return Address |
[rbp+8] |
[rsp+24] |
Saved rbp |
[rbp] |
[rsp+16] |
Local variable 1 |
[rbp-8] |
[rsp+8] |
Local variable 2 |
[rbp-16] |
[rsp] |
my_sub2:
; Returns first argument
push
rbp ; Prologue
mov
rbp, rsp
mov
rax, [rbp+8]
mov
rsp, rbp ; Epilogue
pop
rbp
ret
Return to Professor Page]