Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion labs/lab-09/reading/calling-convention.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ main:

mov rdi, fmt
mov rsi, text
xor rax, rax
Comment thread
alexf05 marked this conversation as resolved.
call printf

pop rbp
xor rax, rax
ret
```

Expand Down
18 changes: 12 additions & 6 deletions labs/lab-09/reading/memory-layout-c-asm.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,20 @@ section .data
text db "291 is the best!", 10, 0
strformat db "%s", 0

section .code
section .text
global main

main:
push dword text
push dword strformat
call printf
add esp, 8
ret
push rbp
mov rbp, rsp

mov rdi, strformat
mov rsi, text
xor rax, rax
call printf

leave
ret
```

Note that the procedure is declared as global and is called `main` - the starting point of any C program.
Expand Down
1 change: 1 addition & 0 deletions labs/lab-09/tasks/max-assembly-calls/solution/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ main:
mov rdi, fmt
mov rsi, rax
mov edx, dword [pos]
xor rax, rax
call printf

; set exit code 0 (in main)
Expand Down
13 changes: 11 additions & 2 deletions labs/lab-09/tasks/max-assembly-calls/support/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ section .data
arr: dd 19, 7, 129, 87, 54, 218, 67, 12, 19, 99
len: equ $-arr

fmt: db "max: %u", 10, 0
fmt: db "max: %u on position: %u", 10, 0

section .bss
; we are _reserving_ space for a double word (4 bytes)
; but we are not initializing it; so it can't reside in .data
pos: resd 1

section .text

Expand All @@ -23,12 +28,16 @@ main:
shr rsi, 2

mov rdi, arr
mov rdx, pos
call get_max

; print maximum value
; print maximum value and its position
; NOTE: RAX holds the return value of get_max()
; NOTE: pos written by get_max() at given memory address
mov rdi, fmt
mov rsi, rax
mov edx, dword [pos]
xor rax, rax
call printf

; set exit code 0 (in main)
Expand Down
43 changes: 28 additions & 15 deletions labs/lab-09/tasks/max-c-calls/solution/max.asm
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,46 @@ section .text
global get_max


; RDI = array pointer
; RSI = array length
; RDX = pos pointer
; EDI = array pointer
; ESI = array length
; EDX = pos pointer
get_max:
push rbp
mov rbp, rsp
push ebp
mov ebp, esp

; save registers
push edi
push esi

mov edi, [ebp + 8]
mov esi, [ebp + 12]
mov edx, [ebp + 16]


; initialize EAX with the first value as currently known maximum
mov eax, [rdi]
mov [rdx], eax
mov eax, [edi]
mov dword [edx], 0

; initialize RCX as loop counter for remaining elements
mov rcx, rsi
dec rcx
; initialize ECX as loop counter for remaining elements
mov ecx, esi
dec ecx

; loop over remaining array elements
compare:
cmp eax, [rdi + 4*rcx]
jge check_end
cmp eax, [edi + 4*ecx]
jae check_end

; update maximum and its position
mov eax, [rdi + 4*rcx]
mov [rdx], ecx
mov eax, [edi + 4*ecx]
mov [edx], ecx
check_end:
loop compare

; result stored in RAX
; result stored in EAX

; restore registers
pop esi
pop edi

leave
ret
Expand Down
35 changes: 21 additions & 14 deletions labs/lab-09/tasks/max-c-calls/support/max.asm
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,36 @@ section .text
global get_max


; RDI = array pointer
; RSI = array length
; EDI = array pointer
; ESI = array length
get_max:
push rbp
mov rbp, rsp
push ebp
mov ebp, esp

; save registers
push edi
push esi

mov edi, [ebp + 8]
mov esi, [ebp + 12]

; initialize EAX with the first value as currently known maximum
mov eax, [rdi]
mov eax, [edi]

; initialize RCX as loop counter for remaining elements
mov rcx, rsi
dec rcx
; initialize ECX as loop counter for remaining elements
mov ecx, esi
dec ecx

; loop over remaining array elements
compare:
cmp eax, [rdi + 4*rcx]
jge check_end
mov eax, [rdi + 4*rcx]
cmp eax, [edi + 4*ecx]
jae check_end
mov eax, [edi + 4*ecx]
check_end:
loop compare

; result stored in RAX
; restore registers
pop esi
pop edi

leave
ret

6 changes: 3 additions & 3 deletions labs/lab-09/tasks/regs-preserve/solution/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ next:
mov rdi, newline
call printf

; restore preserved register
pop rbx

; restore the stack after calling printf
add rsp, 8

; restore preserved register
pop rbx

leave
ret

Expand Down
4 changes: 3 additions & 1 deletion labs/lab-09/tasks/regs-preserve/support/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ print_reverse_array:
mov rcx, rsi

next:
; TODO1: uncomment the following two lines
; TODO1: uncomment push rcx and pop rcx.
; Note: pushing rcx also aligns the stack to 16 bytes for printf.
; push rcx
xor rax, rax
mov esi, [rbx + 4*rcx - 4]
Expand All @@ -40,6 +41,7 @@ next:
mov rdi, newline
call printf


; restore preserved register
pop rbx

Expand Down
Empty file.
Loading