flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [solved] fasm1 separate structs to aligned & misaligned form

Author
Thread Post new topic Reply to topic
ProMiNick



Joined: 24 Mar 2012
Posts: 802
Location: Russian Federation, Sochi
ProMiNick 31 Jan 2019, 13:58
I patch struct macro a little:
Code:
macro strong_struct name
 { virtual at 0
   define @struct
   field@struct equ name
   match child parent, name \{ restore field@struct
                               field@struct equ child,fields@\#parent \}
   sub@struct equ
   struc VOID_8  [val:?] \{ \common define field@struct .,VOID_8,<val> \}
   struc VOID_16 [val:?] \{ \common \local anonymous
                          define field@struct anonymous,align,sizeof.VOID_16,.,VOID_16,<val> \}
   struc VOID_32 [val:?] \{ \common \local anonymous
                          define field@struct anonymous,align,sizeof.VOID_32,.,VOID_32,<val> \}
   struc VOID_64 [val:?] \{ \common \local anonymous
                          define field@struct anonymous,align,sizeof.VOID_64,.,VOID_64,<val> \}
   struc VOID    [val:?] \{ \common \local anonymous
                          define field@struct anonymous,align,sizeof.VOID,.,VOID,<val> \}
   struc PTR     [val:?] \{ \common \local anonymous
                          define field@struct anonymous,align,sizeof.VOID,.,PTR,<val> \}
   struc ptr     [val:?] \{ \common \local anonymous
                          define field@struct anonymous,align,sizeof.VOID,.,ptr,<val> \}
   struc HNDL    [val:?] \{ \common \local anonymous
                          define field@struct anonymous,align,sizeof.VOID,.,HNDL,<val> \}

   struc TCHAR   [val:?] \{ \common \local anonymous
                          define field@struct anonymous,align,sizeof.TCHAR,.,TCHAR,<val> \}
   struc ACHAR   [val:?] \{ \common define field@struct .,ACHAR,<val> \}
   struc WCHAR   [val:?] \{ \common \local anonymous
                          define field@struct anonymous,align,sizeof.TCHAR,.,WCHAR,<val> \}

   macro VOID_8  [val:?] \{ \common \local anonymous
                          define field@struct anonymous,db,<val> \}
   macro VOID_16  [val:?] \{ \common \local anonymous1,anonymous2
                          define field@struct anonymous1,align,sizeof.VOID_16,anonymous2,VOID_16,<val> \}
   macro VOID_32  [val:?] \{ \common \local anonymous1,anonymous2
                          define field@struct anonymous1,align,sizeof.VOID_32,anonymous2,VOID_32,<val> \}
   macro VOID_64  [val:?] \{ \common \local anonymous1,anonymous2
                          define field@struct anonymous1,align,sizeof.VOID_64,anonymous2,VOID_64,<val> \}
   macro VOID     [val:?] \{ \common \local anonymous1,anonymous2
                          define field@struct anonymous1,align,sizeof.VOID,anonymous2,VOID,<val> \}
   macro PTR      [val:?] \{ \common \local anonymous1,anonymous2
                          define field@struct anonymous1,align,sizeof.VOID,anonymous2,PTR,<val> \}
   macro ptr      [val:?] \{ \common \local anonymous1,anonymous2
                          define field@struct anonymous1,align,sizeof.VOID,anonymous2,ptr,<val> \}
   macro HNDL     [val:?] \{ \common \local anonymous1,anonymous2
                          define field@struct anonymous1,align,sizeof.VOID,anonymous2,HNDL,<val> \}
   macro align    val:sizeof.VOID   \{ \local anonymous
                          define field@struct anonymous,align,val \}
   macro union \{ field@struct equ ,union,<
                  sub@struct equ union \}
   macro struct \{ field@struct equ ,substruct,<
                  sub@struct equ substruct \} }

macro custom_struct name
 { virtual at 0
   define @struct
   field@struct equ name
   match child parent, name \{ restore field@struct
                               field@struct equ child,fields@\#parent \}
   sub@struct equ
   struc VOID_8  [val:?] \{ \common define field@struct .,VOID_8,<val> \}
   struc VOID_16 [val:?] \{ \common define field@struct .,VOID_16,<val> \}
   struc VOID_32 [val:?] \{ \common define field@struct .,VOID_32,<val> \}
   struc VOID_64 [val:?] \{ \common define field@struct .,VOID_64,<val> \}
   struc VOID    [val:?] \{ \common define field@struct .,VOID,<val> \}
   struc PTR     [val:?] \{ \common define field@struct .,PTR,<val> \}
   struc ptr     [val:?] \{ \common define field@struct .,ptr,<val> \}
   struc HNDL    [val:?] \{ \common define field@struct .,HNDL,<val> \}

   struc TCHAR   [val:?] \{ \common define field@struct .,TCHAR,<val> \}
   struc ACHAR   [val:?] \{ \common define field@struct .,ACHAR,<val> \}
   struc WCHAR   [val:?] \{ \common define field@struct .,WCHAR,<val> \}

   macro VOID_8  [val:?] \{ \common \local anonymous
                          define field@struct anonymous,db,<val> \}
   macro VOID_16  [val:?] \{ \common \local anonymous
                          define field@struct anonymous,VOID_16,<val> \}
   macro VOID_32  [val:?] \{ \common \local anonymous
                          define field@struct anonymous,VOID_32,<val> \}
   macro VOID_64  [val:?] \{ \common \local anonymous
                          define field@struct anonymous,VOID_64,<val> \}
   macro VOID     [val:?] \{ \common \local anonymous
                          define field@struct anonymous,VOID,<val> \}
   macro PTR      [val:?] \{ \common \local anonymous
                          define field@struct anonymous,PTR,<val> \}
   macro ptr      [val:?] \{ \common \local anonymous
                          define field@struct anonymous,ptr,<val> \}
   macro HNDL     [val:?] \{ \common \local anonymous
                          define field@struct anonymous,HNDL,<val> \}
   macro align    val:sizeof.VOID   \{ \local anonymous
                          define field@struct anonymous,align,val \}
   macro union \{ field@struct equ ,union,<
                  sub@struct equ union \}
   macro struct \{ field@struct equ ,substruct,<
                  sub@struct equ substruct \} }

macro ends
 { match , sub@struct \{ restruc VOID_8,VOID_16,VOID_32,VOID_64,VOID,PTR,ptr,HNDL,TCHAR,ACHAR,WCHAR
                         purge VOID_8,VOID_16,VOID_32,VOID_64,VOID,PTR,ptr,HNDL
                         purge union,struct,align
                         irpv fields,field@struct \\{ restore field@struct
                                                      \\common define fields@struct fields \\}
                         match name tail,fields@struct, \\{ if $
                                                            display 'Error: definition of ',\\`name,' contains illegal instructions.',0Dh,0Ah
                                                            err
                                                            end if \\}
                         match name=,fields,fields@struct \\{ restore @struct
                                                              make@struct name,fields
                                                              define fields@\\#name fields \\}
                         end virtual \}
   match any, sub@struct \{ tmp@struct equ field@struct
                            restore field@struct
                            field@struct equ tmp@struct> \}
   restore sub@struct }

macro strong_ends
 { align
   ends }    
next lines remain untouched.

voids declared... for example for win32a:
Code:
; Win32 programming headers (ASCII)
x86.signbit     = (1 shl 31)
sizeof.PTR      = 4
sizeof.ptr      = 4
sizeof.HNDL     = 4
sizeof.VOID     = 4
sizeof.VOID_8   = 1
sizeof.VOID_16  = 2
sizeof.VOID_32  = 4
sizeof.VOID_64  = 8
sizeof.TCHAR    = 1
sizeof.ACHAR    = 1
sizeof.WCHAR    = 2

struc PTR     [val:?] { common . dd val }
macro PTR     [val:?] { common   dd val }
struc ptr     [val:?] { common . dd val }
macro ptr     [val:?] { common   dd val }

struc HNDL    [val:?] { common . dd val }
macro HNDL    [val:?] { common   dd val }

struc VOID    [val:?] { common . dd val }
macro VOID    [val:?] { common   dd val }

struc VOID_8  [val:?] { common . db val }
macro VOID_8  [val:?] { common   db val }

struc VOID_16 [val:?] { common . dw val }
macro VOID_16 [val:?] { common   dw val }

struc VOID_32 [val:?] { common . dd val }
macro VOID_32 [val:?] { common   dd val }

struc VOID_64 [val:?] { common . dq val }
macro VOID_64 [val:?] { common   dq val }

struc TCHAR [val:?] { common . db val }
struc ACHAR [val:?] { common . db val }
struc WCHAR [val:?] { common . du val }

include 'macro/align.inc'
include 'macro/struct.inc'
include 'os/windows/macro/x86/proc32.inc'
include 'os/windows/macro/x86/com32.inc'
include 'os/windows/macro/x86/import.inc'
include 'os/windows/macro/export.inc'
include 'os/windows/macro/resource.inc'


include 'os/windows/equates/kernel32.inc'
include 'os/windows/equates/user32.inc'
include 'os/windows/equates/gdi32.inc'
include 'os/windows/equates/comctl32.inc'
include 'os/windows/equates/comdlg32.inc'
include 'os/windows/equates/shell32.inc'
include 'os/windows/equates/wsock32.inc'

include 'os/windows/structures/kernel32.inc'
include 'os/windows/structures/user32.inc'
include 'os/windows/structures/gdi32.inc'
include 'os/windows/structures/comctl32.inc'
include 'os/windows/structures/comdlg32.inc'
include 'os/windows/structures/shell32.inc'
include 'os/windows/structures/wsock32.inc'

macro api [name] { if used name
                    label name dword at name#A
                   end if }     


before defining structs macro I override align as:
Code:
struc align val:sizeof.PTR { if val
                               align val
                             end if}
macro align val:sizeof.PTR { if val
                               align val
                             end if}    


I changed structures too to their crossbit form.
compilation is successful, but.

params that goes to struct are shifted.
in strong_struct I internally passed aligments like param definitions.
if I define
wc WNDCLASS 0,-1,$30303030,...
I got
align 0 - dos nothing
dd -1
align $30303030
instead of
dd 0,-1,$30303030,...

Why this shift happend? I naming alignments ananymous like it was with reserving data directives in original struct macro.


I make wrong assumption that struct(original) passes parameters to named fields only... but it is not true:
struct A
db 4 dup ?
B dd 3
ends

A 67
67 not loaded to parameter B of struct A it loaded to 4 dup of db place.

_________________
I don`t like to refer by "you" to one person.
My soul requires acronim "thou" instead.


Last edited by ProMiNick on 01 Feb 2019, 08:41; edited 1 time in total
Post 31 Jan 2019, 13:58
View user's profile Send private message Send e-mail Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 802
Location: Russian Federation, Sochi
ProMiNick 01 Feb 2019, 08:28
why align isn`t allowed by default in virtuals relative to reg?
and needed override to support this:
Code:
macro align boundary:sizeof.PTR,value:? { db (boundary-1)-(($-$$)+boundary-1) mod boundary dup value }    


use case:
Code:
virtual at eax
  db 1,2
  align 8
  A dd 0
end virtual

db A-eax; we got 08 in output, and no errror that section is not aligned    


so allign.inc (it is crossbit) added and it precides struct.inc in list of includes:
Code:
macro align boundary:sizeof.PTR,value:? { db (boundary-1)-(($-$$)+boundary-1) mod boundary dup value }

struc align boundary:sizeof.PTR,value:? { db (boundary-1)-(($-$$)+boundary-1) mod boundary dup value }


struc PTR.aligned     [val:?] { common align
                                       . PTR val }
macro PTR.aligned     [val:?] { common align
                                         PTR val }
struc ptr.aligned     [val:?] { common align
                                       . ptr val }
macro ptr.aligned     [val:?] { common align
                                         ptr val }

struc HNDL.aligned    [val:?] { common align
                                       . HNDL val }
macro HNDL.aligned    [val:?] { common align
                                         HNDL val }

struc VOID.aligned    [val:?] { common align
                                       . VOID val }
macro VOID.aligned    [val:?] { common align
                                         VOID val }

struc VOID_8.aligned  [val:?] { common . db val }
macro VOID_8.aligned  [val:?] { common   db val }

struc VOID_16.aligned [val:?] { common align sizeof.VOID_16
                                       . VOID_16 val }
macro VOID_16.aligned [val:?] { common align sizeof.VOID_16
                                         VOID_16 val }

struc VOID_32.aligned [val:?] { common align sizeof.VOID_32
                                       . VOID_32 val }
macro VOID_32.aligned [val:?] { common align sizeof.VOID_32
                                         VOID_32 val }

struc VOID_64.aligned [val:?] { common align sizeof.VOID_64
                                       . VOID_64 val }
macro VOID_64.aligned [val:?] { common align sizeof.VOID_64
                                         VOID_64 val }

struc TCHAR.aligned [val:?] { common align sizeof.TCHAR
                                       . TCHAR val }
struc WCHAR.aligned [val:?] { common align sizeof.WCHAR
                                       . WCHAR val }      


strong_struct changed to this:
Code:
macro strong_struct name
 { virtual at 0
   define @struct
   field@struct equ name
   match child parent, name \{ restore field@struct
                               field@struct equ child,fields@\#parent \}
   sub@struct equ
   struc VOID_8  [val:?] \{ \common define field@struct .,VOID_8,<val> \}
   struc VOID_16 [val:?] \{ \common define field@struct .,VOID_16.aligned,<val> \}
   struc VOID_32 [val:?] \{ \common define field@struct .,VOID_32.aligned,<val> \}
   struc VOID_64 [val:?] \{ \common define field@struct .,VOID_64.aligned,<val> \}
   struc VOID    [val:?] \{ \common define field@struct .,VOID.aligned,<val> \}
   struc PTR     [val:?] \{ \common define field@struct .,PTR.aligned,<val> \}
   struc ptr     [val:?] \{ \common define field@struct .,ptr.aligned,<val> \}
   struc HNDL    [val:?] \{ \common define field@struct .,HNDL.aligned,<val> \}

   struc TCHAR   [val:?] \{ \common define field@struct .,TCHAR.aligned,<val> \}
   struc ACHAR   [val:?] \{ \common define field@struct .,ACHAR,<val> \}
   struc WCHAR   [val:?] \{ \common define field@struct .,WCHAR.aligned,<val> \}

   macro VOID_8  [val:?] \{ \common \local anonymous
                          define field@struct anonymous,VOID_8,<val> \}
   macro VOID_16  [val:?] \{ \common \local anonymous
                          define field@struct anonymous,VOID_16.aligned,<val> \}
   macro VOID_32  [val:?] \{ \common \local anonymous
                          define field@struct anonymous,VOID_32.aligned,<val> \}
   macro VOID_64  [val:?] \{ \common \local anonymous
                          define field@struct anonymous,VOID_64.aligned,<val> \}
   macro VOID     [val:?] \{ \common \local anonymous
                          define field@struct anonymous,VOID.aligned,<val> \}
   macro PTR      [val:?] \{ \common \local anonymous
                          define field@struct anonymous,PTR.aligned,<val> \}
   macro ptr      [val:?] \{ \common \local anonymous
                          define field@struct anonymous,ptr.aligned,<val> \}
   macro HNDL     [val:?] \{ \common \local anonymous
                          define field@struct anonymous,HNDL.aligned,<val> \}
   macro align    val:sizeof.VOID   \{ \local anonymous
                          define field@struct anonymous,align,val \}
   macro union \{ field@struct equ ,union,<
                  sub@struct equ union \}
   macro struct \{ field@struct equ ,substruct,<
                  sub@struct equ substruct \} }    


that helps!!!:
as result 32 bit executables are assembled from crossbit structures.
ARM 32 bit executables are assembled from same crossbit structures too.
binary outs with legacy fasm struct format & crossbit structures differs only in parts of current datetime fields relative to compilation moment. And they are workable.
64 bit ones not tested yet.

The question in above of this post remains: "Why align isn`t allowed by default in virtuals relative to regs?" and this limitation moved to fasmg realization of align?
Post 01 Feb 2019, 08:28
View user's profile Send private message Send e-mail Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 802
Location: Russian Federation, Sochi
ProMiNick 04 Feb 2019, 09:05
Can we bring back to struct macro?
In case of fasmg parameters passed in conjunctions with field names where they shouyld be passed.
so adding alignment, padding or whatever is trivial.
But in fasm1 parameters passed according to each overriden data definition line,
so if we create struct:
A dd ? ; must be dword size, not "rb 4", so that we couldn`t combine this & next line together
rb 1 ; let suppose we need skip 1 byte; unnamed fields shouldnt be encountered in params for passing to struc or macro
B db 7 dup (?)
than if we pass to struc & macro of the same name 2 params: one of them goes to A. But second one not goes to B. we have to pass one additional param before B.
May be this could be solved with extension struct element from 3 args [field,type,def] to 4, so aligment or padding lines would work with 4th arg, and will not create one more value that will be passed to struc & macro of the same name.
Post 04 Feb 2019, 09:05
View user's profile Send private message Send e-mail Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 04 Feb 2019, 09:27
ProMiNick wrote:
Can we bring back to struct macro?
I'm going to split the thread to make it easier to keep track.
Post 04 Feb 2019, 09:27
View user's profile Send private message Visit poster's website Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 802
Location: Russian Federation, Sochi
ProMiNick 12 Feb 2019, 01:25
post edited (temporary moved back to data definition types standard for fasm)
Code:
force_align = 1;0

def.rb equ db
def.rw equ dw
def.rd equ dd
def.rp equ dp
def.rq equ dq
def.rt equ dt
core.datadefs equ db,dw,du,dd,dp,dq,dt
core.datarsvs equ rb,rw,rd,rp,rq,rt    
Code:
macro struct name {
        define @struct
        match _name rest, name: \{
                @struct_name equ _name
                match :,rest \\{
                        @struct_field equ
                        @struct_place equ \\}
                match parent:,rest \\{
                        @struct_field equ fieldsof.\#parent
                        @struct_place equ placesof.\#parent \\} \}

        sub@struct equ
        virtual at 0
                macro define@field name,type,value \{
                        define @struct_field name,type,value
                        define @struct_place name,type,value \}

                macro reserve@field name,type,value \{
                        define @struct_place name,type,value \}

                irp datadef,core.datadefs \{
                        struc datadef [val:?] \\{ \\common define@field .,datadef,<val> \\}
                        macro datadef [val:?] \\{
                                \\common \\local anonymous
                                reserve@field anonymous,datadef,<val> \\} \}

                irp datadef,core.datarsvs \{
                        struc datadef count \\{ \\common define@field .,def.#datadef,count dup (?) \\} ;????????? wrong
                        macro datadef count \\{
                                \\common \\local anonymous
                                reserve@field anonymous,def.#datadef,count dup (?) \\} \}

                irp datadef,align,padding \{
                        struc datadef count \\{ \\common define@field .,datadef,count \\}
                        macro datadef count \\{
                                \\common \\local anonymous
                                reserve@field anonymous,datadef,count \\} \}

                irp datadef,union,struct \{ macro datadef \\{ field@struct equ ,datadef,<
                                                 sub@struct equ datadef \\} \}

                macro ends \{
                        match , sub@struct \\{ irp datadef,core.datadefs,core.datarsvs,align,padding \\\{ restruc datadef
                                                                                purge datadef \\\}
                        purge union,struct
                        irpv fields,@struct_field \\\{ restore @struct_field
                                                      \\\common define fieldsof.#@struct_name fields \\\}
                        irpv fields,@struct_place \\\{ restore @struct_place
                                                      \\\common define placesof.#@struct_name fields \\\}
                        if $
                                display 'Error: definition of ',`@struct_name,' contains illegal instructions.',0Dh,0Ah
                                err
                        end if
                        make@struct name,fieldsof.#@struct_name ;need uniqe name for fields & places separatedly
                        make@struct name,placesof.#@struct_name ;
        end virtual     \\}
                        match any, sub@struct \\{
                                tmp@struct equ field@struct
                                restore field@struct
                                field@struct equ tmp@struct> \\}
                        restore sub@struct \} }    


the main target of addition is passing data to structs only to named fields. and ofcourse add instructure padding & alignment features
Post 12 Feb 2019, 01:25
View user's profile Send private message Send e-mail 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.