Skip to content

Lab 09 fix#160

Open
alexf05 wants to merge 3 commits intomainfrom
lab-09-fix
Open

Lab 09 fix#160
alexf05 wants to merge 3 commits intomainfrom
lab-09-fix

Conversation

@alexf05
Copy link
Copy Markdown

@alexf05 alexf05 commented Mar 26, 2026

Prerequisite Checklist

  • Read the contribution guidelines regarding submitting new changes to the project;
  • Tested your changes against relevant architectures and platforms;
  • Updated relevant documentation (if needed).

Description of changes

This PR resolves the remaining bugs, ABI violations, and typos from the x86_64 transition in lab-09.

Specific fixes include:

  • Reading Materials: Corrected 64-bit calling conventions in the markdown files (e.g., clearing rax for variadic functions like printf and updating 32-bit stack argument examples to 64-bit register arguments).
  • max-c-calls: Fixed a logical bug by changing the signed comparison (jge) to unsigned (jae). Fixed pointer initialization in the solution.
  • regs-preserve: Added the required 16-byte stack alignment (sub rsp, 8) to the support code before calling printf to prevent immediate segfaults. Corrected the stack restoration order (pop rbx vs add rsp, 8) in the solution.

Question for Reviewer (@teodutu): I noticed that the max-c-calls-x86 task currently contains 64-bit code instead of 32-bit, and the max-assembly-calls-x86 task is missing add esp, X stack cleanups for the cdecl calling convention. Should I convert these files back to proper 32-bit assembly in a follow-up commit, or are these x86 legacy folders planned for deletion?

Fix #141

@alexf05 alexf05 requested a review from teodutu March 26, 2026 08:07
alexf05 added 3 commits March 26, 2026 17:44
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 <alexfechet16@gmail.com>
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 <alexfechet16@gmail.com>
Signed-off-by: alexf05 <alexfechet16@gmail.com>
Copy link
Copy Markdown

@teodutu teodutu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right about max-c-calls-x86. Please convert it to x86. As for cleaning the stack in max-assembly-calls-x86, that's not needed because leave already does that by settign rsp = rbp, so leave this as it is.

leave
ret

ret No newline at end of file
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ret
ret


mov rdi, fmt
mov rsi, text
xor rax, rax
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain why clearing al is needed for variadic functions. And I'd say you should only clear al itself as the System-V ABI requires to be precise.


; RDI = array pointer
; RSI = array length
; RSI = array lengthget_max
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
; RSI = array lengthget_max
; RSI = array length

A wrong copy-paste I guess.

mov rcx, rsi

next:
; TODO1: uncomment the following two lines
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which two? Only one is commented. Also add a comment to remember to align the stack to 16 bytes for printf.


leave
ret
ret No newline at end of file
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ret
ret

compare:
cmp eax, [rdi + 4*rcx]
jge check_end
jae check_end ; <-- Keep the unsigned fix here!
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
jae check_end ; <-- Keep the unsigned fix here!
jae check_end

Comment on lines +39 to +45
sub rsp, 8
xor rax, rax
mov rdi, newline
call printf

add rsp, 8

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sub rsp, 8
xor rax, rax
mov rdi, newline
call printf
add rsp, 8
xor rax, rax
mov rdi, newline
call printf

Adding these lines here actually causes a seg fault.

@teodutu
Copy link
Copy Markdown

teodutu commented Mar 30, 2026

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

labs/lab-09: Check x86_64 version

2 participants