16bit signed comparisons in SX/B and asm
by Perter Verkaik
' =========================================================================
'
' File...... wordcomparing.SXB
' Purpose... Demonstrates comparing words (16bit register values)
'
' =========================================================================
' -------------------------------------------------------------------------
' Device Settings
' -------------------------------------------------------------------------
DEVICE SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
FREQ 4_000_000
ID "CMP16"
' -------------------------------------------------------------------------
' IO Pins
' -------------------------------------------------------------------------
' -------------------------------------------------------------------------
' Constants
' -------------------------------------------------------------------------
' unsigned test actual unsigned results
' X = Y X < Y X > Y
' X = Y b0 True False False
' X < Y b1 False True False
' X <= Y b2 True True False
' X > Y b3 False False True
' X >= Y b4 True False True
' X <> Y b5 False True True
'
' signed test if unsigned X < Y
' if X>=0 AND Y<0 then use column for X > Y
' signed test if unsigned X > Y
' if X<0 AND Y>=0 then use column for X < Y
' the above results in:
' signed test if unsigned X <> Y
' if sign(X) <> sign(Y) then swap column
'unsigned test attributes
uEQ con 1
uLT con 2
uLE con 4
uGT con 8
uGE con 16
uNE con 32
'signed test attributes
EQ con 128+1
LT con 128+2
LE con 128+4
GT con 128+8
GE con 128+16
NE con 128+32
'unsigned compare flags
EQflags con %00010101
LTflags con %00100110
GTflags con %00111000
XORvalue con %00011110 'to swap LTflags and GTflags for some signed tests
true con 1
false con 0
' -------------------------------------------------------------------------
' Variables
' -------------------------------------------------------------------------
ramX VAR Word ' 16-bit value
ramY VAR Word ' 16-bit value
attrib VAR Byte ' compare attribute
result VAR Word
flags VAR Byte
' =========================================================================
PROGRAM Start
' =========================================================================
' -------------------------------------------------------------------------
' Subroutine Declarations
' -------------------------------------------------------------------------
' -------------------------------------------------------------------------
' Program Code
' -------------------------------------------------------------------------
Start:
'assume ramX, ramY and attrib are set
'at the end result holds true or false based on X and Y values and the test to perform
'get unsigned compare flags
if ramX = ramY then
flags = EQflags
else
if ramX < ramY then
flags = LTflags
else
flags = GTflags
endif
endif
'if signed compare then adjust compare flags
if attrib.7 = 1 then
'if ramX <> ramY
if flags.0 = 0 then
result_MSB = ramX_MSB xor ramY_MSB
'if ramX and ramY have different signs
if result.15 = 1 then
flags = flags xor XORvalue 'swap LTflags and GTflags
endif
endif
endif
result = 0
'extract flag for test
flags = flags and attrib
'set result based on flag
result.0 = ~Z 'if flags=0 (Z set) then result=false
ramX=$FF01
ramY=$FF00
attrib = LT
gosub doTest
END
asm
getUnsignedCompareFlags:
mov w,ramX_LSB
mov w,ramY_LSB-w
mov result_LSB,w
mov w,ramX_MSB
sb C ;skip if no borrow
movsz w,++ramX_MSB
mov w,ramY_MSB-w
or w,result_LSB
snz
retw EQflags ;ramX = ramY
snb C
retw LTflags ;ramX < ramY
retw GTflags ;ramX > ramY
;assume ramX, ramY and attrib set
doTest:
call getUnsignedCompareFlags
mov flags,w
jnb attrib.7,test2
jb flags.0,test2
mov w,ramX_MSB
xor w,ramY_MSB
and w,#$80
mov w,#XORvalue
sz
xor flags,w
test2:
clr result_LSB
mov w,attrib
and flags,w
sz
inc result_LSB
clr result_MSB
ret
endasm
' -------------------------------------------------------------------------
' Subroutine Code
' -------------------------------------------------------------------------