Umm.. Actually ☝️
The x86 architecture and MIPS share a lot of common ideas. The % registers we talk about below are just different flavours of the $ registers from MIPS.
Components
- Like MIPS, you have program registers:
%eax
- The main register for holding results of arithmetic%ebx
- The main register for holding data.%esp
- Extended Stack Pointer - Points to the top of the stack. This updates every new operation that’s done.%ebp
- Extended Base Pointer - Whenever you jump to a new function, this points to the base of that function.
Memory Address Space in x86:
Let’s walk through the provisioning of memory in x86… Below is a diagram of it, as well as of C code we’re about to run.
Quickly! What’s a stack frame!
Every time you run a function, you’re actually jumping to a different location in memory. But there’s stuff you need to remember. For example:
- The parameters you’re bringing along with you.
- Where to return to once you’re done. (Return Address)
- Stack Base Pointer - Look at
%ebp
above. - etc.
Note: Remember this is Virtual Memory, so the addresses are actually all split up and correspond to different physical addresses. Fucking love OS.
Step By Step:
- Firstly, you add the function arguments to memory, in reverse order.
- You push the return address (the place to return to once the function is done).
- Stack Base Pointer
%ebp
- look above. - Exception handlers
- Canary goes here. Cos if your local variables are set to overwrite the base pointer, they’d trip up here first
- Local Variables
Careful!
- You should be incredibly careful when dealing directly with memory. Improper handling can lead to Data Overflow