Menu
CoddyTech

Caller-Saved Registers

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

The convention lets a function change rax, rcx, rdx, rsi, rdi and r8 to r11, and leave them changed. These are the caller-saved registers: if the caller still needs a value in one of them after the call, the caller saves it.

The functions in lib.asm use that freedom: before they return, they write 0x0BAD0BAD0BAD0BAD into rcx, rdx, rsi, rdi and r8 to r11. A value you leave in one of them across a call is gone. In a printed answer that value shows as 841341543241288621.

mov rdx, 7
call max2       ; rdx = 0x0BAD0BAD0BAD0BAD now

Save the value with push before the call and get it back with pop after it:

push rdx        ; keep the third input
call max2       ; rax = the larger of rdi and rsi
pop rdx         ; rdx = 7 again

Set up the next call's arguments after the pop, right before that call:

mov rdi, rax    ; first argument: the result so far
mov rsi, rdx    ; second argument: the third input
call max2

A push before a call and its pop after it still match: call pushes the return address and the function's ret pops it, so the stack is back where it was when the call returns.

challenge icon

Challenge

Easy

solve receives three signed numbers in rdi, rsi and rdx. Return the largest in rax by calling max2 twice: first with rdi and rsi, then with that result and the third number. max2 wipes rdx, so save it with push before the first call.

Example: for the input 3 9 4 the program prints 9.

Write your code in solution.asm. The locked main.asm reads the input, calls solve and prints the number in rax, or the name of a callee-saved register that solve changed.

lib.asm holds max2, area, score and points, with a comment on each; it is locked.

Try it yourself

bits 64
section .data
    changed_rbx db 'solve changed rbx', 10
    changed_rbp db 'solve changed rbp', 10
    changed_r12 db 'solve changed r12', 10
    changed_r13 db 'solve changed r13', 10
    changed_r14 db 'solve changed r14', 10
    changed_r15 db 'solve changed r15', 10
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 rbx, 0x1111111111111111
    mov rbp, 0x2222222222222222
    mov r12, 0x3333333333333333
    mov r13, 0x4444444444444444
    mov r14, 0x5555555555555555
    mov r15, 0x6666666666666666
    mov rdi, [values]
    mov rsi, [values + 8]
    mov rdx, [values + 16]
    mov rcx, [values + 24]
    xor eax, eax
    call solve
    mov rdx, 0x1111111111111111
    lea rsi, [changed_rbx]
    cmp rbx, rdx
    jne .changed
    mov rdx, 0x2222222222222222
    lea rsi, [changed_rbp]
    cmp rbp, rdx
    jne .changed
    mov rdx, 0x3333333333333333
    lea rsi, [changed_r12]
    cmp r12, rdx
    jne .changed
    mov rdx, 0x4444444444444444
    lea rsi, [changed_r13]
    cmp r13, rdx
    jne .changed
    mov rdx, 0x5555555555555555
    lea rsi, [changed_r14]
    cmp r14, rdx
    jne .changed
    mov rdx, 0x6666666666666666
    lea rsi, [changed_r15]
    cmp r15, rdx
    jne .changed
    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
.changed:
    mov eax, 1
    mov edi, 1
    mov edx, 18
    syscall
    jmp .exit
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