User Tools

Site Tools


blog:pushbx:2026:0227_alignment_calculation_in_three_instructions

Alignment calculation in three instructions

Found in a 2025-08-29 changeset of the ecm fork of WarpLink:

    mov bx, [alignment_table + bx]	; bx = alignment mask
	and ax, bx
	neg ax
	and ax, bx

and neg and. That's odd. I assume it works because I would have worked it out at the time but it's not intuitive.

The first and is obvious enough: Calculate how far past the aligned boundary we are. Zero means exactly aligned, 1 means 1 byte past alignment, etc.

Then neg: Negative value of how far past the aligned boundary. Zero if exactly aligned.

Then the second and: -1 (0FFFFh) results in ax = alignment mask. -2 (0FFFEh) results in mask clr 1 (one below the mask). -3 (0FFFDh) results in mask clr 2 (two below the mask).

You could leave a comment if you were logged in.
blog/pushbx/2026/0227_alignment_calculation_in_three_instructions.txt · Last modified: 2026-02-27 17:11:42 +0100 Feb Fri by ecm