Menu
CoddyTech

PUSH and POP

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

The stack is a region of memory your program uses to keep values for a short time. The register rsp (the stack pointer) holds the address of the value on top of the stack. When your program starts, rsp already points into the stack.

push puts an 8-byte value on top. push rdi first subtracts 8 from rsp, then stores rdi at the new address in rsp. The stack grows down: every push moves rsp to a lower address. Real addresses are long numbers, so the examples use small ones.

; rsp = 1000
push rdi        ; rsp = 992, rdi is stored at 992
push rsi        ; rsp = 984, rsi is stored at 984

pop takes the top value off. pop rax copies the 8 bytes at the address in rsp into rax, then adds 8 to rsp.

; rsp = 984
pop rax         ; rax = the value from 984, rsp = 992
pop rbx         ; rbx = the value from 992, rsp = 1000

Push and pop can swap two registers without a third one. The first pop returns the last value pushed:

push rdi        ; save rdi
push rsi        ; save rsi on top
pop rdi         ; rdi = the old rsi
pop rsi         ; rsi = the old rdi

In this section the challenges call your code the way a C program calls a function. The locked main.asm puts the inputs in rdi, rsi, rdx and rcx, in that order, calls your solve, and prints the number you leave in rax. Each task says which inputs it uses. Chapter 3 explains why these registers.

challenge icon

Challenge

Easy

solve receives two numbers: the first in rdi, the second in rsi. Swap the two registers with two push and two pop instructions, then return rdi - rsi in rax.

Example: for the input 3 10 the program prints 7.

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

All lessons in Functions, Stack and Strings

Practice on your own: Assembly playground