flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Problem using dup to define an array of structures

Thread Post new topic Reply to topic

Joined: 05 Jan 2006
Posts: 62
Location: China->US->China->?
Hi, All
Consider the following code snippet:
include 'win32ax.inc'

struct ATOM
  proton   dd ?
  electron dd ?
  neutron  dd ?

atom ATOM ; correct
molecular ATOM 128 dup (?) ; incorrect
string db 128 dup (?) ; correct

I was trying to use dup operator to define an array of structures namely molecular, but it seems that fasm does not allow to do this and dup can only be used with 'native' data types. Is it possible to extend dup in such a way that user defined data types can be duplicated in this way? thanks in advance.
Post 30 Jan 2006, 12:14
View user's profile Send private message Reply with quote
Your code has a bug

Joined: 06 May 2005
Posts: 4624
Location: Argentina
You can do:
molecular ATOM
rb 127 * sizeof.ATOM    

But maybe there is a more elegant solution

[edit]This apparently works:
include 'win32ax.inc'

macro struct name
  struc name#.dup count\{
    . name
    rb sizeof.#name * (count - 1)
 struct name

struct ATOM
  proton   dd ? 
  electron dd ? 
  neutron  dd ? 

atom ATOM ; correct 
molecular ATOM.dup 128 ; correct now
string db 128 dup (?) ; correct    
Post 30 Jan 2006, 14:32
View user's profile Send private message Reply with quote

Joined: 05 Jan 2006
Posts: 62
Location: China->US->China->?
I defined the following struc macro which works outside the struct macro. I am still trying to integrate it into struct macro but it seems rather complicated. Anyone has a better idea?

struc ATOM2 Param
        if 1
        match num dup (?),Param
                rept num count
                     struc MakeItem
                           \.proton   dd ?
                           \.electron dd ?
                           \.neutron  dd ?
                     .@\\#count MakeItem

        .proton   dd ?
        .electron dd ?
        .neutron  dd ?
        end if

atom ATOM2
molecular ATOM2 5 dup (?)

include 'macro\struct.inc'

   quantity dd ?
   atoms    ATOM2 5 dup (?)



Actually, this definition of ATOM2 works within the struct macro, but there is a problem, that is after preprocess, above code(only the definition of foo) becomes:
label foo
        ;match,{foo.number dd ?}
        foo.number dd ?
        ;match any,{foo.number dd
        ; if~foo.number eq foo
        ; rb sizeof.MOLECULAR.number-($-foo.number)
        ; end if}
        ;match,{foo.atoms.@1.proton dd ?}
        foo.atoms.@1.proton dd ?
        ;match any,{foo.atoms.@1.proton dd
        ; if~foo.atoms.@1.proton eq foo
        ; rb sizeof.MOLECULAR.atoms.@1.proton-($-foo.atoms.@1.proton)
        ; end if}
        ;match,{foo.atoms.@1.electron dd ?}
        foo.atoms.@1.electron dd ?
        ;match any,{foo.atoms.@1.electron dd
        ; if~foo.atoms.@1.electron eq foo
        ; rb sizeof.MOLECULAR.atoms.@1.electron-($-foo.atoms.@1.electron)
        ; end if}
        ;match,{foo.atoms.@1.neutron dd ?}
        foo.atoms.@1.neutron dd ?
        ;match any,{foo.atoms.@1.neutron dd
        ; if~foo.atoms.@1.neutron eq foo
        ; rb sizeof.MOLECULAR.atoms.@1.neutron-($-foo.atoms.@1.neutron)
        ; end if}
        ;match,{foo.atoms.@2.proton dd ?}
        foo.atoms.@2.proton dd ?
        ;match any,{foo.atoms.@2.proton dd
        ; if~foo.atoms.@2.proton eq foo
        ; rb sizeof.MOLECULAR.atoms.@2.proton-($-foo.atoms.@2.proton)
        ; end if}
        ;match,{foo.atoms.@2.electron dd ?}
        foo.atoms.@2.electron dd ?
        ;match any,{foo.atoms.@2.electron dd
        ; if~foo.atoms.@2.electron eq foo
        ; rb sizeof.MOLECULAR.atoms.@2.electron-($-foo.atoms.@2.electron)
        ; end if}
        ;match,{foo.atoms.@2.neutron dd ?}
        foo.atoms.@2.neutron dd ?
        ;match any,{foo.atoms.@2.neutron dd
        ; if~foo.atoms.@2.neutron eq foo
        ; rb sizeof.MOLECULAR.atoms.@2.neutron-($-foo.atoms.@2.neutron)
        ; end if}
        ;match,{foo.atoms.@3.proton dd ?}
        foo.atoms.@3.proton dd ?
        ;match any,{foo.atoms.@3.proton dd
        ; if~foo.atoms.@3.proton eq foo
        ; rb sizeof.MOLECULAR.atoms.@3.proton-($-foo.atoms.@3.proton)
        ; end if}
        ;match,{foo.atoms.@3.electron dd ?}
        foo.atoms.@3.electron dd ?
        ;match any,{foo.atoms.@3.electron dd
        ; if~foo.atoms.@3.electron eq foo
        ; rb sizeof.MOLECULAR.atoms.@3.electron-($-foo.atoms.@3.electron)
        ; end if}
        ;match,{foo.atoms.@3.neutron dd ?}
        foo.atoms.@3.neutron dd ?
        ;match any,{foo.atoms.@3.neutron dd
        ; if~foo.atoms.@3.neutron eq foo
        ; rb sizeof.MOLECULAR.atoms.@3.neutron-($-foo.atoms.@3.neutron)
        ; end if}
        ;match,{foo.atoms.@4.proton dd ?}
        foo.atoms.@4.proton dd ?
        ;match any,{foo.atoms.@4.proton dd
        ; if~foo.atoms.@4.proton eq foo
        ; rb sizeof.MOLECULAR.atoms.@4.proton-($-foo.atoms.@4.proton)
        ; end if}
        ;match,{foo.atoms.@4.electron dd ?}
        foo.atoms.@4.electron dd ?
        ;match any,{foo.atoms.@4.electron dd
        ; if~foo.atoms.@4.electron eq foo
        ; rb sizeof.MOLECULAR.atoms.@4.electron-($-foo.atoms.@4.electron)
        ; end if}
        ;match,{foo.atoms.@4.neutron dd ?}
        foo.atoms.@4.neutron dd ?
        ;match any,{foo.atoms.@4.neutron dd
        ; if~foo.atoms.@4.neutron eq foo
        ; rb sizeof.MOLECULAR.atoms.@4.neutron-($-foo.atoms.@4.neutron)
        ; end if}
        ;match,{foo.atoms.@5.proton dd ?}
        foo.atoms.@5.proton dd ?
        ;match any,{foo.atoms.@5.proton dd
        ; if~foo.atoms.@5.proton eq foo
        ; rb sizeof.MOLECULAR.atoms.@5.proton-($-foo.atoms.@5.proton)
        ; end if}
        ;match,{foo.atoms.@5.electron dd ?}
        foo.atoms.@5.electron dd ?
        ;match any,{foo.atoms.@5.electron dd
        ; if~foo.atoms.@5.electron eq foo
        ; rb sizeof.MOLECULAR.atoms.@5.electron-($-foo.atoms.@5.electron)
        ; end if}
        ;match,{foo.atoms.@5.neutron dd ?}
        foo.atoms.@5.neutron dd ?
        ;match any,{foo.atoms.@5.neutron dd
        ; if~foo.atoms.@5.neutron eq foo
        ; rb sizeof.MOLECULAR.atoms.@5.neutron-($-foo.atoms.@5.neutron)
        ; end if}
        ;--------------------- not expected below --------------------------------

        ;match,{foo.atoms.proton dd ?}
        foo.atoms.proton dd ? 
        ;match any,{foo.atoms.proton dd
        ; if~foo.atoms.proton eq foo
        ; rb sizeof.MOLECULAR.atoms.proton-($-foo.atoms.proton)
        ; end if}
        ;match,{foo.atoms.electron dd ?}
        foo.atoms.electron dd ?
        ;match any,{foo.atoms.electron dd
        ; if~foo.atoms.electron eq foo
        ; rb sizeof.MOLECULAR.atoms.electron-($-foo.atoms.electron)
        ; end if}
        ;match,{foo.atoms.neutron dd ?}
        foo.atoms.neutron dd ?
        ;match any,{foo.atoms.neutron dd
        ; if~foo.atoms.neutron eq foo
        ; rb sizeof.MOLECULAR.atoms.neutron-($-foo.atoms.neutron)
        ; end if}

that is, one more set of ATOM2 members are generated. Sad
Post 05 Feb 2006, 08:46
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.