The Stack
So far, you've been reading from memory addresses that we set up for you.
But your program already has a region of memory ready to go: the stack.
The stack is pointed to by the rsp register, and it contains useful data about how your program was launched and accumulates other data as the program executes.
Let's explore it!
The Stack
Stack Offsets
Program Arguments on the Stack
Popping From the Stack
Stack Addresses as a Number Line
The stack is easiest to reason about if you remember that addresses are just numbers. Diagrams often draw the stack vertically, but the numbers themselves still have a simple left-to-right order:
smaller addresses larger addresses
... rsp-0x10 rsp-0x08 rsp rsp+0x08 rsp+0x10 ...
Positive offsets from rsp, such as [rsp+8], read bytes at larger addresses.
Negative offsets, such as [rsp-8], read bytes at smaller addresses.
When a program starts, the kernel has already placed launch data at the starting rsp and at larger addresses to its right on this number line.