Author Topic: problem in using %if  (Read 18039 times)

nobody

  • Guest
problem in using %if
« on: August 06, 2008, 09:59:39 AM »
HI,I have a problem about %if...
 here is my programme(helloworld.asm):

extern printf
[SECTION .data]
        msg1 db "hello world", 0xa, 0
        msg2 db "wrong",0xa, 0
        num  db 1
[SECTION .text]
        global main
main:
        %if num==1
                push dword msg1
        %else
                push dword msg2
        %endif
        call printf
        add esp, byte 4
return:
        mov eax, 0
        ret

It seems will print "hello world", but in fact , the result is "wrong" .
Is my programme wrong ?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: problem in using %if
« Reply #1 on: August 06, 2008, 02:49:26 PM »
"num" is a runtime variable, and "%if" tests preprocessor variables. If, instead of "num db 1", you did "num equ 1" or "%define num 1", it would work. If you intend to test the contents of a variable, you'll have to do it at runtime:

cmp byte [num], 1 ; note: "[num]", not "num"!
je it_is_one
...

If you want an "if" macro for that, you'll have to include a macro for it. Nasm64developer's flow-control macros available in the "contributions" section on the download page here should have an "if" macro (I think)...

Best,
Frank

nobody

  • Guest
Re: problem in using %if
« Reply #2 on: August 07, 2008, 01:27:57 AM »
Yeah, got it! Thanks :)