Skip to content

asm-6: align stack to 16-byte boundary before C function calls#77

Merged
0xAX merged 1 commit intomasterfrom
float-fix
Mar 8, 2026
Merged

asm-6: align stack to 16-byte boundary before C function calls#77
0xAX merged 1 commit intomasterfrom
float-fix

Conversation

@0xAX
Copy link
Owner

@0xAX 0xAX commented Mar 7, 2026

Description

The System V AMD64 ABI requires the stack to be 16-byte aligned before calling any C library function. Without proper alignment, C functions like strtod() and printf() can segfault on systems that strictly enforce ABI requirements (e.g., AMD EPYC processors).

This fix adds a single instruction at _start to force stack alignment. This ensures RSP % 16 == 0 before any call instruction, which after pushing the 8-byte return address results in RSP % 16 == 8 inside the called function - exactly what the ABI specifies.

Related issues

Related issue - #64

@0xAX 0xAX requested a review from klaudiagrz as a code owner March 7, 2026 13:46
@0xAX 0xAX mentioned this pull request Mar 7, 2026
1 task
Copy link
Collaborator

@klaudiagrz klaudiagrz left a comment

Choose a reason for hiding this comment

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

One small comment

content/asm_6.md Outdated
```

The definition of the `.text` section starts from referencing the external functions: `strtod` and `printf`. As mentioned above, these functions are part of the C standard library, and we will use them to simplify our program. After defining the entry point, we immediately jump to the `_read_first_float_vector` label. This is where our code starts.
The definition of the `.text` section starts from referencing the external functions: `strtod` and `printf`. As mentioned above, these functions are part of the C standard library, and we will use them to simplify our program. After defining the entry point, we align the stack to 16 bytes boundary how System V AMD64 ABI requires before calling C functions and immediately jump to the `_read_first_float_vector` label. This is where our code starts.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
The definition of the `.text` section starts from referencing the external functions: `strtod` and `printf`. As mentioned above, these functions are part of the C standard library, and we will use them to simplify our program. After defining the entry point, we align the stack to 16 bytes boundary how System V AMD64 ABI requires before calling C functions and immediately jump to the `_read_first_float_vector` label. This is where our code starts.
The definition of the `.text` section starts from referencing the external functions: `strtod` and `printf`. As mentioned above, these functions are part of the C standard library, and we will use them to simplify our program. After defining the entry point, we align the stack to 16 bytes boundary (as System V AMD64 ABI requires before calling C functions) and immediately jump to the `_read_first_float_vector` label. This is where our code starts.

The System V AMD64 ABI requires the stack to be 16-byte aligned
before calling any C library function. Without proper alignment,
C functions like strtod() and printf() can segfault on systems
that strictly enforce ABI requirements (e.g., AMD EPYC processors).

This fix adds a single instruction at _start to force stack alignment.
This ensures RSP % 16 == 0 before any call instruction, which after
pushing the 8-byte return address results in RSP % 16 == 8 inside
the called function - exactly what the ABI specifies.

Signed-off-by: Alexander Kuleshov <kuleshovmail@gmail.com>
@0xAX 0xAX merged commit 575898c into master Mar 8, 2026
1 check passed
@0xAX 0xAX deleted the float-fix branch March 8, 2026 10:40
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.

2 participants