flat assembler
Message board for the users of flat assembler.

Index > Main > please explain this macro behavior, looks wrong to me

Author
Thread Post new topic Reply to topic
tthsqe



Joined: 20 May 2009
Posts: 730
tthsqe
Code:
White fix 0
Black fix 1

macro testing color {
local color, value
 if color eq White
   value equ 42
 end if
 if color eq Black
   value equ 24
 end if
                mov  eax, value
}

Start:

testing White
testing Black       

    


code generated:
Code:
mov eax, 0x18
mov eax,0x18    


same problem with
Code:
macro testing color {
local color, value
 match White,color \{
   value equ 42
 \}
 match Black,color \{
   value equ 24
 \}
                mov  eax, value
}     


Last edited by tthsqe on 11 Dec 2014, 03:06; edited 1 time in total
Post 11 Dec 2014, 02:56
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17664
Location: In your JS exploiting you and your system
revolution
Use equals (=) instead of equ.
Code:
value = 42    
You are mixing preprocessor and assembler stages when using "equ" and "if" together.
Post 11 Dec 2014, 02:59
View user's profile Send private message Visit poster's website Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 730
tthsqe
Ah, thanks. I ended up using equ because value was used in another macro, and fasm didn't like = in the case. Ill try to switch from preprocessor to assembler in that macro and see if I can fix it.
Post 11 Dec 2014, 03:15
View user's profile Send private message Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 730
tthsqe
but then the match should handle the conditional p eprocessing correctly, no?
Post 11 Dec 2014, 03:16
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17664
Location: In your JS exploiting you and your system
revolution
Don't use "color" as a local and it should be fine.
Post 11 Dec 2014, 03:28
View user's profile Send private message Visit poster's website Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 730
tthsqe
ah, don't know what I was thinking with local color. This seem to be working. For a moment I though you couldn't work with conditional defintions of local variables
Code:
White fix 0 
Black fix 1 

macro  move a,b {
 if b eq 42
      mov a, 42
 else if b eq 24
      mov [a], dword 24
 else
  err
 end if
}

macro testing color {
local value
 match =White,color \{
   value equ 42
 \}
 match =Black,color \{
   value equ 24
 \}
              move  eax, value
}

Start:

testing Black
testing White         
Post 11 Dec 2014, 03:36
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
tthsqe,

Had you intentionally used symbolic comparison (eq) instead of numeric (=) in if conditions?
Post 13 Dec 2014, 22:30
View user's profile Send private message Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 730
tthsqe
After reading the docs, it was my impression that something like the conditional in "if A = B" is invisible to the preprocessor and is dealt with by the assembler. Something like "if A eq B" is handled as expected by the precoressor. Is there something done wrong in the last example I posted?

After thinking about this more, it looks like the preprocessor is limited in fasm with respect to expression evaluation. For example, something like
Code:
macro bar c {
 if c eq 8
    mov  eax, 8
 else
  err
 end if
}

macro foo a, b {
   bar a+b
}

foo 5,3 ; 5+3 is passed to bar and is not "eq" to 8, so err is encountered    

I realize that "if c = 8" will make this work witht he assembler, but what if I want to keep using the preprocessor?
Post 14 Dec 2014, 09:28
View user's profile Send private message Reply with quote
evk1



Joined: 18 Jun 2014
Posts: 24
evk1
tthsqe wrote:
Something like "if A eq B" is handled as expected by the precoressor.
Shocked

Are you sure? After reading docs I came to the opposite conclusion. To my mind, the preprocessor just replaces symbolic constants with their values in expressions like if A eq B, while equality check is performed by the assembler. For example following code displays only expr1 is true.
Code:
a equ xyz

expr1 equ a eq xyz
define expr2 a eq xyz

if expr1
  display 'expr1 is true'
end if
if expr2
  display 'expr2 is true'
end if    


Don't I understand something?

_________________
Sorry for my English
Post 14 Dec 2014, 10:52
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
tthsqe & evk1,

Condition in if directive is definitely evaluated during assembly stage. I'd meant that eq operator straightforwardly compares its operands for being exactly equal (i.e. same parsed symbols in the same order, even if those operands are nonsensical for numeric comparison):
Code:
if Hello, tbyte ! eq Hello, tword !
  display "eq"
else
  display "~eq"
end if    
Numerical equality operator (=) requires something more intelligible. Wink

evk1, your example with define works as defined. You may use match directive to expand symbolic constants in defined symbolic constants' values though (one step at a time):
Code:
define A xyz
define B A; B eq A, not xyz
define C B; C eq B, not xyz or A

macro define_@ {; allow recursion
  macro @ [$*] \{; expand tail and use the result as a statement
    define_@
    match \\$, $ \\{ \\$ \\}
    purge @
  \}
} define_@

if A eq xyz
  display "A eq xyz (direct expansion)", 13, 10
end if

if B eq xyz; expands to 'if A eq xyz'
  display "B eq xyz (direct expansion)", 13, 10
@ else if B eq xyz; expand 'if A eq xyz' once
  display "B eq xyz (indirection level 1)", 13, 10
end if

if C eq xyz; expands to 'if B eq xyz'
  display "C eq xyz (direct expansion)", 13, 10
@ else if C eq xyz; expand once to 'if A eq xyz'
  display "C eq xyz (indirection level 1)", 13, 10
@ @ else if C eq xyz; expand twice
  display "C eq xyz (indirection level 2)", 13, 10
end if    
Post 14 Dec 2014, 16:53
View user's profile Send private message Reply with quote
evk1



Joined: 18 Jun 2014
Posts: 24
evk1
baldr wrote:
evk1, your example with define works as defined. You may use match directive to expand symbolic constants in defined symbolic constants' values though (one step at a time


Thank you, but I had already known it. My goal was to demonstrate eq cannot expand symbolic constant by itself.

_________________
Sorry for my English
Post 14 Dec 2014, 17:13
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
evk1,

eq is an operator of assembly stage directive if, how could it expand anything? Wink
Post 14 Dec 2014, 17:21
View user's profile Send private message Reply with quote
evk1



Joined: 18 Jun 2014
Posts: 24
evk1
baldr wrote:
evk1,

eq is an operator of assembly stage directive if, how could it expand anything? Wink


Of course, it can't. And this was the main idea of my post. I thought, it was clear.

Oh, most of my posts was misunderstood or just ignored. Sad Is my English really so dreadful? Sad Embarassed

Oh, moderators, sorry for so many off-topic replies.

_________________
Sorry for my English
Post 14 Dec 2014, 17: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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.