News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

why does this loop? small sample code included

Started by drarem, March 10, 2005, 06:21:04 PM

Previous topic - Next topic

drarem

why does this loop?


mainloop:
xor ebx,ebx
loopA:
cmp ecx,dwcnt
jg dropdis        ;does not jump, ecx is way less then dwcnt
cmp bl,255
jg mainloop     ;however this does, is it a flag setting?

drarem

I went ahead and used CMP EBX,255 and it works.

QvasiModo

Since you're comparing BL (one byte) and usign a signed conditional jump, you have to regard 0xFF (255 decimal) as -1 in two's complement notation. So your program was actually comparing 0 to -1, instead of 255 as intended.

Hope that helps! :)

farrier

#3
drarem,

In your line:

cmp bl,255

the byte value 255 is considered a negative number; most significant bit is set.  And in the next line you choose to use "jg" which is used to compare signed values.  This means if either value can be considered signed, it will be.

For unsigned comparisons--where you don't want either value to be considered signed--use:

jb, jbe, ja, jae    They test the Carry flag

and for signed comparisons use:

jl, jle, jg, jge     They test the Sign & Overflow flags

hth,

farrier
It is a GOOD day to code!
Some assembly required!
ASM me!
With every mistake, we must surely be learning. (George...Bush)

QvasiModo

Also note that an unsigned comparison of a byte value with 255 will always have the same result, no matter what value it is. For example, cmp bl,255 / ja mainloop behaves just as jmp mainloop. That's because unsigned byte values can only be in the range 0-255.

drarem

THhanks qvasimodo and farrier, the explanations help.