hey guys/gals

do you know how to determine of signed int (x) is less than signed int (y)

using binary arithmetic, ie shifting bits to the right/left xor-ing, etc ?

thanks :)
Posted on 2003-02-08 19:29:07 by darknocturnal
um... i don't see how arithmetic alone could test that... i would think you would need to do some compares, or at least bit tests.
Posted on 2003-02-08 19:57:25 by jademtech
what sort of bit tests would you do ?

im trying to stay away from using the comparative operators ( <= , <, >, >=)
Posted on 2003-02-08 20:09:21 by darknocturnal
what is wrong /w cmp eax,ebx? it performs a bunch of bit tests simultaneously and sets flags accordingly.

anyway, my solution (by no means optimal):

rol eax,1 ;sign bit is in bit 0
rol ebx,1

xor eax,ebx ;get mask of differences
jz Equal ;numbers are equal
test eax,1
jz @F ;signs are the same
test ebx,1
jz ebxGreater ;bit not set? then eax was negative and ebx was not.
jmp eaxGreater
@@:
rol eax,1
rol ebx,1
test eax,1
jz @B ;no bit differences here...
test ebx,1
jz eaxGreater ;eax contained a "1" in this position. eax was greater
;fall through to ebxGreater

ebxGreater:
;ebx is bigger

eaxGreater:
;eax is bigger

Equal:
;numbers are equal

or, you could:

xor eax,ebx
test eax,80000000h
jnz @F
test ebx,80000000h
jz ebxGreater
jmp eaxGreater
@@:
sub eax,ebx
jz Equal
jns eaxGreater
;fall through to ebxGreater
Posted on 2003-02-08 21:29:54 by jademtech

hey guys/gals

do you know how to determine of signed int (x) is less than signed int (y)

using binary arithmetic, ie shifting bits to the right/left xor-ing, etc ?

thanks :)

Do you mean to determine it without cmp or sub?
Posted on 2003-02-08 22:31:13 by The Svin
yup, like a generic algorithm, using binary arithmetic?

essentially, what i'm trying to do is implement a "less than" operator...

so the more generic it is, the better...

pseudo-code even....
Posted on 2003-02-08 23:25:43 by darknocturnal
I stole the schematic for an adder from here!

``````
; eax = A
; ebx = B

neg ebx ; Negate B because we want to subtract rather than add...

mov esi, eax
xor esi, ebx ; esi now holds all 32 bits of the first XOR

mov edi, eax
and edi, ebx
not edi      ; edi now holds all 32 bits of the first NAND

xor ebx, ebx
mov bh, 32

@@:
; bl = Cin

xor eax, eax
shr esi, 1
setc al      ; al = result of the first XOR

shr edi, 1
setc ah      ; ah = result of the first NAND

mov dl, al
xor dl, bl   ; second XOR, (A XOR B) XOR Cin

shr dl, 1
rcl ecx, 1   ; Add bit to SUM...

mov dh, al
and dh, bl
not dh       ; second NAND, (A XOR B) NAND Cin
and dh, 1    ; mask out unused bits...

and dh, ah
not dh       ; third NAND, ((A XOR B) NAND Cin) NAND (A NAND B)
and dh, 1    ; mask out unused bits...

mov bl, dh   ; move back to bl, as this is the Cin for the next itteration.

dec bh
jnz @B

; ecx is now the result, although the bits are in the reverse order!
; bit 31 = bit 0...
; bl = carry

; test bit 0 - this is bit 31 of the real number!
; and hence the sign bit...
``````

Mirno
Posted on 2003-02-09 06:18:51 by Mirno
thanks guys! :)

your help has been very useful..
Posted on 2003-02-10 09:11:03 by darknocturnal
mov eax,"int x"
jnb eax,"int y"
Posted on 2003-02-10 12:38:14 by mrgone
jnb takes two operands?!
Posted on 2003-02-10 19:17:22 by comrade