Author Topic: Structure alignment  (Read 638 times)

sinsi

  • Member
  • *****
  • Posts: 1072
Structure alignment
« on: December 31, 2017, 10:30:59 AM »
Within a structure, items should be naturally aligned
Code: [Select]
structure1 struct
  v32_1    dd ? ;offset 0
           dd ? ;offset 4, padding
  v64_1    dq ? ;offset 8
structure1 ends

Automatically aligned?
Code: [Select]
structure1 struct 8
  v32_1    dd ? ;offset 0
  v64_1    dq ? ;offset 8
structure1 ends

Should a structure pad the end to align 8?
Code: [Select]
structure2 struct 8
  v64_1    dq ? ;offset 0
  v32_1    dd ? ;offset 8
           dd ?
structure2 ends

Does alignment only matter within a structure?
Code: [Select]
var1  dd ?          ;.data? offset 0
str2  structure2 <> ;.data? offset 4 - str2.v64_1 not aligned to 8 - doesn't matter?
I can walk on water but stagger on beer.

nidud

  • Member
  • *****
  • Posts: 1614
    • https://github.com/nidud/asmc
Re: Structure alignment
« Reply #1 on: December 31, 2017, 11:21:05 AM »
Within a structure, items should be naturally aligned

Padding should be avoided: use the -Zp[n] switch instead.

Quote
Automatically aligned?

Some structures are still byte-aligned so they will be needing some adjustment if the switch above is used.

Quote
Should a structure pad the end to align 8?

No.

Quote
Does alignment only matter within a structure?

Yes: You may see this as an agreement between two parties to ensure the correct offset is used on both ends.

No: The main point of the alignment is speed so the start offset should be aligned.

sinsi

  • Member
  • *****
  • Posts: 1072
Re: Structure alignment
« Reply #2 on: December 31, 2017, 01:40:05 PM »
So memory alignment isn't required but would be more efficient.
Does the -Zp[n] switch override the structure definition?

Would a nested structure need alignment within the main structure?


Thanks for your time.
I can walk on water but stagger on beer.

jj2007

  • Member
  • *****
  • Posts: 8773
  • Assembler is fun ;-)
    • MasmBasic
Re: Structure alignment
« Reply #3 on: December 31, 2017, 07:19:12 PM »
I agree with Nidud that the Zp switch is the way to go. -Zp8 for 64-bit code, to be precise. Everything else is a can of worms.