Arguments in Registers
Part of the Functions, Stack and Strings section of Coddy's Assembly journey. Lesson 11 of 33.
A calling convention is the set of rules every function agrees on: where the arguments go, where the result comes back, and which registers a function may change. Code written by different people, or made by a C compiler, can call each other because they follow the same rules. On Linux x86-64 the rules are called System V AMD64.
The first six integer arguments go in rdi, rsi, rdx, rcx, r8 and r9, in that order. The function returns its result in rax. To call area(3, 4):
mov rdi, 3 ; first argument: the width
mov rsi, 4 ; second argument: the height
call area ; rax = 12The locked main.asm follows the same rules: every challenge in this section has given you its inputs in rdi, rsi, rdx and rcx because it calls solve the way a C program calls a function, and reads your answer from rax.
A function can live in another file. extern area at the top of your file tells nasm that the label area is defined somewhere else; the file that defines it marks it global area, and the linker joins the two.
extern area
...
call areaEvery argument must be in its register when call runs. You can compute an argument in place: lea rdi, [rdi + rdx * 2] sets rdi to rdi + 2 * rdx in one instruction, without touching rdx.
Challenge
Easysolve receives a picture's width in rdi, its height in rsi and a border size in rdx (all 0 to 100). Call area with the framed width (width + 2 times the border) in rdi and the framed height (height + 2 times the border) in rsi, and return its result in rax.
Example: for the input 3 4 1 the program prints 30.
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 .exitThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Functions, Stack and Strings
4Stack Frames and Recursion
Local VariablesFrames with RBPRecursionTwo Recursive CallsRecap: Digit Sum3The Calling Convention
Arguments in RegistersReturn ValuesCaller-Saved RegistersCallee-Saved RegistersRecap: Scoreboard6Project: Word Counter
The Read SyscallCount the CharactersPractice on your own: Assembly playground