Menu
CoddyTech

A Stack That Must Balance

Part of the Functions, Stack and Strings section of Coddy's Assembly journey. Lesson 9 of 33.

ret does not know where it came from. It pops whatever is on top of the stack and jumps there. So a procedure must leave the stack as it found it: when its ret runs, the return address has to be on top.

Saving a register inside a helper is fine when its pop comes before ret. This helper uses rbx as the divisor and gives it back:

; add_tax: rax = rdi + rdi / 10
add_tax:
    push rbx
    mov rbx, 10
    mov rax, rdi
    xor edx, edx
    div rbx         ; rax = rdi / 10
    add rax, rdi
    pop rbx
    ret

Delete the pop rbx and ret pops the saved rbx instead, then jumps to it as if it were an address. The program crashes with a segmentation fault and prints nothing, so the test shows empty output.

An extra pop breaks it too: it takes the return address off the stack, and ret jumps to whatever is under it. The same goes for a branch that jumps to ret past a pop. Every path through a procedure needs as many pops as pushes.

When a test prints nothing, check each procedure: follow every path to its ret and count the pushes and pops on it.

challenge icon

Challenge

Easy

The starter's add_tax should return rdi + rdi / 10 in rax (a price plus 10 percent tax, rounded down), but the program crashes and prints nothing. Find the push that has no matching pop and fix the helper. solve receives the price (0 to 100000) in rdi and returns add_tax(price) in rax.

Example: for the input 100 the program prints 110.

Write your code in solution.asm. The locked main.asm reads the input, calls solve and prints the number in rax.

Try it yourself

bits 64
section .bss
    input_buffer resb 256
    output_buffer resb 32
    values resq 4
section .text
    global _start
    extern solve
_start:
    xor eax, eax
    xor edi, edi
    mov rsi, input_buffer
    mov edx, 255
    syscall
    test rax, rax
    jle .loaded
    mov rsi, input_buffer
    lea rdi, [input_buffer + rax]
    xor ecx, ecx
.scan:
    cmp rsi, rdi
    jae .loaded
    cmp ecx, 4
    jae .loaded
    movzx eax, byte [rsi]
    cmp al, 32
    jbe .space
    mov r10, 1
    cmp al, '-'
    jne .number
    mov r10, -1
    inc rsi
.number:
    xor eax, eax
.digit:
    cmp rsi, rdi
    jae .store
    movzx edx, byte [rsi]
    cmp dl, '0'
    jb .store
    cmp dl, '9'
    ja .store
    imul rax, rax, 10
    sub edx, '0'
    add rax, rdx
    inc rsi
    jmp .digit
.store:
    imul rax, r10
    mov [values + rcx * 8], rax
    inc ecx
.space:
    inc rsi
    jmp .scan
.loaded:
    mov rdi, [values]
    mov rsi, [values + 8]
    mov rdx, [values + 16]
    mov rcx, [values + 24]
    xor eax, eax
    call solve
    lea rsi, [output_buffer + 31]
    mov byte [rsi], 10
    mov ecx, 1
    xor r8d, r8d
    test rax, rax
    jns .positive
    mov r8d, 1
    neg rax
.positive:
    mov r9d, 10
.convert:
    xor edx, edx
    div r9
    add dl, '0'
    dec rsi
    mov [rsi], dl
    inc ecx
    test rax, rax
    jnz .convert
    test r8d, r8d
    jz .write
    dec rsi
    mov byte [rsi], '-'
    inc ecx
.write:
    mov edx, ecx
    mov eax, 1
    mov edi, 1
    syscall
.exit:
    mov eax, 60
    xor edi, edi
    syscall
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Functions, Stack and Strings

Practice on your own: Assembly playground