CALL and RET
Part of the Functions, Stack and Strings section of Coddy's Assembly journey. Lesson 6 of 33.
A procedure is code you can run from several places in a program. In Assembly it is a label whose code ends with ret. call double runs the code at double, and its ret sends the program back to the instruction after the call.
call remembers where to come back to by pushing it. call double pushes the address of the next instruction (the return address), then jumps to double.
; rsp = 1000
call double ; pushes the return address: rsp = 992
add rax, 1 ; the return address points hereret pops that address into rip, the register that holds the address of the next instruction. The program continues right after the call, and rsp is back where it was.
double:
add rax, rax
ret ; pops the return address: rsp = 1000Before a call, put the value where the procedure expects it. Here double doubles rax, so solve copies its input into rax first:
solve:
mov rax, rdi
call double ; rax = rdi * 2
ret
double:
add rax, rax
retYour solve is a procedure too. The locked main.asm runs call solve; your ret returns to it, and it prints rax, so every solve ends with ret.
Challenge
EasyWrite a procedure double that doubles the value in rax and leaves the result in rax. solve receives a number n (-1000 to 1000) in rdi: copy it into rax, call double twice, and return the result (4 times n) in rax.
Example: for the input 3 the program prints 12.
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
syscallAll 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