flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [fasmg] empty params, eqtype

Author
Thread Post new topic Reply to topic
Jin X



Joined: 06 Mar 2004
Posts: 133
Location: Russia
Jin X 14 Apr 2017, 22:52
1. How to check empty parameters of macro, like in fasm 1:
Code:
macro xx param
{
  if param eq
    ; empty
  else
    ; not empty
  end if
}    

2. In fasm 1 I can make something like:
Code:
macro   movx    op1,op2
{
    if  op2 eqtype 0 & op2 = 0
        xor     op1,op1
    else
        mov     op1,op2
    end if
}    
How can I do it on fasmg (check param for zero - even when param specified as const = 0)
Post 14 Apr 2017, 22:52
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 15 Apr 2017, 06:59
Jin X wrote:
1. How to check empty parameters of macro, like in fasm 1:
Code:
macro xx param
{
  if param eq
    ; empty
  else
    ; not empty
  end if
}    
It can be done with MATCH in a similar way to how it was possible to check for empty value in the preprocessor of fasm 1:
Code:
macro xx param
  match ,param
    ; empty
  else
    ; not empty
  end match
end macro    

Jin X wrote:
2. In fasm 1 I can make something like:
Code:
macro   movx    op1,op2
{
    if  op2 eqtype 0 & op2 = 0
        xor     op1,op1
    else
        mov     op1,op2
    end if
}    
How can I do it on fasmg (check param for zero - even when param specified as const = 0)
Unfortunately fasmg's EQTYPE compares only the result types of valid expressions and there is currently no simple way to determine if argument is such expression, the only way is to MATCH all possible variants of arguments that are not. If you use the set of x86 macros that come with fasmg then you can utilize the "parse_expression" macro to do the work for you:
Code:
macro   movx op1,op2
        x86.parse_operand @src,op2
        if @src.type = 'imm' & @src.imm = 0
                xor op1,op1
        else
                mov op1,op2
        end if
end macro     
Nevertheless I recognize the need for a method to detect valid expressions, I thought about introducing an additional kind of conditional directive for this purpose and I may add one in the future.
Post 15 Apr 2017, 06:59
View user's profile Send private message Visit poster's website Reply with quote
Jin X



Joined: 06 Mar 2004
Posts: 133
Location: Russia
Jin X 15 Apr 2017, 07:57
Thanks! Wink
Post 15 Apr 2017, 07:57
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 28 May 2017, 13:04
Tomasz Grysztar wrote:
Nevertheless I recognize the need for a method to detect valid expressions, I thought about introducing an additional kind of conditional directive for this purpose and I may add one in the future.
I've been thinking about various variants of a directive that would allow to detect malformed expressions and I did not like any of them, perhaps because they were too narrow. But I finally came up with an idea for a directive that could do this with a potential for more. My working name for it is IFONLY, and it would be a variant of IF that would ignore any errors when evaluating the condition. The meaning of such directive would be "assemble this block if the condition is a valid expression that can be correctly evaluated and is true, otherwise go to ELSE block".

For example to check if a macro parametr "op" is a valid expression and has a value that is an absolute number and is equal to zero, a simple "IFONLY (op) = 0" would suffice. The parentheses would ensure that tricky arguments like "1 | 0" would not pass through.
Post 28 May 2017, 13:04
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: 20363
Location: In your JS exploiting you and your system
revolution 28 May 2017, 13:09
Why not "if valid ..."
Code:
if valid <expression>    
And it would separate the two parts: validity and evaluation:
Code:
if valid <something> & <something> = 0    
Post 28 May 2017, 13:09
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 28 May 2017, 13:25
revolution wrote:
Why not "if valid ..."
Code:
if valid <expression>    
And it would separate the two parts: validity and evaluation:
Code:
if valid <something> & <something> = 0    
This is one of the main problems that led me to not following the EQ implementation of fasm 1 - if we want to allow absolutely any text in an argument, we cannot embed it in a larger expression, the only way to deal with it is to use a kind of directive where the argument can be put as a "rest of text until the end of line", like MATCH does it. Because any other structure can be tricked by putting the same special characters into the argument itself. In fact even the "(op) = 0" I put as an example above could be tricked, passing the value "1) | (0" as "op" would be enough to break it. This is why I considered directives like IFEXPR where the only argument would be a free-form text that you want to check if it is a valid expression. I thought that maybe a directive like IFONLY could be more robust, but it again has all the problems related to embedding a free-form text inside a syntactical structure. Yeah, perhaps the IFEXPR is the only good way to do it, after all - even though I do not like it.
Post 28 May 2017, 13:25
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: 20363
Location: In your JS exploiting you and your system
revolution 28 May 2017, 13:43
I suppose there could be a special separator construct:
Code:
if valid op && op = 0    
Anyone trying to "trick" it just makes it worse:
Code:
op equ 1) | (0
if valid (op) && (op) = 0
op equ 1) && (0
if valid (op) && (op) = 0    
Post 28 May 2017, 13:43
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 28 May 2017, 14:36
But then if value already contains this special separator, a part of free-form text would be interpreted as a separate expression - again not the expected behavior. If what we need is to check if something is a valid expression without causing an error, then it this text contains our "special separator" it is then able to create an error anyway and the entire purpose of such construction is compromised.

You could try to invent some clever separators that "nobody is ever going to use in actual code", but even if it worked in practical applications it still would be wrong in principle. The only really safe way is to do it like MATCH does (it is not coincidence that MATCH uses "pattern, text" order.

PS This also shows that my idea of IFONLY directive, even though initially sounded interesting, is still not what I was looking for. I may have to stick to IFEXPR anyway.
Post 28 May 2017, 14:36
View user's profile Send private message Visit poster's website Reply with quote
zhak



Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak 28 May 2017, 17:34
I do it as

Code:
if lengthof `param > 0
. . .
end if 
    
Post 28 May 2017, 17:34
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.