NASM Forum > Programming with NASM
problem in using %if
(1/1)
nobody:
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 ?
Frank Kotler:
"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:
Yeah, got it! Thanks :)
Navigation
[0] Message Index
Go to full version