typestar

Stack frames in Assembly

rbp anchors locals at fixed offsets: the classic prologue.

; A stack frame: rbp anchors locals at fixed offsets.
        global _start

        section .text
area:                           ; rdi=w, rsi=h -> rax
        push rbp
        mov rbp, rsp
        sub rsp, 16             ; room for two locals

        mov [rbp - 8], rdi      ; local w
        mov [rbp - 16], rsi     ; local h
        mov rax, [rbp - 8]
        imul rax, [rbp - 16]

        mov rsp, rbp            ; epilogue
        pop rbp
        ret

_start:
        mov rdi, 6
        mov rsi, 9
        call area
        mov rdi, rax            ; 54
        mov rax, 60             ; exit code 54
        syscall

How it works

  1. push rbp; mov rbp, rsp; sub rsp, 16 opens the frame.
  2. Locals live at [rbp - 8] and [rbp - 16].
  3. The epilogue restores rsp and rbp before ret.

Keywords and builtins used here

The run, in numbers

Lines
25
Characters to type
478
Tokens
80
Three-star pace
55 tpm

At the three-star pace of 55 tokens a minute, this run takes about 87 seconds.

Type this snippet

Step 3 of 3 in The stack, step 21 of 31 in Language basics.

← Previous Next →