flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Make macro insert code at a different point

Author
Thread Post new topic Reply to topic
413x1nkp



Joined: 13 Jan 2026
Posts: 4
413x1nkp 14 Jan 2026, 19:47
Is there some sort of "preprocessor jump" instruction that would allow insertion of code at a point other than the immediate position of a macro?

Example:
Code:
format ELF64

section '.text' executable

entry _start

STATIC_STR_COUNTER = 0

macro define_static_str content {
    ; preprocessor jump to section '.data'
    ; insert str#STATIC_STR_COUNTER: db content, 0
    ; preprocessor jump back to this macro
    ;
    ; do something with str#STATIC_STR_COUNTER here
    ;
    ; STATIC_STR_COUNTER = STATIC_STR_COUNTER +1
    ; finish
}

_start:
    define_static_str "some string"
    define_static_str "another string"



section '.data' writeable
; after the macro is processed:
; str0: db "some string", 0
; str1: db "another string", 0

    


So kinda like what C compiler does to string literals.
I've read a bunch of info on FASM macros but couldn't manage to find a way of doing something like this. Is this behavior something that's possible to achieve with just the FASM preprocessor?
I thought about using virtual at a dummy string in '.data', but it just defines a label to that dummy string.

Oh, also, is there a way to expand STATIC_STR_COUNTER to an integer for concatenation? I couldn't find any info on that either..

I suppose i could make a function that outputs each string as an entry in a separate include file, which is then included at the '.data' section, but that sounds really cursed..

Would greatly appreciate any ideas :)
Post 14 Jan 2026, 19:47
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8500
Location: Kraków, Poland
Tomasz Grysztar 14 Jan 2026, 19:57
One of the examples in fasm2 package does it. If you need something similar for fasm 1.x, see the globals package.
Post 14 Jan 2026, 19:57
View user's profile Send private message Visit poster's website Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1208
Location: Russia
macomics 14 Jan 2026, 20:04
Code:
; fasm ./this_file.asm ./this_file.o
; ld -o ./this_file.x64 ./this_file.o
format ELF64

macro init_data_section {}

section '.text' executable
public _start

STATIC_STR_COUNTER = 0

macro define_static_str content {
  local string_label, string_length
  macro init_data_section \{
    init_data_section
    string_label db content
    string_length = $ - string_label
  \}
  lea rax, [string_label]
  mov edx, string_length
}

_start:
    define_static_str "some string"
    push rax
    push rdx
    define_static_str "another string"
    mov rsi, rax
    mov edi, 1
    mov eax, 1
    syscall

    pop rdx
    pop rsi
    mov edi, 1
    mov eax, 1
    syscall

    mov dil, 0
    mov eax, 60
    syscall

section '.data' writeable
init_data_section
irpv a,init_data_section { resore a }    
Post 14 Jan 2026, 20:04
View user's profile Send private message Reply with quote
413x1nkp



Joined: 13 Jan 2026
Posts: 4
413x1nkp 14 Jan 2026, 22:53
Quote:

One of the examples in fasm2 package does it. If you need something similar for fasm 1.x, see the globals package.


Oh thanks! I'm on fasm 1.73.33 and _globals.inc seems like exactly what i want!
I'm still not entirely sure how to procedurally generate names for these variables that could be easily cross-referenced across the macro, though..
I tried doing the concatenation of a counter, but it doesn't expand to a value during preprocessing..

Quote:

; fasm ./this_file.asm ./this_file.o
; ld -o ./this_file.x64 ./this_file.o
format ELF64

macro init_data_section {}

section '.text' executable
public _start

STATIC_STR_COUNTER = 0

macro define_static_str content {
local string_label, string_length
macro init_data_section \{
init_data_section
string_label db content
string_length = $ - string_label
\}
lea rax, [string_label]
mov edx, string_length
}

_start:
define_static_str "some string"
push rax
push rdx
define_static_str "another string"
mov rsi, rax
mov edi, 1
mov eax, 1
syscall

pop rdx
pop rsi
mov edi, 1
mov eax, 1
syscall

mov dil, 0
mov eax, 60
syscall

section '.data' writeable
init_data_section
irpv a,init_data_section { resore a }


This actually seems like a perfect solution for my problem!! I somehow forgot how powerful `local' is!

I greatly appreciate your help, thanks everyone!! :)

Although i'm still curious if evaluating a constant like STATIC_STR_COUNTER to a value would be possible during preprocessing, so that it can be concatenated to a label (kind of like local but DIY)
Post 14 Jan 2026, 22:53
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8500
Location: Kraków, Poland
Tomasz Grysztar 15 Jan 2026, 13:26
413x1nkp wrote:
Although i'm still curious if evaluating a constant like STATIC_STR_COUNTER to a value would be possible during preprocessing, so that it can be concatenated to a label (kind of like local but DIY)
In fasm 1.x symbolic variables (defined with EQU/DEFINE) are preprocessor-time, while numeric variables (defined with =) are assembly-time. However, REPT is a directive that can perform preprocessor-time calculations, and can be used to update the value of symbolic variable with a decimal token containing updated number:
Code:
STATIC_STR_COUNTER equ 0

macro define_static_str content {
        rept 1  this:STATIC_STR_COUNTER, next:STATIC_STR_COUNTER+1 \{
                static_data equ str\#this db content
                STATIC_STR_COUNTER equ next
        \}
}

define_static_str "some string"
define_static_str "another string"

irpv static_string, static_data {

        static_string
}

; result is:
;       str0 db 'some string'
;       str1 db 'another string'    
Post 15 Jan 2026, 13:26
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.