flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > doif macro with curly bracket

Author
Thread Post new topic Reply to topic
Jin X



Joined: 06 Mar 2004
Posts: 133
Location: Russia
Jin X 11 Nov 2023, 14:09
I want to do a macro which will process the code in curly brackets only when condition is met.
Code:
macro doif cond
{
  local x, y, z
  x equ cond
  z equ 0
  if cond
    z equ 1  ; <-- how to do this???
  end if
  rept z
}

doif 1 = 0
{
  ret
}    
I know that z equ 1 is processed before if statement.
But I don't know how to set z to 1 ONLY if condition is met.
I tried to call macro inside if cond, tried to use repeat like this:
Code:
  if cond
    y = 1
  end if
  repeat y
    z equ 1
  end repeat
    
But all of this don't work Sad


And the second problem.
I want to process macro call like this:
Code:
doif 1 = 0 {
  ret
}    
I tried to implement match and irps but didn't got the result Sad
Post 11 Nov 2023, 14:09
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20298
Location: In your JS exploiting you and your system
revolution 11 Nov 2023, 14:12
Use =
Code:
z = 1    
And eliminate the curly brackets. You can only do assembler pass condition detection after the macro pass.

Otherwise if your constants are not variables but are defines (or equ) then use match.
Post 11 Nov 2023, 14:12
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 11 Nov 2023, 14:38
revolution wrote:
Use =
Code:
z = 1    
rept can't get symbols defined by = (invalid value).

I didn't understand the second part of your answer :-/
What curly brackets should I remove?

How to detect { char in the end of macro parameter?
This works (with fixed rept parameter):
Code:
macro doif cond*
{
  local @cond
  @cond equ cond
  match a \\{, cond \{
    @cond equ a
    display 'yes!',10
  \}
  if @cond
    display 'true'
  else
    display 'false'
  end if
  rept 1 \{
}

doif 1 = 1 \\{
  ret
}
    
But I don't want to escape bracket by 2 backslashes each time when calling: \\{
Post 11 Nov 2023, 14:38
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20298
Location: In your JS exploiting you and your system
revolution 11 Nov 2023, 14:49
Maybe like this.
Code:
macro doif val0, val1 {
        match =val0,val1
}

doif 0,1
{ display '-',10 }
doif 1,1
{ display '+',10 }    
Post 11 Nov 2023, 14:49
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 11 Nov 2023, 15:28
revolution wrote:
Maybe like this.
I want to use it like this:
Code:
@if x and y > z {
  ...
}
@elif x and y = z & b {
  ...
}
@else {
  ...
}    
Post 11 Nov 2023, 15:28
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20298
Location: In your JS exploiting you and your system
revolution 11 Nov 2023, 22:30
What are x, y and z? Are they assembler variables assigned with =? If they are then what you want to do is impossible. The preprocessor has no insight into the values of assembler time variables.
Post 11 Nov 2023, 22:30
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: 20298
Location: In your JS exploiting you and your system
revolution 12 Nov 2023, 02:36
To show the problem, consider the simplest case.
Code:
x = 5
rept x { display '.' }    
Code:
flat assembler  version 1.73.31  (16384 kilobytes memory)
test.asm [2]:
rept x { display '.' }
error: invalid value.    
The preprocessor only sees x, it doesn't know that the assembler will later assign x the value of 5.

Compare to:
Code:
x equ 5
rept x { display '.' }    
Code:
flat assembler  version 1.73.31  (16384 kilobytes memory)
.....
1 passes, 0 bytes.    
Now it works, because the preprocessor substitutes 5 for x.

Now note the problem with this type of approach:
Code:
x equ 5
x = 3    
Code:
flat assembler  version 1.73.31  (16384 kilobytes memory)
test.asm [2]:
x = 3
processed: 5=3
error: reserved word used as symbol.    
The preprocessor replaces all instances of x with 5, and the assembler now sees 5 = 3, instead of x = 3.
Post 12 Nov 2023, 02:36
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 12 Nov 2023, 11:19
I know about this but I'm looking for some hack Smile
Maybe some assembling-time construction that can take assembler variable. Or some macro-if-equ-match-smthelse combination that will help to allow to achieve the goal Smile
Post 12 Nov 2023, 11:19
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20298
Location: In your JS exploiting you and your system
revolution 12 Nov 2023, 11:22
Please specify whether x, y and z are assembler variables or preprocessor defines.

That makes a difference to the code you can write.
Post 12 Nov 2023, 11:22
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 12 Nov 2023, 11:57
revolution wrote:
Please specify whether x, y and z are assembler variables or preprocessor defines.
Anything. Let's look at both possibility.
Post 12 Nov 2023, 11:57
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20298
Location: In your JS exploiting you and your system
revolution 12 Nov 2023, 12:04
I mentioned above with assembler variables you can't mix them with curly brackets. So if you want x, y, and z to be "anything" then it can't ever work.

I you really need to use curly brackets, for some reason, then you have to use preprocessor defines. These are the rules of fasm and how it works internally.

If you remove your criterion for curly brackets then using if/else/end if will work perfectly fine for almost all circumstances. It is more flexible and more clear about what it is doing.
Post 12 Nov 2023, 12:04
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1617
Location: Toronto, Canada
AsmGuru62 12 Nov 2023, 13:26
I also did not get the part why the standard FASM constructs are to be replaced by {}.
If typing the .endif or .elseif is a little annoying -- and I can accept that as a fact -- it annoyed me too.
Until I started to use a better text editor (or find an IDE), which allows to insert the prepared code fragments.

There are some nice fragments to use in FASM.

IF() statements:
Code:
.if ()
.endif
    

Code:
.if (CARRY?)
.endif
    

Full IF() statement:
Code:
.if ()
.else
.endif
    

switch() statement:
Code:
.if ()
.elseif ()
.elseif ()
.elseif ()
.else
.endif
    

Loops of all kinds:
Code:
.repeat

    sub     ecx, 1
.until (ZERO?)
    

Code:
.while (ecx)

    sub     ecx, 1
.endw
    

A function:
Code:
proc NONAME uses reg reg reg, param, param, param
    local   loc1:DWORD


    ret
endp
    

Once your IDE or editor is set up with fragments -- you can insert those with just a few mouse clicks.
Post 12 Nov 2023, 13:26
View user's profile Send private message Send e-mail Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 669
Location: Ukraine
Overclick 12 Nov 2023, 14:13
Post 12 Nov 2023, 14:13
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: 20298
Location: In your JS exploiting you and your system
revolution 12 Nov 2023, 14:30
To those advocating .if and the like, please note that those are very different from if.

The .if macro variants place code into the output that evaluates things at runtime.
The if native assembler construct evaluates things at assembly time and omits code from the output.

So they are not the same, and are not replacements for each other.

The OP was describing constructs similar to if.
Post 12 Nov 2023, 14:30
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1617
Location: Toronto, Canada
AsmGuru62 12 Nov 2023, 17:03
I see. Indeed, the conditional assembly would use "if" and "end if", but my point was why replace those with a "{", "}"?
Just because typing those will be too time consuming or is there a "beauty of code" involved somehow?
The good editor or IDE will help with that in the same way I described.
Post 12 Nov 2023, 17:03
View user's profile Send private message Send e-mail 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.