flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > struct macro

Author
Thread Post new topic Reply to topic
kempis



Joined: 12 Jun 2008
Posts: 49
kempis
Hello:
I wrote an extended struct macro, it allows you to write code:
Code:
struct        SAMPLE_A
   field1 dd ?
   field2 dd ?
   field3 dd ?
ends

struct    SAMPLE_B
   field1 dd ?
   field2 dd ?
   field3 SAMPLE_A
   field4 SAMPLE_A
ends

;defining-data
data1 SAMPLE_B field4=<1,field3=2>,field1=3,4,<field2=5,6>

;it will set:
;[data1.field1]=3
;[data1.field2]=4
;[data1.field3.field1]=?
;[data1.field3.field2]=5
;[data1.field3.field3]=6
;[data1.field4.field1]=1
;[data1.field4.field2]=?
;[data1.field4.field3]=2    


This is the code:
Code:
;advanced-struct
;but it's difficult to implement 'union'
;anyone have idea?

macro struct name*{
   fields@struct equ name
   match child parent,name\{
      fields@struct equ child,fields@\#parent\}
   irp type,b,u,w,d,p,q,t\{
      macro d\#type[arg]\\{\\common\\local x
       fields@struct equ fields@struct,x,d\#type,<arg>\\}
      macro r\#type count*\\{\\local x
        fields@struct equ fields@struct,x,d\#type,<(count) dup ?>\\}
      struc d\#type[arg]\\{\\common fields@struct equ fields@struct,.,d\#type,<arg>\\}
      struc r\#type count*\\{fields@struct equ fields@struct,.,d\#type,<(count) dup ?>\\}
   \}
   macro label NAME\{fields@struct equ fields@struct,NAME,:,\}
   virtual at 0
}

macro ends{
   purge db,du,dw,dd,dp,dq,dt,rb,ru,rw,rd,rp,rq,rt,label
   restruc db,du,dw,dd,dp,dq,dt,rb,ru,rw,rd,rp,rq,rt
   match name=,fields,fields@struct\{
      fields@struct equ
      define@struct name,fields
      fields@\#name equ fields\}
}

fields@struct equ

macro define@struct name,[field,type,value]{
common
   if $
      display "Error: definition of struct ",`name
      display "contains illegal instructions",0x0D,0x0A
      "Error"
   end if
   match ,definedstruct@#name\{
      display "Error: multiple definition of struct "
      display `name,0x0D,0x0A
      "Error"
   \}
   define definedstruct@#name
   define else else
forward
   name#.#field type value
   sizeof.#name#.#field=$-name#.#field
   match ,else\{
      match BEFORE,beforefield@struct\\{
      nextfield@#name#.\\#BEFORE equ field
      beforefield@struct equ field
      \\}
   \}
   match =else,else\{define else
      beforefield@struct equ field
      firstfield@#name equ field
   \}
common
   restore else,else
   sizeof.#name=$
   end virtual

   struc name [arg]\{
   \common
      match ,fields@struct\\{
       label .
     parse@struct equ
    bracket@struct equ
  irps ARG,arg\\\{
       define else else
            match ==,ARG\\\\{define else
          match ,bracket@struct\\\\\{
                  parse@struct equ parse@struct=,\\\\\}
          match any,bracket@struct\\\\\{
               parse@struct equ parse@struct=\\\\\}
        \\\\}
      match <,ARG\\\\{define else
        parse@struct equ parse@struct<
           bracket@struct equ bracket@struct,\\\\}
         match >,ARG\\\\{define else
        parse@struct equ parse@struct>
           restore bracket@struct\\\\}
     match =else,else\\\\{define else
              parse@struct equ parse@struct ARG\\\\}
          restore else,else
        \\\}
forward
     value@#name#.#field equ
common
   match PARSE,parse@struct\\\{
           ;PARSE
      make@struct name,PARSE
   \\\}
forward
     match VALUE,value@#name#.#field\\\{
            .#field type VALUE
          rb sizeof.#name#.#field-($-.#field)
      \\\}
        match ,value@#name#.#field\\\{
         .#field type value
       \\\}
common
      \\}
      match any,fields@struct\\{
       fields@struct equ fields@struct,.,name,<arg>\\}
      sizeof.\#.=sizeof.#name
   \}
   macro name[arg]\{
   \common \local x
      x name arg
   \}
}

macro make@struct name,[value]{
common
   currentfield@struct equ firstfield@#name
forward
   define else else
   match FIELD==,value\{define else
      currentfield@struct equ FIELD
   \}
   match =else,else\{define else
      match CURRENT,currentfield@struct\\{
  match any,value@#name#.\\#CURRENT\\\{
        display "Error: defining instance of struct "
         display `name," contains illegal data",0x0D,0x0A
          define else else
            match =value@#name#.\\#CURRENT,any\\\\{define else
          display "field exceed boundary",0x0D,0x0A
      \\\\}
      match =else,else\\\\{define else
              display "Multiple definition of field "
           display \\`CURRENT,0x0D,0x0A
           \\\\}
      restore else,else
           "Error"
        \\\}
        match ,value@#name#.\\#CURRENT\\\{
           value@#name#.\\#CURRENT equ value
         currentfield@struct equ nextfield@#name#.\\#CURRENT
    \\\}
      \\}
   restore else,else
   \}
}



    


But it's still not complete. The union macro has not been implemented (It's so confusing). And I do'nt check for deeper field( does it still work?).

Maybe someone has some ideas about union macro etc...?
Or just check this macro if it works.


Last edited by kempis on 02 Jan 2009, 12:56; edited 3 times in total
Post 31 Dec 2008, 14:31
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17716
Location: In your JS exploiting you and your system
revolution
How can SAMPLE_A have field4?
Code:
;[data1.field4.field1]=1
;[data1.field4.field2]=?
;[data1.field4.field3]=2    
Question
Post 31 Dec 2008, 14:36
View user's profile Send private message Visit poster's website Reply with quote
kempis



Joined: 12 Jun 2008
Posts: 49
kempis
I'm sorry, I means it's SAMPEL_B
Code:
;defining-data
data1 SAMPLE_B field4=<1,field3=2>,field1=3,4,<field2=5,6>    
Post 31 Dec 2008, 23:34
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17716
Location: In your JS exploiting you and your system
revolution
How can SAMPLE_B have SAMPLE_B? Is that for real?
Code:
struct  SAMPLE_B
   field1 dd ?
   field2 dd ?
   field3 SAMPLE_A
   field4 SAMPLE_B
ends    
Why is there no [data1.field4.field4]?
Post 01 Jan 2009, 00:27
View user's profile Send private message Visit poster's website Reply with quote
kempis



Joined: 12 Jun 2008
Posts: 49
kempis
I'm sorry. I have mistakes for the sample.
Post 01 Jan 2009, 00:46
View user's profile Send private message Reply with quote
kempis



Joined: 12 Jun 2008
Posts: 49
kempis
I have edited it.
Post 01 Jan 2009, 00:53
View user's profile Send private message Reply with quote
kempis



Joined: 12 Jun 2008
Posts: 49
kempis
OK, i edit it again. (comment out PARSE).
Post 02 Jan 2009, 13:00
View user's profile Send private message 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. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.