NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: MIchael on May 24, 2018, 12:11:01 PM

Title: Making If/Elseif/Else statement with macros
Post by: MIchael on May 24, 2018, 12:11:01 PM
Hi to all! After a long search on the Internet I couldn't solve my problem. How to implement If/Elseif/Else statement with macros, just like this:
Code: [Select]
if al, equal, bl
    mov rcx, 128
elseif al, greater, bl
    mov rcx, 1
else
    mov rcx, 0
endif
Title: Re: If/Elseif/Else statement with macros
Post by: avcaballero on May 24, 2018, 07:02:59 PM
Maybe you may have a look to the nasmx package. The win32\Demo15 use it:
Code: [Select]
if eax,ABOVE,[argv(.h)]
fdivp st1,st0  ; w/h
fld st0
fmul qword [var(.right)]
fxch
fmul qword [var(.left)]
fstp qword [var(.left)]
fstp qword [var(.right)]
else
fdivrp st1,st0 ; h/w
fld st0
fmul qword [var(.top)]
fxch
fmul qword [var(.bottom)]
fstp qword [var(.bottom)]
fstp qword [var(.top)]
endif
Title: Re: If/Elseif/Else statement with macros
Post by: MIchael on May 24, 2018, 10:11:50 PM
Hi to all! After a long search on the Internet I couldn't solve my problem. How to implement If/Elseif/Else statement with macros, just like this:
Code: [Select]
if al, equal, bl
    mov rcx, 128
elseif al, greater, bl
    mov rcx, 1
else
    mov rcx, 0
endif
Just like this?

%define equal ne
%define n_equal e
; you can add more definitions there

%macro if 3
    %push if
    cmp %1, %3
    j%2 %$ifnot
%endmacro

%macro elseif 3
    %ifctx if
%$ifnot:
        cmp %1, %3
        j%2 %$ifnot
    %else
       %error "ELSIF directive must be within an 'if' block."
    %endif
%endmacro
   
%macro else 0
    %ifctx if
        %repl else
        jmp %$ifend
%$ifnot:
    %else
        %error "ELSE directive must be within an 'if' or 'elseif' block."
    %endif
%endmacro
   
%macro endif 0
    %ifctx if
%$ifnot:
        %pop
    %elifctx else
%$ifend:
        %pop
    %else
        %error "ENDIF directive must be within an 'if' block."
    %endif
%endmacro