flat assembler
Message board for the users of flat assembler.

Index > Main > equ $-sym inside a macro does not work as expected

Author
Thread Post new topic Reply to topic
alaa



Joined: 30 May 2019
Posts: 2
alaa 30 May 2019, 19:32
Hi,

My name is Ala'a and I'm new in this forum, as well as new to fasm.

I'm trying to learn assembly macroinstructions by doing small examples. the following example does not do what I expected (which is print hello world!). instead it prints random characters on the console.

upon trying to understand the macro expansion of 'string' and 'write' it seems that the second line of the 'string' which has equ, does not evaluate to the length of string. instead the hello.length equ $-hello and this is substituted in the write macro leading to incorrect string length (I used Fresh-IDE CTRL-U feature to inspect the output).


Code:

macro string name, quoted {
        name db quoted , 0
        name#.length equ $ - name
}

define sys_write 1
define stdout 1
macro write fd*, string*  {
        mov rax, sys_write
        mov rdi, fd 
        mov rsi, string
        mov rdx, string#.length
        syscall
}

macro print string* {
        write stdout, string
}

define sys_exit 60
define success 0
macro exit status=success {
        mov rax, sys_exit
        mov rdi, status
        syscall 
}

format elf64 executable

segment readable writeable
        string hello, "Hello World!"

segment executable readable
entry $
        print hello
        exit
    


I'm working on Linuxmint 19.1, 64bit, kernel 4.18.0-17-generic.

I tried to read the manual several times, but I think I'm skimming on something important on the order of preprocessing or something else.

thanks for your help.
Post 30 May 2019, 19:32
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8359
Location: Kraków, Poland
Tomasz Grysztar 30 May 2019, 19:50
In fasm EQU is a preprocessor's definition (textual replacement). To have a numeric value computed, use "=" instead:
Code:
name#.length = $ - name    
Post 30 May 2019, 19:50
View user's profile Send private message Visit poster's website Reply with quote
alaa



Joined: 30 May 2019
Posts: 2
alaa 30 May 2019, 19:55
Thanks a lot! It works now.
Post 30 May 2019, 19:55
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.