Frames with RBP
Part of the Functions, Stack and Strings section of Coddy's Assembly journey. Lesson 17 of 33.
With locals, pushes and calls, rsp keeps moving, so [rsp + 8] can mean a different slot at different points in a function. A stack frame fixes a point to measure from: the register rbp (the base pointer).
A function with a frame starts with three lines, the prologue:
push rbp ; save the caller's rbp
mov rbp, rsp ; rbp marks this frame
sub rsp, 16 ; two localsThe locals sit below rbp, at [rbp - 8] and [rbp - 16]. Those addresses name the same slots for the whole function, however many values you push later.
The epilogue undoes the prologue. mov rsp, rbp drops every local at once, and pop rbp gives the caller its rbp back:
mov rsp, rbp ; drop the locals
pop rbp ; the caller's rbp is back
retrbp is callee-saved, so the push at the start and the pop at the end are what keep it unchanged for the caller. Above rbp are the saved rbp at [rbp] and the return address at [rbp + 8]. Other callee-saved registers you use go on the stack right after the prologue and come off right before mov rsp, rbp:
push rbp
mov rbp, rsp
sub rsp, 16
push rbx
; ... the function ...
pop rbx
mov rsp, rbp
pop rbp
retChallenge
Easysolve receives n (1 to 50) in rdi. Use a frame with two locals: [rbp - 8] for the highest score seen so far and [rbp - 16] for its level. Check the levels 1 to n and keep the first level with the highest score. Return level * 100 + score in rax.
Example: for the input 5 the program prints 509.
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