Reading with [rsp]
Part of the Functions, Stack and Strings section of Coddy's Assembly journey. Lesson 4 of 33.
rsp holds an address, so you can read the stack like any other memory. mov rax, [rsp] copies the top value into rax without popping it: rsp stays where it is.
Each slot is 8 bytes, and older values sit at higher addresses. After pushing 10, 20 and 30:
push 10
push 20
push 30
mov rax, [rsp] ; rax = 30, the top
mov rbx, [rsp + 8] ; rbx = 20
mov rcx, [rsp + 16] ; rcx = 10, pushed firstReading does not remove anything. When you are done, give the space back: pop each value, or drop them all at once with add rsp, 24 for three values (3 times 8 bytes).
The stack must balance before ret. solve ends with ret, which reads the address to return to from [rsp]. If a value you pushed is still on top, ret uses it as an address and the program crashes before it prints anything.
Challenge
Easysolve receives four numbers in rdi, rsi, rdx and rcx. Push them in that order. Without popping, read the value second from the top and the value at the bottom (the first one pushed) with [rsp + ...], and return their sum in rax. Remove all four values before ret.
Example: for the input 1 2 3 4 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.
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
syscallThis 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