User Tools

Site Tools


blog:pushbx:2026:0227_alignment_calculation_in_three_instructions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

blog:pushbx:2026:0227_alignment_calculation_in_three_instructions [2026-02-27 17:11:42 +0100 Feb Fri] (current)
ecm created
Line 1: Line 1:
 +====== Alignment calculation in three instructions ======
 +
 +Found [[https://hg.pushbx.org/ecm/warplink/rev/319bf908e2e5#l2.101|in a 2025-08-29 changeset]] of the ecm fork of WarpLink:
 +
 +<code>    mov bx, [alignment_table + bx] ; bx = alignment mask
 + and ax, bx
 + neg ax
 + and ax, bx</code>
 +
 +''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).
 +
 +{{tag>warplink codespam}}
 +
 +
 +~~DISCUSSION~~
  
blog/pushbx/2026/0227_alignment_calculation_in_three_instructions.txt ยท Last modified: 2026-02-27 17:11:42 +0100 Feb Fri by ecm