Professor Cullen Lecture Notes

Computer Architecture and Assembly Language


64-bit x86 Program Stack




This is what the stack looks like when you start your program:


Address of Last Environmental Variable

...

...

Address of Environmental variable 3

Address of Environmental variable 2

Address of Environmental variable 1

8 NULL bytes

Address of Last Argument

...

...

Address of Argument 3 (RSI+24)

Address of Argument 2 (RSI+16)

Address of Argument 1 (RSI+8)

Address of Argument 0 (RSI+0)

(Program PathName)

Argument Count (RDI)







Argument Count (integer contained in RDI)
This will ALWAYS be at least 1, even without passing any parameters to your program. Why? Linux will always give you the path to your app.



Argument List (list address pointed to by RSI)
The first address will point to the pathname of the program.

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:

Saved RSI

Saved RDI

Local Variable 3 (RBP-24)

Local Variable 2 (RBP-16)

Local Variable 1 (RBP-8)

Saved RBP

Return Address (RBP+8)

Parameter 1 (RBP+16)

Parameter 2 (RBP+24)

Parameter 3 (RBP+32)

...

...







Stack Frame

(example without rbp or local variables)



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]