Menu
CoddyTech

Writing a Helper

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

A helper procedure has an agreement with the code that calls it: which register holds its input and which register gets its result. Write that agreement in a comment above the label, so every caller knows what to set and what to read.

; square: rax = rdi * rdi
square:
    mov rax, rdi
    imul rax, rdi
    ret

A helper can be called many times. Before each call, set the input register; after it, take the result out of rax before the next call overwrites it:

mov rdi, 3
call square     ; rax = 9
mov r8, rax     ; keep the 9
mov rdi, 4
call square     ; rax = 16
add rax, r8     ; rax = 25

The caller above keeps its 9 in r8, which works because square never touches r8. If a helper used r8 for its own work, that 9 would be lost. Keep a helper to the registers its comment names.

A helper's name is a normal label, without a dot. A label that starts with a dot, such as .loop, belongs to the normal label above it, so two helpers can each have their own .loop.

count_up:
.loop:              ; count_up.loop
    ret
count_down:
.loop:              ; count_down.loop
    ret
challenge icon

Challenge

Easy

Write a helper square that returns rdi * rdi in rax. solve receives a in rdi and b in rsi (both -1000 to 1000). Call square once for each and return a * a + b * b in rax.

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

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