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 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 :)

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.

what sort of bit tests would you do ?

im trying to stay away from using the comparative operators ( <= , <, >, >=)

im trying to stay away from using the comparative operators ( <= , <, >, >=)

ARGH. my response got cleared during posting (thread not found?)

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

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

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?

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....

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

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

pseudo-code even....

I stole the schematic for an adder from here!

Mirno

```
```

; 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

thanks guys! :)

your help has been very useful..

your help has been very useful..

mov eax,"int x"

jnb eax,"int y"

jnb eax,"int y"

jnb takes two operands?!