flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Problem using complex # constructions

Author
Thread Post new topic Reply to topic
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
I'm trying to write some macros that store some preprocessor values with a numeric index but the #i# does not resolve to its numeric value just the label character 'i' - is there some way to resolve this?

(The code below is a simplification of the intended macros)

Code:
macro example name,[array]
 {
 common  ; init
        i = 0
 forward ; store values
        _some_#name#_#i#_index = i ;  _some_x_i_index NOT _some_x_0_index etc ...
        i = i + 1
 common  ; write header
        db i
        i = 0
 forward ; write values
        db _some_#name#_#i#_index
 }

example x,1,1,1,1,1,1 ; ignore the 1's they're just placeholders     


This outputs hex 06 05 05 05 05 05 05 instead of 06 00 01 02 03 04 05

Many thanks! Cool
Post 17 Aug 2009, 12:17
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7740
Location: Kraków, Poland
Tomasz Grysztar
The = operator is an assembly-time operation, not preprocessor's one - therefore at the time the = is interpreted, all the preprocessing is already finished long time ago (see Understanding fasm for more details on this).

So you should use the symbolic variable (defined with "equ") instead of numerical one. And note, that to get the value of such variable available for concatenation you need to extract it with "match" directive (check out the documentation on this one, too).

With fasm 1.69 you can do it like this:
Code:
macro example name,[array]
 { 
 common  ; init 
        i equ 0
 forward ; store values 
        match value,i \{ _some_#name#_\#value\#_index = value \}
        rept 1 j:i+1 \{ i equ j \}
 common  ; write header 
        db i 
        i equ 0
 forward ; write values 
        match value,i \{ db _some_#name#_\#value\#_index  \}
        rept 1 j:i+1 \{ i equ j \}
 }

example x,1,1,1,1,1,1 ; ignore the 1's they're just placeholders

display '0'+_some_x_3_index ; tester, should display digit 3    
Post 17 Aug 2009, 12:52
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17352
Location: In your JS exploiting you and your system
revolution
It is a bit complex to do like that because "i=0" is an assembler stage construction and the rest are pre-processor stage constructions.

It requires you to get up "i equ 0" and then to use match to map the name to the value for the variable name.

Try this:
Code:
macro example name,[array]
 {
 common  ; init
        i equ 0
 forward ; store values
        match j,i \{_some_#name#_\#j\#_index = j \};  _some_x_i_index NOT _some_x_0_index etc ...
   match j,i \{
          rept 2 k:j {
                       i equ k
             \\}
  \}
 common  ; write header
        db i
        i equ 0
 forward ; write values
        match j,i \{ db _some_#name#_\#j\#_index \}
      match j,i \{
          rept 2 k:j {
                       i equ k
             \\}
  \}
 }

example x,1,1,1,1,1,1 ; ignore the 1's they're just placeholders    


[edit]
Tomasz Grysztar beat me to it, and made a better job of it than me.

I still haven't updated myself with the latest fasm capabilities. It seems I am living in the past.
Post 17 Aug 2009, 12:59
View user's profile Send private message Visit poster's website Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
WOW - thank you both very much!

I'd never really grasped match before... I also like that rept trick Cool Truely amazing how powerful macros can be.
Post 19 Aug 2009, 02:15
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 YouTube, Twitter.

Website powered by rwasa.