User Tools

Site Tools


aligning_20structures

Differences

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

Link to this comparison view

Next revision
Previous revision
aligning_20structures [2018/03/31 13:19] – external edit 127.0.0.1aligning_20structures [2024/01/05 00:22] (current) – external edit 127.0.0.1
Line 4: Line 4:
 ==== Method 1 ==== ==== Method 1 ====
 \\  This involves two steps, firstly adding some dummy //padding// to the end of the structure so that there is room to move its data (if necessary), and secondly to adjust the structure's data address. For example to align a structure on a **QWORD** boundary (a multiple-of-eight address) you can do this:\\  \\  This involves two steps, firstly adding some dummy //padding// to the end of the structure so that there is room to move its data (if necessary), and secondly to adjust the structure's data address. For example to align a structure on a **QWORD** boundary (a multiple-of-eight address) you can do this:\\ 
 +<code bb4w>
         DIM mystruct{member1, member2, etc, padding%}         DIM mystruct{member1, member2, etc, padding%}
-        !(^mystruct{}+4) = (mystruct{}+4) AND -8+        PTR(mystruct{}) = PTR(mystruct{}) AND -8 
 +</code>
 When the structure is declared, a dummy member **padding%** is added at the very end; this allows the structure to be moved up in memory by 4 bytes, if necessary, without corrupting subsequent memory contents; //it is vitally important that this member is never written//! The second line adjusts the address of the structure so that it is a multiple of eight.\\ \\  If you want to align the structure at a multiple-of-16 address the padding needs to be 12 bytes in size, most easily arranged by making it an array:\\  When the structure is declared, a dummy member **padding%** is added at the very end; this allows the structure to be moved up in memory by 4 bytes, if necessary, without corrupting subsequent memory contents; //it is vitally important that this member is never written//! The second line adjusts the address of the structure so that it is a multiple of eight.\\ \\  If you want to align the structure at a multiple-of-16 address the padding needs to be 12 bytes in size, most easily arranged by making it an array:\\ 
 +<code bb4w>
         DIM mystruct{member1, member2, etc, padding%(2)}         DIM mystruct{member1, member2, etc, padding%(2)}
-        !(^mystruct{}+4) = (mystruct{}+12) AND -16+        PTR(mystruct{}) = PTR(mystruct{}) AND -16 
 +</code>
 \\  \\ 
 ==== Method 2 ==== ==== Method 2 ====
 \\  With this method there is no need to add any padding to the structure, but the original structure's storage is wasted so it is less efficient in memory use. To align a structure on a **QWORD** boundary (a multiple-of-eight address):\\  \\  With this method there is no need to add any padding to the structure, but the original structure's storage is wasted so it is less efficient in memory use. To align a structure on a **QWORD** boundary (a multiple-of-eight address):\\ 
 +<code bb4w>
         DIM mystruct{member1, member2, etc}         DIM mystruct{member1, member2, etc}
         DIM newdata% DIM(mystruct{})+6         DIM newdata% DIM(mystruct{})+6
-        !(^mystruct{}+4) = (newdata%+7) AND -8+        PTR(mystruct{}) = (newdata%+7) AND -8 
 +</code>
 Similarly to align it on a multiple-of-16 address:\\  Similarly to align it on a multiple-of-16 address:\\ 
 +<code bb4w>
         DIM mystruct{member1, member2, etc}         DIM mystruct{member1, member2, etc}
         DIM newdata% DIM(mystruct{})+14         DIM newdata% DIM(mystruct{})+14
-        !(^mystruct{}+4) = (newdata%+15) AND -16+        PTR(mystruct{}) = (newdata%+15) AND -16 
 +</code>
 Note that you must declare the structure using this method **only once** and not repeat the above code (for example in a loop).\\ \\  If declaring a structure for local use in a procedure or function, use **DIM LOCAL**:\\  Note that you must declare the structure using this method **only once** and not repeat the above code (for example in a loop).\\ \\  If declaring a structure for local use in a procedure or function, use **DIM LOCAL**:\\ 
 +<code bb4w>
         LOCAL mystruct{}, newdata%         LOCAL mystruct{}, newdata%
         DIM mystruct{member1, member2, etc}         DIM mystruct{member1, member2, etc}
         DIM newdata% LOCAL DIM(mystruct{})+6         DIM newdata% LOCAL DIM(mystruct{})+6
-        !(^mystruct{}+4) = (newdata%+7) AND -8+        PTR(mystruct{}) = (newdata%+7) AND -8 
 +</code>
aligning_20structures.1522502344.txt.gz · Last modified: 2024/01/05 00:18 (external edit)