flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [fasmg] get data size of struc element

Author
Thread Post new topic Reply to topic
zhak



Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak 10 Feb 2017, 15:11
I extended simple struct macro to be able to pass data sequences.
Code:
macro struct? name
  macro end?.struct?!
      end namespace
        local a, i, n, t
        a = 0
        i = 0
        t equ
        irp def, args
          match name:type <value, def
            if a = 0
              store value:type at .name
              n equ .name
              t equ type
              i = 0
              a = 1
            else
              err "Sequences should be in 'name:type <n1,n2,...,nx>' format"
            end if
          else match value>, def
            if a = 1
              store value:t at n + (i + 1) * t
              a = 0
            else
              err "Sequences should be in 'name:type <n1,n2,...,nx>' format"
            end if
          else match name:value, def
            store value at .name
          else match value, def
            if a = 1
              i = i + 1
              store value:t at n + i * t
            else
              err "Values should be passed in 'name:value' format"
            end if
          end match
        end irp
    end struc
    virtual at 0
      name name
      sizeof.name = $
    end virtual
    purge end?.struct?

  end macro

    struc name args&
      label . : sizeof.name
      namespace .
end macro
    

But to do this, I have to pass data type for the sequence:
Code:
_my MY a:1, b:word <1,2,3>
    

Is it possible to retrieve data size from struc element to re-use it without the need to specify it explicitly?
Post 10 Feb 2017, 15:11
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 10 Feb 2017, 17:02
If you define a label with associated size, like:
Code:
data1 db ?
label data2 : word    
you can then extract this size with "metadata 0" operator:
Code:
dd data1 metadata 0 ; 1
dd data2 metadata 0 ; 2    
There is also an unary operator "sizeof" which is simply an alias for "metadata 0":
Code:
dd sizeof data1 ; 1
dd sizeof data2 ; 2    


As you may already know (because the basic "struct" macro uses it) when STORE directive has no declared size, the size that is associated with address is then used:
Code:
label name : type
store value : type at name
store value : sizeof name at name ; same as above but uses "name" only
store value at name               ; also the same result    
(note that this is not compatible with fasm 1, where STORE with no size specified used the default size 1).
Post 10 Feb 2017, 17:02
View user's profile Send private message Visit poster's website Reply with quote
zhak



Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak 11 Feb 2017, 00:07
Thanks Tomasz!

Another question: how can I pull first param value outside match?
From the above macro,
Code:
match name:value, def
  . . .
end match
; here want to use name
    

I tried equ, define, and even =, but none worked.
Something like
Code:
local param
;define param
match name:value, def
  param equ .name ; or redefine param name
end match
display sizeof param ;or .param
    
Post 11 Feb 2017, 00:07
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 11 Feb 2017, 10:49
Yes, it's the EQU (or DEFINE) that should work for this purpose:
Code:
match name:value, x:1
  name dd value
  param equ name
end match 
display '0' + sizeof param    
(this is a working snippet, it assembles all by itself).
But sometimes you may need to extract the value of symbolic variable into a parameter with MATCH:
Code:
match name:value, x:1
  param equ name
  v = value
end match

match name, param
  name dd v
end match

display '0' + sizeof param    
(attempting to define it as "param dd v" without MATCH would try to define symbol called "param" instead of "x" - the symbolic variables are automatically evaluated only inside the expressions in arguments to directives).
Post 11 Feb 2017, 10:49
View user's profile Send private message Visit poster's website 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.