Callee-Saved Registers
Part of the Functions, Stack and Strings section of Coddy's Assembly journey. Lesson 14 of 33.
The other half of the rule: rbx, rbp and r12 to r15 are callee-saved. A function that uses one of them must put its old value back before ret. In return, values in these registers survive every call.
So a value that must outlive a call belongs in a callee-saved register. A loop that calls score can keep its counter in rbx and its total in r12, and score leaves both alone.
mov rdi, rbx ; the level to score
call score ; rbx and r12 are unchanged
add r12, raxYour solve is a function too: main.asm may keep its own values in rbx and r12. Save them at the start of solve and restore them at the end, in reverse order:
solve:
push rbx
push r12
; ... use rbx and r12 ...
pop r12
pop rbx
retIn this chapter main.asm checks the rule. It puts known values in rbx, rbp and r12 to r15 before it calls solve and compares them after. If one changed, it prints a line such as solve changed rbx instead of your answer.
rsp must come back unchanged as well. Every push matched by a pop gives you that, and the harness would crash at your ret otherwise.
Challenge
Easysolve receives a number n (1 to 50) in rdi. Return in rax the total of score(i) for every level i from 1 to n, calling score with the level in rdi. Keep the counter in rbx and the total in r12: push both at the start of solve and pop them, in reverse order, before ret.
Example: for the input 1 the program prints 4.
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 .exitAll 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