flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Aligned structures using "struc"

Author
Thread Post new topic Reply to topic
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
(repost from here)

recently i had funny alignment problem with FASM.

I had structure, let's say STRUC1
Code:
struc STRUC1 x,y,z{
.x dd x+0
.y dd y+0
.z db z+0
}    


I wanted to align it
Code:
struc STRUC1 x,y,z{
.x dd x+0
.y dd y+0
.z db z+0
align  ;<----
}    


This worked nicely, structure size was aligned. Later, I "overloaded" it with STRUC2:
Code:
struc STRUC2 x,y,z,xx,yy,zz{
. STRUC1 x,y,z
.xx dd xx+0
.yy dd yy+0
.zz db zz+0
align 4
}    


This seemed to work too, until once i found that some data of intialized STRUC2 structure were shifted by two bytes against where they should be. It took me nearly hour and half to realize what is happening:

the initialized structure was being declared on unaligned offset. so the resulting declaration (after preprocessing of struct) looked like:
Code:
; jano  STRUC2  1,2,3,4,5,6
jano:
.x dd 1
.y dd 2
.z db 3
align 4
.xx dd 4
.yy dd 5
.zz db 6
align 4    

first members of structure were declared unaligned, until first align was reached.

So for every structure with aligned members in FASM, also align the structure itself, like this:
Code:
struc STRUC1 x,y,z{
align 4
.:
.x dd x+0
.y dd y+0
.z db z+0
align 4
}    

but of course, it's always better to use standard STRUCT macros.
Post 03 Feb 2007, 20:39
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.