Long Mode Alignment
Long Mode Alignment
Quick question,
Do procedures/call destinations need to be 16byte aligned in long mode? For some reason if I don't align 16 before procedure call labels things go very badly wrong... even on an empty function like
MyFunction:
ret
Do procedures/call destinations need to be 16byte aligned in long mode? For some reason if I don't align 16 before procedure call labels things go very badly wrong... even on an empty function like
MyFunction:
ret
- Combuster
- Member

- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Long Mode Alignment
No you don't need to. Something else is wrong with your setup.
Re: Long Mode Alignment
Its bizarre.. it's almost as if theres a bug in FASM, where macro expansion isn't working properly... like the target addresses are going wonky when it assembles over macros... before i had any macros i had no issues, now with macros in the code things have gone pear-shaped
- Combuster
- Member

- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Long Mode Alignment
If you want to blame FASM, prove it. Until then, the compiler is always right.
Re: Long Mode Alignment
Hi,
If it's with macro's, look at the expansion - is it what you would expect? If FASM does not have an option to show you the expanded macro, try assembling and then disassembling to see if the output was as expected.
Cheers,
Adam
If it's with macro's, look at the expansion - is it what you would expect? If FASM does not have an option to show you the expanded macro, try assembling and then disassembling to see if the output was as expected.
Cheers,
Adam
Re: Long Mode Alignment
I agree. Check out the resulting code before you blame the FASM.
But, in case of some rare unknown bug, check to see if you've enabled alignment checking as that would probably cause havoc with unaligned calls/memory operations.
But, in case of some rare unknown bug, check to see if you've enabled alignment checking as that would probably cause havoc with unaligned calls/memory operations.
Website: https://joscor.com
- Combuster
- Member

- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Long Mode Alignment
If its the AC mechanism, it means the AC bit in the flags got set (maybe), AM bit in CR0 got set (very unlikely, even by accident!), and code's running in ring 3 (given the problem description, also unlikely), and the code's doing unaligned accesses.
So I personally wouldn't bet on that.
So I personally wouldn't bet on that.
Re: Long Mode Alignment
Just a guess, might be this could be related. Anyway, this not supposed to cause a problems alone ...johnsa wrote:Quick question,
Do procedures/call destinations need to be 16byte aligned in long mode? For some reason if I don't align 16 before procedure call labels things go very badly wrong... even on an empty function like
MyFunction:
ret
Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 3A: System Programming Guide, Part 1
5.14.2 64-Bit Mode Stack Frame
In legacy mode, the size of an IDT entry (16 bits or 32 bits) determines the size of interrupt-stack-frame pushes. SS:ESP is pushed only on a CPL change. In 64-bit mode, the size of interrupt stack-frame pushes is fixed at eight bytes. This is because only 64-bit mode gates can be referenced. 64-bit mode also pushes SS:RSP unconditionally, rather than only on a CPL change.
Aside from error codes, pushing SS:RSP unconditionally presents operating systems with a consistent interrupt-stackframe size across all interrupts. Interrupt service-routine entry points that handle interrupts generated by the INTn instruction or external INTR# signal can push an additional error code place-holder to maintain consistency.
In legacy mode, the stack pointer may be at any alignment when an interrupt or exception causes a stack frame to be pushed. This causes the stack frame and succeeding pushes done by an interrupt handler to be at arbitrary alignments. In IA-32e mode, the RSP is aligned to a 16-byte boundary before pushing the stack frame. The stack frame itself is aligned on a 16-byte boundary when the interrupt handler is called. The processor can arbitrarily realign the new RSP on interrupts because the previous (possibly unaligned) RSP is unconditionally saved on the newly aligned stack. The previous RSP will be automatically restored by a subsequent IRET.
Aligning the stack permits exception and interrupt frames to be aligned on a 16-byte boundary before interrupts are re-enabled. This allows the stack to be formatted for optimal storage of 16-byte XMM registers, which enables the interrupt handler to use faster 16-byte aligned loads and stores (MOVAPS rather than MOVUPS) to save and restore XMM registers.
Although the RSP alignment is always performed when LMA = 1, it is only of consequence for the kernel-mode case where there is no stack switch or IST used. For a stack switch or IST, the OS would have presumably put suitably aligned RSP values in the TSS.
Re: Long Mode Alignment
Eventually found the issue... normally in FASM macros have to be declared before use.. for some reason it can't pick them up with a forward reference , which it can do with procs/labels/vars etc.. for some reason when i assembled i had one macro which was a forward reference and it didn't complain about it.. i moved it up before the code that uses it (right at the beginning where i define equates etc) and it came right.. its a bit fishy, but it fixed it...
Re: Long Mode Alignment
It's the same, be it the C preprocessor's macros or MASM's macros... Macros usually have to be defined before use, however undefined macros are usually flagged by the compiler/assembler as undefined identifiers.johnsa wrote:Eventually found the issue... normally in FASM macros have to be declared before use.. for some reason it can't pick them up with a forward reference , which it can do with procs/labels/vars etc.. for some reason when i assembled i had one macro which was a forward reference and it didn't complain about it.. i moved it up before the code that uses it (right at the beginning where i define equates etc) and it came right.. its a bit fishy, but it fixed it...
Re: Long Mode Alignment
i can forward declare macros in masm no prob.. do it all the time, its a bit of pain not being able too.. it means i have to have one include with code, and then a helper include with macros at the start..
