Jameco Electronics 2000 Network Card User Manual


 
User’s Manual 37
The BOOL instruction is a special instruction designed to help test the HL register. BOOL
sets HL to the value 1 if HL is non zero, otherwise, if HL is zero its value is not changed.
The flags are set according to the result. BOOL can also operate on IX and IY.
BOOL HL ; set HL to 1 if non- zero, set flags to match HL
BOOL IX
BOOL IY
ALTD BOOL HL ; set HL’ an f’ according to HL
ALTD BOOL IY ; modify IY and set f’ with flags of result
The SBC instruction can be used in conjunction with the BOOL instruction for performing
comparisions. The SBC instruction subtracts one register from another and also subtracts
the carry bit. The carry out is inverted compared to the carry that would be expected if the
number subtracted was negated and added. The following examples illustrate the use of
the SBC and BOOL instructions.
; Test if HL>=DE - HL and DE unsigned numbers 0-65535
OR a ; clear carry
SBC HL,DE ; if C==0 then HL>=DE else if C==1 then HL<DE
; convert the carry bit into a boolean variable in HL
;
SBC HL,HL ; sets HL==0 if C==0, sets HL==0ffffh if C==1
BOOL HL ; HL==1 if C was set, otherwise HL==0
;
; convert not carry bit into boolean variable in HL
SBC HL,HL ; HL==0 if C==0 else HL==ffff if C=1
INC HL ; HL==1 if C==0 else HL==0 if C==1
; note carry flag set, but zero / sign flags reversed
In order to compare signed numbers using the SBC instruction, the programmer can map
the numbers into an equivalent set of unsigned numbers by inverting the sign bit of each
number before performing the comparison. This maps the most negative number 0x08000
to the smallest unsigned number 0x0000, and the most positive signed number 0x07FFF to
the largest unsigned number 0x0FFFF. Once the numbers have been converted, the com-
parision can be done as for unsigned numbers. This procedure is faster than using a jump
tree that requires testing the sign and overflow bits.
; example - test for HL>=DE where HL and DE are signed numbers
; invert sign bits on both
ADD HL,HL ; shift left
CCF ; invert carry
RR HL ; rotate right
RL DE
CCF
RR DE ; invert DE sign
SBC HL,DE ; no carry if HL>=DE
; generate boolean variable true if HL>=DE
SBC HL,HL ; zero if no carry else -1
INC HL ; 1 if no carry, else zero
BOOL ; use this instruction to set flags if needed