# Stack arguments and a stack frame

[Courses](https://asm-editor.specy.app/learn/courses.md) › [x86-64 assembly](https://asm-editor.specy.app/learn/courses/x86.md) › Examples

Web page: https://asm-editor.specy.app/learn/courses/x86/examples/subroutine-with-stack-arguments

> Pass a seventh argument on the stack, use a frame pointer for local variables, and save and restore a callee-saved register.

The same program in: [M68K](https://asm-editor.specy.app/learn/courses/m68k/examples/subroutine-with-stack-arguments.md), [MIPS](https://asm-editor.specy.app/learn/courses/mips/examples/subroutine-with-stack-arguments.md), [RISC-V](https://asm-editor.specy.app/learn/courses/risc-v/examples/subroutine-with-stack-arguments.md), [Z80](https://asm-editor.specy.app/learn/courses/z80/examples/subroutine-with-stack-arguments.md)

The first six integer arguments travel in registers. For a seventh, the caller uses the stack. This
example adds seven numbers and uses `rbp` to keep the stack argument and a local variable easy to
find while `rsp` changes.

```x86
default rel
global _start

section .text
; sum7(a, b, c, d, e, f, g) -> the total
sum7:
    push rbp                ; save the caller's frame pointer
    mov rbp, rsp            ; fixed base for this frame
    sub rsp, 16             ; reserve two local qwords
    push rbx                ; save the caller's rbx before using it

    mov rbx, rdi            ; total of the first six arguments
    add rbx, rsi
    add rbx, rdx
    add rbx, rcx
    add rbx, r8
    add rbx, r9
    mov [rbp - 8], rbx      ; write that total to the first local
    add rbx, [rbp + 16]     ; add the seventh argument
    mov rax, rbx            ; return the answer

    pop rbx                 ; restore the caller's rbx
    leave                   ; mov rsp, rbp, then pop rbp
    ret

_start:
    mov rdi, 1
    mov rsi, 2
    mov rdx, 3
    mov rcx, 4
    mov r8, 5
    mov r9, 6
    sub rsp, 8              ; padding: _start began with rsp aligned to 16
    push 7                  ; seventh argument; rsp is aligned again
    call sum7
    add rsp, 16             ; remove the argument and padding
    mov r12, rax            ; keep the answer, 28, visible in the register view

    mov rax, 60
    xor rdi, rdi
    syscall
```

The playground starts `_start` with `rsp` divisible by 16. `sub rsp, 8` and `push 7` each move it
by eight bytes, so it is divisible by 16 immediately before `call`, as the System V convention
requires. The caller reserves the padding before pushing the argument, so the padding is at a
higher memory address than the argument.

Set a breakpoint at `mov rax, rbx`. Inspect `rbp` and `rsp` in the register panel, then enter the
value of `rsp` in the memory panel's address box. Use the offsets in this table to find the slots.
Reserved slots may have no names in the view.
At this point `rsp = rbp - 24` and `rbx` holds 28.

| Location     | Meaning                         | How it got there                          |
| ------------ | ------------------------------- | ----------------------------------------- |
| `[rbp + 24]` | alignment padding; value unused | `sub rsp, 8` reserved it                  |
| `[rbp + 16]` | 7, the seventh argument         | `push 7` wrote it                         |
| `[rbp + 8]`  | return address                  | `call sum7` wrote it                      |
| `[rbp]`      | caller's `rbp`                  | `push rbp` wrote it                       |
| `[rbp - 8]`  | 21, total of the first six      | `sub rsp, 16` reserved it; `mov` wrote it |
| `[rbp - 16]` | second local; value unused      | `sub rsp, 16` reserved it                 |
| `[rbp - 24]` | saved caller's `rbx`            | `push rbx` wrote it                       |

Reserving a slot only moves `rsp`; it does not put a useful value there. Neither the padding nor
the second local is read, so do not rely on whatever bytes the debugger displays for them. The
first local becomes 21 only when `mov [rbp - 8], rbx` runs.

`rbp` stays fixed while `rsp` changes. That makes `[rbp + 16]` the seventh argument and
`[rbp - 8]` the first local throughout this call. To finish, `pop rbx` must run before `leave`:
`leave` sets `rsp` back to `rbp`, past the saved `rbx`. Then `ret` reads the return address.
The caller's `add rsp, 16` releases both the argument and its padding. Step past `mov r12, rax`
to see 28 in `r12`; the exit setup then reuses `rax`.

## Your turn

Complete a call to `sum7(3, 4, 5, 6, 7, 8, 9)`. The first six arguments are set up for you.
In the caller, reserve padding, push the seventh argument, and remove both after the call. In
`sum7`, build a frame, reserve 16 bytes, save `rbx`, add the first six arguments in `rbx`, store
that subtotal at `[rbp - 8]`, then add the seventh from `[rbp + 16]`. Return the total in `rax`
and restore both `rbx` and the frame.

Before running, predict the subtotal and final answer. Press **Test** to check that `r12` has
the answer, `r13` has the caller's original `rbx`, and `r14` and `r15` are zero. The caller
records `rsp % 16` in `r14` immediately before `call`, so zero checks the call's alignment.
It subtracts the final `rsp` from the original one in `r15`, so zero also checks cleanup.

*Exercise: the code to start from, checked by the testcase after it:*

```x86
default rel
global _start

section .text
sum7:
    ; Build a frame, reserve two qwords, and save rbx.

    ; Add the first six arguments in rbx and store the subtotal locally.
    ; Add the seventh argument and put the answer in rax.

    ; Restore rbx and the frame before returning.
    ret

_start:
    mov rbx, 1234           ; recognizable value the caller wants preserved
    mov r15, rsp            ; remember the original stack pointer
    mov rdi, 3
    mov rsi, 4
    mov rdx, 5
    mov rcx, 6
    mov r8, 7
    mov r9, 8
    ; Add padding and push the seventh argument, 9.

    mov r14, rsp
    and r14, 15             ; record rsp % 16 immediately before call
    call sum7
    ; Remove the seventh argument and padding.
    sub r15, rsp            ; zero only if the caller restored rsp

    mov r12, rax            ; answer
    mov r13, rbx            ; caller's rbx after the call
    mov rax, 60
    xor rdi, rdi
    syscall
```

*Testcase for the program above (JSON):*

```json
{
    "expectedRegisters": {
        "r12": 42,
        "r13": 1234,
        "r14": 0,
        "r15": 0
    }
}
```

<details>
<summary>Show solution</summary>

*Solution:*

```x86
default rel
global _start

section .text
sum7:
    push rbp
    mov rbp, rsp
    sub rsp, 16
    push rbx

    mov rbx, rdi
    add rbx, rsi
    add rbx, rdx
    add rbx, rcx
    add rbx, r8
    add rbx, r9
    mov [rbp - 8], rbx      ; 33
    add rbx, [rbp + 16]     ; 33 + 9 = 42
    mov rax, rbx

    pop rbx
    leave
    ret

_start:
    mov rbx, 1234
    mov r15, rsp
    mov rdi, 3
    mov rsi, 4
    mov rdx, 5
    mov rcx, 6
    mov r8, 7
    mov r9, 8
    sub rsp, 8
    push 9
    mov r14, rsp
    and r14, 15
    call sum7
    add rsp, 16
    sub r15, rsp

    mov r12, rax
    mov r13, rbx
    mov rax, 60
    xor rdi, rdi
    syscall
```

</details>

## Instructions in this Lecture

- x86-64: [`push`](https://asm-editor.specy.app/documentation/x86/instruction/push.md), [`mov`](https://asm-editor.specy.app/documentation/x86/instruction/mov.md), [`sub`](https://asm-editor.specy.app/documentation/x86/instruction/sub.md), [`add`](https://asm-editor.specy.app/documentation/x86/instruction/add.md), [`pop`](https://asm-editor.specy.app/documentation/x86/instruction/pop.md), [`leave`](https://asm-editor.specy.app/documentation/x86/instruction/leave.md), [`ret`](https://asm-editor.specy.app/documentation/x86/instruction/ret.md), [`call`](https://asm-editor.specy.app/documentation/x86/instruction/call.md), [`xor`](https://asm-editor.specy.app/documentation/x86/instruction/xor.md), [`syscall`](https://asm-editor.specy.app/documentation/x86/instruction/syscall.md), [`and`](https://asm-editor.specy.app/documentation/x86/instruction/and.md)

Previous: [A subroutine with its arguments in registers](https://asm-editor.specy.app/learn/courses/x86/examples/subroutine-with-register-arguments.md)

Next: [Recursion: factorial and Fibonacci](https://asm-editor.specy.app/learn/courses/x86/examples/factorial-and-fibonacci.md)
