From 6b217c07bf07e735eac6896cd1e3ba81a600eecc Mon Sep 17 00:00:00 2001 From: alexf05 Date: Fri, 20 Mar 2026 12:10:55 +0000 Subject: [PATCH 1/4] lab-09/reading: Fix 32-bit leftover syntax and typos Update memory-layout and calling-convention reading materials to correctly reflect the 64-bit System V AMD64 ABI. Add the missing `xor rax, rax` before variadic function calls and convert 32-bit stack argument examples to 64-bit register arguments. Fix #141 Signed-off-by: alexf05 --- labs/lab-09/reading/calling-convention.md | 1 + labs/lab-09/reading/memory-layout-c-asm.md | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/labs/lab-09/reading/calling-convention.md b/labs/lab-09/reading/calling-convention.md index fafa9bab7..4d982dc81 100644 --- a/labs/lab-09/reading/calling-convention.md +++ b/labs/lab-09/reading/calling-convention.md @@ -49,6 +49,7 @@ main: mov rdi, fmt mov rsi, text + xor rax, rax call printf pop rbp diff --git a/labs/lab-09/reading/memory-layout-c-asm.md b/labs/lab-09/reading/memory-layout-c-asm.md index 82f45645c..2fea21f85 100644 --- a/labs/lab-09/reading/memory-layout-c-asm.md +++ b/labs/lab-09/reading/memory-layout-c-asm.md @@ -40,13 +40,19 @@ 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 + push rbp + mov rbp, rsp + + mov rdi, strformat + mov rsi, text + xor rax, rax call printf - add esp, 8 + + leave ret ``` From 2e3e935710088c26b6666ee506e3e11c7c14c12d Mon Sep 17 00:00:00 2001 From: alexf05 Date: Fri, 20 Mar 2026 12:11:36 +0000 Subject: [PATCH 2/4] lab-09/tasks: Fix ABI violations and logical bugs in 64-bit ports Correct multiple issues introduced during the x86_64 transition: - max-c-calls: Fix signedness comparison by using `jae` and fix pointer initialization bug in the solution. Revert support file. - regs-preserve: Add required 16-byte stack alignment before calling printf and fix the order of stack restoration in the solution. Fix #141 Signed-off-by: alexf05 --- .../tasks/max-assembly-calls/solution/main.asm | 1 + .../tasks/max-assembly-calls/support/main.asm | 13 +++++++++++-- labs/lab-09/tasks/max-c-calls/solution/max.asm | 4 ++-- labs/lab-09/tasks/max-c-calls/support/max.asm | 10 +++------- labs/lab-09/tasks/regs-preserve/solution/main.asm | 6 +++--- labs/lab-09/tasks/regs-preserve/support/main.asm | 3 +++ .../tasks/stack-frame/support/print_hello.asm | 2 +- 7 files changed, 24 insertions(+), 15 deletions(-) diff --git a/labs/lab-09/tasks/max-assembly-calls/solution/main.asm b/labs/lab-09/tasks/max-assembly-calls/solution/main.asm index ea28cd6b2..707810978 100644 --- a/labs/lab-09/tasks/max-assembly-calls/solution/main.asm +++ b/labs/lab-09/tasks/max-assembly-calls/solution/main.asm @@ -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) diff --git a/labs/lab-09/tasks/max-assembly-calls/support/main.asm b/labs/lab-09/tasks/max-assembly-calls/support/main.asm index d5228f675..707810978 100644 --- a/labs/lab-09/tasks/max-assembly-calls/support/main.asm +++ b/labs/lab-09/tasks/max-assembly-calls/support/main.asm @@ -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 @@ -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) diff --git a/labs/lab-09/tasks/max-c-calls/solution/max.asm b/labs/lab-09/tasks/max-c-calls/solution/max.asm index b0a4fce24..96424f888 100644 --- a/labs/lab-09/tasks/max-c-calls/solution/max.asm +++ b/labs/lab-09/tasks/max-c-calls/solution/max.asm @@ -14,7 +14,7 @@ get_max: ; initialize EAX with the first value as currently known maximum mov eax, [rdi] - mov [rdx], eax + mov dword [rdx], 0 ; initialize RCX as loop counter for remaining elements mov rcx, rsi @@ -23,7 +23,7 @@ get_max: ; loop over remaining array elements compare: cmp eax, [rdi + 4*rcx] - jge check_end + jae check_end ; update maximum and its position mov eax, [rdi + 4*rcx] diff --git a/labs/lab-09/tasks/max-c-calls/support/max.asm b/labs/lab-09/tasks/max-c-calls/support/max.asm index 1794c71ba..1a14a0d68 100644 --- a/labs/lab-09/tasks/max-c-calls/support/max.asm +++ b/labs/lab-09/tasks/max-c-calls/support/max.asm @@ -6,7 +6,7 @@ global get_max ; RDI = array pointer -; RSI = array length +; RSI = array lengthget_max get_max: push rbp mov rbp, rsp @@ -18,16 +18,12 @@ get_max: mov rcx, rsi dec rcx - ; loop over remaining array elements compare: cmp eax, [rdi + 4*rcx] - jge check_end + jae check_end ; <-- Keep the unsigned fix here! mov eax, [rdi + 4*rcx] check_end: loop compare - ; result stored in RAX - leave - ret - + ret \ No newline at end of file diff --git a/labs/lab-09/tasks/regs-preserve/solution/main.asm b/labs/lab-09/tasks/regs-preserve/solution/main.asm index be5df9353..83c18dd9b 100644 --- a/labs/lab-09/tasks/regs-preserve/solution/main.asm +++ b/labs/lab-09/tasks/regs-preserve/solution/main.asm @@ -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 diff --git a/labs/lab-09/tasks/regs-preserve/support/main.asm b/labs/lab-09/tasks/regs-preserve/support/main.asm index 1f5b8cfe8..b3a989e9f 100644 --- a/labs/lab-09/tasks/regs-preserve/support/main.asm +++ b/labs/lab-09/tasks/regs-preserve/support/main.asm @@ -36,10 +36,13 @@ next: ; pop rcx loop next + sub rsp, 8 xor rax, rax mov rdi, newline call printf + add rsp, 8 + ; restore preserved register pop rbx diff --git a/labs/lab-09/tasks/stack-frame/support/print_hello.asm b/labs/lab-09/tasks/stack-frame/support/print_hello.asm index ecc3d902b..86b5de1af 100644 --- a/labs/lab-09/tasks/stack-frame/support/print_hello.asm +++ b/labs/lab-09/tasks/stack-frame/support/print_hello.asm @@ -17,4 +17,4 @@ print_hello: call printf leave - ret + ret \ No newline at end of file From d8dfaaf4e6b5840c32116ae0a949cec64cf2b7c5 Mon Sep 17 00:00:00 2001 From: alexf05 Date: Fri, 17 Apr 2026 05:21:19 +0000 Subject: [PATCH 3/4] Modified max-c-calls-x86 to x86 Resolved seg fault in reg-preserve Another minor changes Signed-off-by: alexf05 --- labs/lab-09/reading/calling-convention.md | 1 - .../tasks/max-c-calls-x86/solution/max.asm | 43 ++++++++++++------- .../tasks/max-c-calls-x86/support/max.asm | 35 +++++++++------ .../lab-09/tasks/max-c-calls/solution/max.asm | 4 +- labs/lab-09/tasks/max-c-calls/support/max.asm | 10 +++-- .../tasks/regs-preserve/support/main.asm | 5 +-- .../tasks/regs-preserve/tests/results.txt | 0 .../tasks/stack-frame/support/print_hello.asm | 2 +- 8 files changed, 61 insertions(+), 39 deletions(-) create mode 100644 labs/lab-09/tasks/regs-preserve/tests/results.txt diff --git a/labs/lab-09/reading/calling-convention.md b/labs/lab-09/reading/calling-convention.md index 4d982dc81..31d57fd85 100644 --- a/labs/lab-09/reading/calling-convention.md +++ b/labs/lab-09/reading/calling-convention.md @@ -53,7 +53,6 @@ main: call printf pop rbp - xor rax, rax ret ``` diff --git a/labs/lab-09/tasks/max-c-calls-x86/solution/max.asm b/labs/lab-09/tasks/max-c-calls-x86/solution/max.asm index b0a4fce24..b40eda8b7 100644 --- a/labs/lab-09/tasks/max-c-calls-x86/solution/max.asm +++ b/labs/lab-09/tasks/max-c-calls-x86/solution/max.asm @@ -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 diff --git a/labs/lab-09/tasks/max-c-calls-x86/support/max.asm b/labs/lab-09/tasks/max-c-calls-x86/support/max.asm index 1794c71ba..a577d66de 100644 --- a/labs/lab-09/tasks/max-c-calls-x86/support/max.asm +++ b/labs/lab-09/tasks/max-c-calls-x86/support/max.asm @@ -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 - diff --git a/labs/lab-09/tasks/max-c-calls/solution/max.asm b/labs/lab-09/tasks/max-c-calls/solution/max.asm index 96424f888..b0a4fce24 100644 --- a/labs/lab-09/tasks/max-c-calls/solution/max.asm +++ b/labs/lab-09/tasks/max-c-calls/solution/max.asm @@ -14,7 +14,7 @@ get_max: ; initialize EAX with the first value as currently known maximum mov eax, [rdi] - mov dword [rdx], 0 + mov [rdx], eax ; initialize RCX as loop counter for remaining elements mov rcx, rsi @@ -23,7 +23,7 @@ get_max: ; loop over remaining array elements compare: cmp eax, [rdi + 4*rcx] - jae check_end + jge check_end ; update maximum and its position mov eax, [rdi + 4*rcx] diff --git a/labs/lab-09/tasks/max-c-calls/support/max.asm b/labs/lab-09/tasks/max-c-calls/support/max.asm index 1a14a0d68..1794c71ba 100644 --- a/labs/lab-09/tasks/max-c-calls/support/max.asm +++ b/labs/lab-09/tasks/max-c-calls/support/max.asm @@ -6,7 +6,7 @@ global get_max ; RDI = array pointer -; RSI = array lengthget_max +; RSI = array length get_max: push rbp mov rbp, rsp @@ -18,12 +18,16 @@ get_max: mov rcx, rsi dec rcx + ; loop over remaining array elements compare: cmp eax, [rdi + 4*rcx] - jae check_end ; <-- Keep the unsigned fix here! + jge check_end mov eax, [rdi + 4*rcx] check_end: loop compare + ; result stored in RAX + leave - ret \ No newline at end of file + ret + diff --git a/labs/lab-09/tasks/regs-preserve/support/main.asm b/labs/lab-09/tasks/regs-preserve/support/main.asm index b3a989e9f..a79f17199 100644 --- a/labs/lab-09/tasks/regs-preserve/support/main.asm +++ b/labs/lab-09/tasks/regs-preserve/support/main.asm @@ -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] @@ -36,12 +37,10 @@ next: ; pop rcx loop next - sub rsp, 8 xor rax, rax mov rdi, newline call printf - add rsp, 8 ; restore preserved register pop rbx diff --git a/labs/lab-09/tasks/regs-preserve/tests/results.txt b/labs/lab-09/tasks/regs-preserve/tests/results.txt new file mode 100644 index 000000000..e69de29bb diff --git a/labs/lab-09/tasks/stack-frame/support/print_hello.asm b/labs/lab-09/tasks/stack-frame/support/print_hello.asm index 86b5de1af..ecc3d902b 100644 --- a/labs/lab-09/tasks/stack-frame/support/print_hello.asm +++ b/labs/lab-09/tasks/stack-frame/support/print_hello.asm @@ -17,4 +17,4 @@ print_hello: call printf leave - ret \ No newline at end of file + ret From 72527d115e4ab3d94b94b51122ce0fd64f3cf9c1 Mon Sep 17 00:00:00 2001 From: alexf05 Date: Fri, 17 Apr 2026 05:28:57 +0000 Subject: [PATCH 4/4] fixing trailiing whitespace for the reading materials Signed-off-by: alexf05 --- labs/lab-09/reading/memory-layout-c-asm.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/labs/lab-09/reading/memory-layout-c-asm.md b/labs/lab-09/reading/memory-layout-c-asm.md index 2fea21f85..a8b3c4c8f 100644 --- a/labs/lab-09/reading/memory-layout-c-asm.md +++ b/labs/lab-09/reading/memory-layout-c-asm.md @@ -44,16 +44,16 @@ section .text global main main: - push rbp - mov rbp, rsp + push rbp + mov rbp, rsp - mov rdi, strformat - mov rsi, text - xor rax, rax - call printf + mov rdi, strformat + mov rsi, text + xor rax, rax + call printf - leave - ret + leave + ret ``` Note that the procedure is declared as global and is called `main` - the starting point of any C program.