flat assembler
Message board for the users of flat assembler.

Index > Main > Concat constant value to symbol in a macro (fasm 1.73.30)

Author
Thread Post new topic Reply to topic
BismuthGlass



Joined: 08 Mar 2022
Posts: 4
BismuthGlass 08 Mar 2022, 17:01
Hello everyone,

I've been recently trying to make the switch over from nasm to fasm, and something I use a lot over at nasm is the generation of unique global labels through appending a variable value to a label name. I was trying to figure out how to do this in fasm, but I just can't seem to do it, and I can't find any info on it either. Here's an example of what I'm trying to write:

Code:
macro strtbl name, [string] {
    common
        local variable
        variable = 1
    forward
        label name#variable
        db string, 0
        variable = variable + 1
}

strtbl lbl, 'abc', 'dfg', 'eaf'
    


So the idea is that this would generate:

Code:
label lbl1
db 'abc', 0
label lbl2
db 'dfg', 0
label lbl3
db 'eaf', 0
    


Is there a way to achieve this?

Also, on an unrelated note, should I be using FASM1 at all, or should I just use FASMG? I am somewhat aware of the differences, but I can't really find a consensus on what people usually use, and the reasons to use FASM1 over the newer FASMG.

Thank you in advance.
Post 08 Mar 2022, 17:01
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8358
Location: Kraków, Poland
Tomasz Grysztar 08 Mar 2022, 18:26
Because fasm 1 has a separate preprocessing stage, you need to use a preprocessor-time variable and REPT directive to make a computed token. The variables defined with "=" are assembly-time, so they are of no use here (preprocessor sees lines like "variable = variable + 1" as just a raw text to pass to the assembly stage).

Your macro should therefore look like this:
Code:
macro strtbl name, [string] {
    common
        local variable
        variable equ 1
    forward
        rept 1 v:variable
        \{
            label name\#v
        \}
        db string, 0
        variable equ variable + 1
}

strtbl lbl, 'abc', 'dfg', 'eaf'    
To make sure that you get what you wanted, you can generate a symbols file (with "-s" switch in command line) and then use PREPSRC tool (from the TOOLS directory) to see the output of the processor that is then passed to the assembler:
Code:
; variable?0
;variable?0 equ 1

;rept 1 v:variable?0
;{
; label lbl#v
;}
label lbl1
db 'abc',0
;variable?0 equ 1+1
;rept 1 v:variable?0
;{
; label lbl#v
;}
label lbl2
db 'dfg',0
;variable?0 equ 1+1+1
;rept 1 v:variable?0
;{
; label lbl#v
;}
label lbl3
db 'eaf',0
;variable?0 equ 1+1+1+1    
BismuthGlass wrote:
Also, on an unrelated note, should I be using FASM1 at all, or should I just use FASMG? I am somewhat aware of the differences, but I can't really find a consensus on what people usually use, and the reasons to use FASM1 over the newer FASMG.
I have written a short summary of potential reasons in the migration guide. In general, unless you need a really fast assembly of lots of x86 code, you should find fasmg more capable. With fasmg a macro doing the same thing could look like:
Code:
macro strtbl name, strings&
        iterate string, strings
                label name#%
                db string, 0
        end iterate
end macro

strtbl lbl, 'abc', 'dfg', 'eaf'    
Post 08 Mar 2022, 18:26
View user's profile Send private message Visit poster's website Reply with quote
BismuthGlass



Joined: 08 Mar 2022
Posts: 4
BismuthGlass 09 Mar 2022, 10:37
Thanks for the suggestions. I must admit that fasmg looks very scary, but reading through the examples and the manual it has a lot of cool features, and I really like the "everything is a macro and nothing is sacred" philosophy. It seems like the kind of thing I've been looking for, so I'll give it a go.
Post 09 Mar 2022, 10:37
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.