Menu
CoddyTech

Last In, First Out

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

The stack returns values in the reverse order you pushed them: the last value pushed is the first one popped. This order is called LIFO, last in, first out. A stack of plates works the same way: you take the plate you put down last.

Push 1, 2 and 3, and the pops return 3, 2 and 1:

mov rax, 1
push rax
mov rax, 2
push rax
mov rax, 3
push rax
pop rbx         ; rbx = 3
pop rcx         ; rcx = 2
pop rdx         ; rdx = 1

Each pop into the same register overwrites it. Popping three times into r8 leaves only the last value popped in r8, so use each value before the next pop.

To throw away the top value without reading it, move the stack pointer yourself: add rsp, 8 drops one value, and add rsp, 16 drops two.

push rdi
push rsi
add rsp, 8      ; the value from rsi is dropped
pop rax         ; rax = rdi

An editor keeps its undo history this way: every change is pushed, and Undo takes the newest change off the top.

challenge icon

Challenge

Easy

solve receives four digits (0 to 9) in rdi, rsi, rdx and rcx. Push them in that order, then pop them one at a time and build a number in rax: start at 0, and for each popped digit multiply rax by 10 and add the digit. Return the number, which has the digits in reverse order.

Example: for the input 1 2 3 4 the program prints 4321.

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