flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Easier conditional jumps with .if and .endif macroses

Author
Thread Post new topic Reply to topic
Reverend



Joined: 24 Aug 2004
Posts: 408
Location: Poland
Reverend 09 Feb 2005, 21:58
I discovered a "for" macro by Privalov, and then it enlightened me suddendly Wink. I put here macroses to handle conditional jumps nearly as it was in masm. Check examples below:
Code:
; MACRO DEFINITIONS

        macro   .if     [var,comp,what] {
        common
                local   _condition_not_met

        forward
                if      var     in <eax,ebx,ecx,edx,esi,edi,ebp,esp>
                        if      what    eq 0
                                if      comp    eq jnz
                                        test    var,var
                                        jnz     _condition_not_met
                                else
                                        cmp     var,what
                                        comp    _condition_not_met
                                end if
                        else
                                cmp     var,what
                                comp    _condition_not_met
                        end if
                end if

        common
                _if     equ _condition_not_met
        }

        macro   .endif  {
                _if:

                restore _if
        }

        equal      fix ,jnz,
        more       fix ,jbe,
        less       fix ,jae,
        more_equal fix ,jb,
        less_equal fix ,ja,
    

Code:
; EXAMPLE CODE
                mov     eax,1
                mov     ebx,2
                mov     ecx,3
                mov     edx,4
                mov     esi,5

                .if     eax equal 1,\
                        ebx more 1,\
                        ecx less 5,\
                        edx more_equal 4,\
                        esi more_equal 6

                        inc ecx
                .endif
    

And now some explanation how it works. First of course we start .if and then we put a name of a variable. Next after space we must put a correct word. Here is a list comparing it with masm ones:

equal =
more >
less <
more_equal >=
less_equal <=

So when writing: ".if eax more_equal 100" it'd be in masm ".if eax >= 100". Unforutantely I couldn't use "<>=" chars, but even tough I think this macro is quite useful.

Also I must mention that this macro has some other powers. It checks given arguments for special case. I mean that if someone writes a code:
Code:
.if     eax equal 0
.endif
    

It gets assembled to:
Code:
test    eax,eax
jnz     blablabla
    

In other cases it's just cmp instruction. I was thinking about also one another case. When comparing a register to -1 we do not have to do cmp. It can be done like this:
Code:
inc     eax
jz      blablabla
dec    eax
    

If there was any interest in this macro I may add it (or even everyone can add it yourself, it's not so hard)

And on the end I must also say that this macro can check several conditions at once. Just use a coma between every of them, as in example listed above.

I'm waiting for your comments now Smile

P.S: Sorry for my english Embarassed
Post 09 Feb 2005, 21:58
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 09 Feb 2005, 22:03
Similar macros are already provided with the FASMW package in INCLUDE\MACRO\IF.INC file.
Post 09 Feb 2005, 22:03
View user's profile Send private message Visit poster's website Reply with quote
Reverend



Joined: 24 Aug 2004
Posts: 408
Location: Poland
Reverend 09 Feb 2005, 22:14
Yes I know there are similar macroses. But afaik they do not allow many conditions being checked in one statement (I mean in one line of code). Also my macro checks a special case, as I said above. And imho seeing ".if eax equal 100" in sourcecode is more readfriendly (? hehe. nice word Laughing) than ".if eax,z,100"

I forgot to say also before that my macro will correctly interpret nested .if:
Code:
                .if     eax equal 0
                        call    SOME_PROCEDURE
                        .if     edx more_equal 0
                                call    IF_ALL_CONDITIONS_WERE_MET
                        .endif
                .endif
    
Post 09 Feb 2005, 22:14
View user's profile Send private message Visit poster's website Reply with quote
Vasilev Vjacheslav



Joined: 11 Aug 2004
Posts: 392
Vasilev Vjacheslav 19 Feb 2005, 15:14
Reverend, i modify a bit your macro, but because i am a beginner in macroses i made many mistakes (i think). PLEASE, If somebody can - check for errors this code:

Code:
macro   .if [v1,c,v2]
{
  common
        local   __endif
        local   __else

  forward
        if      v1 in <eax,ebx,ecx,edx,esi,edi,ebp,esp> & v2 eq 0
                test    v1,v1
                jn#c    __else
        else
                cmp     v1,v2
                jn#c    __else
        end if

  common
        __pif    equ
        __pendif equ __endif
        __pelse  equ __else
}

macro .else
{
        jmp     __pendif
  __pelse:
        restore __pif
        __pif    equ ,
}

macro   .elseif [v1,c,v2]
{
  common
        local   __else

        jmp     __pendif
  __pelse:
        restore __else

  forward
        if      v1 in <eax,ebx,ecx,edx,esi,edi,ebp,esp> & v2 eq 0
                test    v1,v1
                jn#c    __else
        else
                cmp     v1,v2
                jn#c    __else
        end if

  common
        __pelse  equ __else
}

macro   .endif
{
        if      __pif eq
  __pelse:
        end if
  __pendif:
        restore __pelse
        restore __pendif
        restore __pif
}

jnnz  equ jz
jnne  equ je
jnna  equ ja
jnnb  equ jb
jnng  equ jg
jnnl  equ jl
jnnae equ jae
jnnbe equ jbe
jnnge equ jge
jnnle equ jle
    

_________________
[not enough memory]
Post 19 Feb 2005, 15:14
View user's profile Send private message Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost 20 May 2005, 09:55
haha, i made my own before i even knew if statements came with fasm as macros in the default package Mad

anyways both of yours are much better than mine, nj
Post 20 May 2005, 09:55
View user's profile Send private message AIM Address MSN Messenger Reply with quote
Reverend



Joined: 24 Aug 2004
Posts: 408
Location: Poland
Reverend 23 May 2005, 20:58
As the topic got renewed I may add something from me. Does anybody know the way to check what is the next instruction? Or is it possible at all? I mean when...:
Code:
.if     eax, a, 100
 add     eax, 100
 .if     eax, a, 300
  sub     eax, 50
 .endif
.endif    

When compiler builds such code it produces unnecessary jmps in place where two .endifs are next to other. It's two byte on every conditional check, and it also slows program a bit, when using it in some kind of inner loop of some big algorithm. So I ask, if there's a possibility to check whether the next instruction is .endif and if so, omit assembling unnecessary jump? Am I clear enough? Very Happy
Post 23 May 2005, 20:58
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.