The layer underneath C โ what your compiler actually turns your code into. Not something you write day-to-day, but understanding it is the whole point of a computer-architecture or systems course (like CMU's Bomb Lab and Attack Lab).
objdump -d) for the fastest way to actually learn it.A handful of tiny, extremely fast storage slots built directly into the CPU โ far faster than RAM, and where almost all work actually happens.
| Register | Common use |
|---|---|
| %rax | "Accumulator" โ usually holds a function's return value |
| %rbx, %rcx, %rdx | General-purpose working registers |
| %rdi, %rsi | First and second function arguments (System V calling convention) |
| %rsp | Stack pointer โ points at the top of the stack |
| %rbp | Base pointer โ anchors the current function's stack frame |
movq $5, %rax # %rax = 5 (the "q" suffix = 64-bit quadword)
movq %rax, %rbx # %rbx = %rax
movq (%rax), %rbx # %rbx = *(the value AT the address in %rax) โ like a C pointer dereference
addq $1, %rax # %rax += 1
subq %rbx, %rax # %rax -= %rbx
imulq %rbx, %rax # %rax *= %rbx (signed multiply)
A last-in-first-out region of memory used for local variables, saved registers, and return addresses โ this is what your program uses every time you call a function.
pushq %rax # save %rax's value onto the stack, move %rsp down
popq %rbx # take the top of the stack into %rbx, move %rsp back up
Assembly has no if or while โ every branch is really "compare, then jump."
cmpq %rbx, %rax # compute %rax - %rbx (result discarded, only FLAGS set)
je equal_case # jump if the last comparison was EQUAL
jg greater_case # jump if GREATER
jmp done # unconditional jump
equal_case:
# ...
done:
cmp/j* instructions and figuring out what C if/while statement they came from.callq my_function # pushes the return address, then jumps
ret # pops the return address, jumps back to the caller
Arguments are passed in registers (%rdi, %rsi, ...), and the return value comes back in %rax โ this is exactly why a C function like int add(int a, int b) compiles down to reading %rdi and %rsi and leaving its answer in %rax.