; Copyright (C) Reston Publishing Company Inc. 1983 ; #start HexDisplay -------------------------------------------- ;--------------------------------------------------------------- ; HexDisplayL: make A the display of the left hex digit of A. ; preserves -- BC, DE, HL ;--------------------------------------------------------------- HexDisplayL: rar rar rar rar ;--------------------------------------------------------------- ; HexDisplayR: make A the display of the right hex digit of A. ; preserves -- BC, DE, HL ; Note: the code is taken from William Barden's column in "80 ; Microcomputing," August 1981. ;--------------------------------------------------------------- HexDisplayR: ani 0Fh adi 90h daa aci 40h daa ret ; #end HexDisplay ; #start UnpackBCD --------------------------------------------- ; UnpackBCD: convert the positive binary number in HL into five ; packed BCD digits in the C, D, and E registers. ; preserves -- AF, B, HL ; returns -- C,D,E contain 0d,dd,dd -- BCD digits ; Note: the code is after a routine from Dr. Dobb's Journal, ; #60, October 1981. ;--------------------------------------------------------------- UnpackBCD: push h push b ; preserving only B -- see exit push psw ; lxi b,(16 shl 8) ; B:=loop count, C:=0 mov d,c mov e,d ; zero D, E also Unpack3: dad h ; next bit of HL to carry, add mov a,e ; ..it to doubled partial result adc a daa mov e,a ; low-order 2 digits done mov a,d ! adc a ! daa ! mov d,a mov a,c ! adc a ! daa ! mov c,a DJNZ Unpack3 ; pop psw ; recover input A, flags, pop h mov b,h ; .. B register, pop h ; .. and HL ret ; #end UnpackBCD ; #start SRLDE ------------------------------------------------- ; SRLDE: shift-right-logical the DE register pair. ; preserves -- AF, BC, HL ;--------------------------------------------------------------- SRLDE: push psw mov a,d ora a ; clear carry, rar ; shift most sig. byte right mov d,a mov a,e rar ; least sig. byte receives mov e,a ; old D bit 0 as its bit 7 pop psw ret ; #end SRLDE ; #start CmpBD ------------------------------------------------- ; CmpBD: compare BC to DE as a 16-bit unsigned integer, setting ; the machine flags as for a CMP instruction. ; preserves -- all except the flags ;--------------------------------------------------------------- CmpBD: push h mov h,a mov a,b cmp d JRNZ CmpBDZ mov a,c cmp e CmpBDZ: mov a,h pop h ret ; #end CmpBD ; #start CmpDH ------------------------------------------------- ; CmpBD: compare DE to HL as a 16-bit unsigned integer, setting ; the machine flags as for a CMP instruction. ; preserves -- all except the flags ;--------------------------------------------------------------- CmpDH: push b mov b,a mov a,d cmp h JRNZ CmpDHZ mov a,e cmp l CmpDHZ: mov a,b pop b ret ; #end CmpDH ; #start Div16by8 ---------------------------------------------- ; Div16by8: Divide the 8-bit unsigned number in A into the ; 16-bit unsigned number in HL. The quotient is returned in ; HL, and the remainder in A. ; ; The code is based on "Dr. Dobb's Clinic", Doctor Dobb's ; Journal #60, October 1981. ;-------------------------------------------------------------- Div16by8: push b mvi b,16 ; iteration count in b mov c,a ; divisor in c xra a ; a begins at zero Div16by8Loop: dad h ; shift-left HL ral ; high bit of HL to low bit of A cmp c ; partial remainder up to divisor? JRC Div16by8Skip ; (no) sub c ; yes, reduce by divisor inr l ; and set low bit of quotient Div16by8Skip: DJNZ Div16by8Loop pop b ret ; #end Div16by8 ; #start DecToBinary ------------------------------------------- ; ; DecToBinary: convert a digit string from HL-->text to a binary ; integer in DE. The first nondigit seen stops the conversion. ; Supports only positive integers, signs are not recognized. ; preserves -- A, Flags, BC ; output -- HL advanced to a non-digit ; DE contains a binary integer ;--------------------------------------------------------------- DecToBinary: push psw push b ; lxi d,0 ; partial product begins 0000h push d ; ..and is kept on stack ; DecToBloop: mov a,m ; next digit, maybe sui '0' ; ..convert it and see JRC DecToBdone ; (no, less than "0") cpi 9+1 JRNC DecToBdone ; (no, greater than "9") ; mov e,a ; DE = 000d xthl ; HL = partial product mov b,h ! mov c,l ; BC has a copy dad h ; times 2 dad h ; times 4 dad b ; times 5 dad h ; times 10 dad d ; plus digit xthl ; partial back to stack inx h ; on to next digit JMPR DecToBloop ; DecToBdone: pop d ; DE = result pop b pop psw ret ; #end DecToBinary nx h ; on to next digit JMPR DecToBloop ; DecToBdone: pop d ; DE = result