Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why doesn't "if" work with NASM

Status
Not open for further replies.

Utako

Programmer
May 10, 2001
4
US
if z jmp exit ;if so, exit to DOS> prompt

Why doesn't this line of code work in NASM Assembler, yet is just fine using the A86 assembler?
 
what kind of command is if? John Fill
1c.bmp


ivfmd@mail.md
 
I don't know what you mean by what "kind" of command it is. But it's like

if (this is true) (do this)

Now why doesn't it work in NASM but does in A86? Does NASM just not support such a thing or does it work differently?
 
Are you shure if it is a command and not a directive? I don't heard about such assembler(machine) commands. John Fill
1c.bmp


ivfmd@mail.md
 
Oh, hmm. I don't want to seem ignorant, but what is a directive? Is it an assembler specific command? Because if that's what a directive is, this might explain my problem. I didn't know assemblers had their own exclusive commands.
 
I think what John is trying to say is that "If" is not part of the Intel x86 Instruction set. The phrase "if (this is true)(do this)" sound like it comes from a higher level language like C++ or VB and not assembly. Normally in assembly, you would compare to values using one of the compare command like CMP and then jumping to a label/address based upon how the comparision set the flags in the CPU.
What I just described (and its translation) might look something like this . . .


Code:
     MOV AX,SomeData1
     MOV BX,SomeData2
     CMP AX,BX
     JE  Exit
 
if SomeData1 = someData2 then goto exit
- Jeff Marler B-)
 
Actually this is pure Assembly. I thought A86 was an x86 assembler... well, it must be otherwise it wouldn't even run on my machine, now would it. no, it isn't an external operation, pure ASM code for A86. Now, I'll post all of my code in here, and maybe someone can tell me why it doesn't compile with NASM. For those who are just interested, it's also commented:


start: mov ds,cs ;data segment same as code segment
jmp goahd ;go ahead jump over defines no1-no2

no1: db 'PRESS THE ESC KEY AT ANY TIME TO EXIT TO THE DOS> PROMPT',0
no2: db 'Hello World!',0 ;no1 & no2 = zero terminated strings

goahd: mov ax,3 ;reset video to text mode
int 10h ;to clear the screen
call cursof ;turn off the cursor
mov es,0b800h ;text mode video segment address
call newpaj ;set video page bright white on blue
mov di,no1 ;ESC key message above
mov si,1460 ;line 10 on video display
call display ;display it
mov di,no2 ;Hello World! message above
mov si,2138 ;line 14 on video display
call display ;display it

await: mov ah,0 ;wait for keyboard press
int 16h ;interrupt 16h returns key value
cmp ah,1 ;ESC key pressed ?
if z jmp exit ;if so, exit to DOS> prompt
jmp await ;wait for ESC key to exit

newpaj: pusha ;save most all regs on stack
mov cx,2000 ;num bytes per screen - add x 2
mov ah,31 ;bright white text on blue background
mov al,20h ;= ASCII space to erase anything
mov di,0 ;top of video address
b2: mov es:[di],ax ;display it
add di,2 ;next video location
loop b2 ;continue till cx zero
popa ;restore most all regs from stack
ret ;all done

exit: call curson ;restore the cursor
mov ax,3 ;reset video text mode =
int 10h ;clear the screen
mov ax,4c00h ;exit pointer and
int 21h ;interrupt 21h returns to DOS> prompt

display: mov al,[di] ;get ASCII byte to display
cmp al,0 ;zero terminated string = the end ?
if z ret ;if so return after call if all done
mov ah,31 ;31 decimal = blue background + bright white letters
inc di ;next byte to display
mov es:[si],ax ;display byte and attribute on video
add si,2 ;requires 2 bytes
jmp display ;go do next one

curson: mov cx,11 ;cursor video bios display
curs: mov bh,0 ;this part
mov ah,1 ;of video
int 10h ;do it
ret ;return to instruction after call
cursof: mov cx,0e00h ;cursor video move out of sight
jmp curs
 
The if z instruction above shown that the result is from compare instruction.

cmp al,0
if z ret

This means that if al=0, z is true then return.
 
The if z instruction above shown that the result is from compare instruction.

cmp al,0
if z ret

This means that if al=0, z is true then return. This instruction also works with TASM.
 
Try this macro for numeric comparison:

%imacro if 3
cmp %1, %2
je %3
%endmacro

mov al, 0x13
if al 0x13 equal ;loop forever
jmp end ;else end

equal:
jmp equal

end:
mov ax, 0x4C00
int 0x21
 
You can use a High-Level flow Directive in MASM. It would look something like this:

.IF(AL<10)
add al,1
.ELSEIF(AL<16)
add al,2
.ELSE
mov al,3
.ENDIF

This won't work with NASM as far as I'm aware. You would have to do some form of a JUMP instruction based on a CMP instruction. That would look something like this:

cmp al,0dh
jne Cont
jmp Found

Cont:
...

Found:
...

Does this help any?
 
If you want to branch if the zero flag set, use jz instead of &quot;if z jmp ...&quot;

Look up your conditional jump instructions, such as jc (jump if carry), je (jump if equal), jp(jump on parity), etc.
 
Hey sorry so late with this tip but here is an excellent resource you might find useful: this should point you to Issue 2 where you'll find Gij's &quot;A Guide to NASM for TASM Coders.&quot; You might also look through mammon_'s &quot;Using the GNU AS (gas) Assembler&quot; where conditional assembly is discussed...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top