Registers

Intel x86 32bit registers

Notion image

r stands for 64 bits, e stands for 32 bits.

Notion image

How function call works in assembly

  1. Firstly, we put arguments in reverse order. (push $2, push $1) $ for denoting literal.

  2. Push the current instruction pointer on the stack, and then the %eip is pointed to the start of the new function. call callee

  3. Then, we put the base pointer value on stack. We copy esp value to ebp. The compiler already knows how much space is needed for the local variable of callee, we subtract this much from the stack pointer. These new values of ebp and esp form new stack frame. (push %ebp, mov %esp %esp, ).

  4. From here we start allocating spaces for variables, int local in this case. sub $4 %esp

  5. At the end we put return value back in eax. mov $42, %eax

  6. After the function is done, we move the stack pointer back to the base of the current function stack. The local variable space is not deallocated, but it will be overridden when we want to call the next function. This saves us time . mov %ebp, %esp

  7. Then we pop the next value from the stack, which is like the EBP of caller function, into the EBP register.

  8. ret It saves; it pops the next value from the stack, which is the EIP of the previous function where we left off, and it is put back into the EIP register.

  9. We move the stack pointer to move the arguments from the current stack frame.add $8, %esp

Notion image

System v ABI

The System V AMD64 ABI (Application Binary Interface) is the standard calling convention for x86-64 processors on Unix-like operating systems, including Linux, macOS, FreeBSD, and Solaris. It specifies how functions receive parameters, how they return values, and how the stack is managed during execution.

Unlike 32-bit x86 conventions that passed most arguments on the stack, the System V AMD64 ABI uses registers for the first several arguments to improve performance. %rdi, %rsi, %rdx %rcx %r8 %r9.

Floating-Point Arguments: The first eight are passed in SSE registers: %xmm0 through %xmm7. Excess Arguments: Any additional arguments beyond these are passed on the stack in reverse order (right-to-left). Variadic Functions: For functions like printf, the %al (low byte of %rax) register must hold the number of SSE registers used to pass arguments

Return Values

**Register Preservation (Callee vs. Caller Saved) **

Notion image

Notion image