flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Macro for struct.

Author
Thread Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 1080
Roman
I write on Fasmw 1.73
I want do auto register for struct
Code:
Struct ar
A dd 0
B dw 0
C db 0
E dq 0
Ends

Macro n cval {} ;some magic

Mov rax, 0xff3040ee
 n ar.A ;generate Mov [ar.A], eax
 n ar.B ;generate Mov [ar.B], ax
 n ar.C ;generate Mov [ar.C], al
 n ar.E ;generate Mov [ar.E], rax
    

How do this?
Post 26 Jan 2021, 01:27
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3282
Location: vpcmipstrm
bitRAKE
I'm way out of practice with fasm1, but if you had the size you could do:
Code:
macro n cval,sz {
      rept 1 i:sz \{
      match X|=i:A|Y,||1:al|2:ax|4:eax|8:rax|| \\{
            mov [cval],A
      \\}
      \}
}    
(In fasmg labels have a size. Razz)

_________________
¯\(°_o)/¯ unlicense.org
Post 26 Jan 2021, 18:01
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7919
Location: Kraków, Poland
Tomasz Grysztar
Wow, that's a nice trick!

The standard "struct" macro for fasm defines some additional constants that you could use:
Code:
include 'macro/struct.inc'

struct ar
 A dd 0
 B dw 0
 C db 0
 E dq 0
ends

display '0'+sizeof.ar.A    
but to have them at preprocessing time you might have to alter the macro.
Post 26 Jan 2021, 18:25
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1080
Roman
Srt ar
Why we not get size Srt. X?
But fine get size of struct ar.
Post 27 Jan 2021, 06:03
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3282
Location: vpcmipstrm
bitRAKE
Code:
macro na cval,parent {
  local sz
  sz = sizeof.#parent
  if sz = 1
    mov [cval],al
  else if sz = 2
    mov [cval],ax
  else if sz = 4
    mov [cval],eax
  else if sz = 8
    mov [cval],rax
  else
    display "size of item is not register size"
    err
  end if
}    
...but it needs to be used like:
Code:
child ar ; define an instance
na child.A,ar.A    
STRUCT wasn't meant to support this, but a slight change would make it work? Like have each instance creation define the label "name#.parent" and then get the parent within the "n" macro?

_________________
¯\(°_o)/¯ unlicense.org
Post 27 Jan 2021, 20:44
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1080
Roman
Thanks bitRAKE !
Work fine.
I little modified.
Code:
  macro sSv cval {
  local sz
  sz = cval#_tp
  if sz = 1
    mov [cval],al
  else if sz = 2
    mov [cval],ax
  else if sz = 4
    mov [cval],eax
  else if sz = 8
    mov [cval],rax
  else
    display "size of item is not register size"
    err
  end if
}           
     mov  eax,0xff3040aa
     zio.A_tp equ sizeof.ar.A ;would be nice if some macro do this automatically.
     zio.B_tp equ sizeof.ar.B
     zio.C_tp equ sizeof.ar.C
     sSv  zio.A
     sSv  zio.B
     sSv  zio.C
    


Last edited by Roman on 28 Jan 2021, 05:57; edited 1 time in total
Post 28 Jan 2021, 05:29
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.