# Lecture 006

## Application Binary Interface (ABI)

Application Binary Interface: designer choice to layout memory, control function calls, and passing data.

## Stack

pushq src:

• sub 0x08, %rsp: 8 byte

• mov src, (%rsp) (If we push 1 to stack top, the 1 bit will be at %rsp with little endian)

## Function Call

### Passing Control

call label:

• push %rip: push %rip that points to the line after call, this will be the return address for after a call is finished.

• jmp label

ret:

• pop %rip

### Data Flow

When having more than 6 arguments:

• 7th argument is at %rsp

### Manage Local Data

Frame: Function Call

• return location

• saved register

• local memory for variables exceeds available registers

## Common Questions

Will function over-allocate stack? as passing 7th 8th argument? (looks like there is a 8 bit gap above 8th argument)

• For alignment reasons, callee will allocate more memory than needed

• We always align to 64 bits or 8 byte (sub $0x10$rsp)

Behavior of movl, movq. mov \$0x4030d8,%esi does it assume equal size? so 0x4030d8 will be padded with 0s?

• Will zero out what's not in destination

• TODO

if a function call is a tail call, does C do optimization to stack allocation in recursion?

• compiler will do all sorts or optimization. tail-recursive might end up with non-recursive

Why do assembly generate nop (see in assembly, not 0x00)

• for alignment reason to improve performance depending on hardware

Why don't we mov %rbp %rsp in 64 bit

• people realized that might not be a good idea because every register is precious and modern compilers are getting better and better at optimization

Why would caller want to save r10 and r11 that are not arguments to callee? (they are caller-saved but not arguments)

• they are convenient for temporary calculation so that the function don't have to push and pop those register

what is rep; ret

• if there is a jmp after ret, AMD processors will have branch prediction penalty. To avoid this, rep; ret is used.

Why would mov 0x0, %eax before function call? has to do with vector?

• compiler do this for debugging (reset return value before calling function

• If I have 2 threads, 2 sets of registers doing different jobs?

Table of Content