Routine: Print
Function: Displays a floating point number
Called by: Calib, Main, Pb2, Pb3, Pc
Calls: Err, Mlt, Mult, Regld, Regsv
Entry: HL points to the number
Exit: Error 1: Print overflow, Error 2: Print negative attempted
Preserved: All
PRINT CALL REGSV
XOR A
LD (PRIOVR),A
LD (PRIEXP),A
; Priovr is a flag indicating print overflow, Priexp holds the decimal exponent of the number, Pribuf is a 7 byte temporary storage space.
LD BC,5
LD DE,PRIBUF
LDIR
; Transfer the number into Pribuf.
LD IY,PRIEXP
LD A,(PRIBUF+3)
RL A
; Check the number is positive, if not jump to Prin21, to signal error 2.
JP C,PRIN21
LD A,(PRIBUF+4)
CP 128
; Investigate size of the number: if less than 1, call Prin15; if greater than 1, call Prin14. These routines reduce the magnitude of the binary exponent.
CALL C,PRIN14
CALL NC,PRIN15
LD IX,PRIBUF
SET 7,(IX+3)
OR A
JR Z,PRIN2
LD B,A
; Multiply by two by shifting right, until the binary exponent becomes zero. This completes the binary to decimal conversion.
PRIN1 SRL (IX+3)
RR (IX+2)
RR (IX+1)
RR (IX)
DJNZ PRIN1
PRIN2 LD IX,MLTBUF
LD HL,PRIBUF
LD BC,4
LD DE,MLTBUF
LDIR
; Move the number to mltbuf.
LD B,7
LD IY,PRIBUF
; The next part converts the binary fraction to decimal by repeatedly multiplying by ten and taking the integer part, until seven decimal digits have been built up in Pribuf.
PRIN3 LD A,10
LD (IX+4),A
XOR A
LD (IX+5),A
LD (IX+6),A
LD (IX+7),A
; Put 10 in Mltbuf 4-7
PUSH BC
CALL MLT
POP BC
LD A,(IX+12)
LD (IY),A
; Store integer part in Pribuf and move the result back into Mltbuf 0-3.
INC IY
LD HL,(MLTBUF+8)
LD (MLTBUF),HL
LD HL,(MLTBUF+10)
LD (MLTBUF+2),HL
DJNZ PRIN3
LD A,(PRIEXP)
DEC A
CP 127
; Investigate the decimal exponent; if the number is less than 1, call Prin9, if greater than 1, continue at Prin4.
JR C,PRIN4
CALL PRIN9
LD A,(PRIOVR)
OR A
JR Z,PRIN5
; If the number is less than 0.00001, then just print '0.' and return, otherwise jump to prin5.
LD A,17
OUT (8),A
LD A,32
OUT (144),A
JR PRINZ
PRIN4 CP 6
; If the decimal exponent is too big to display then jump to Prin17 to indicate error 1.
JP NC,PRIN17
CALL PRIN18
LD A,(PRIOVR)
OR A
; Jump to Prin17 in the event of an overflow.
JP NZ,PRIN17
PRIN5 LD A,255
PUSH AF
; Next shift out all the leading zeros, using the A register to hold the information to be sent to the digit blanking register, Out 144.
PRIN6 LD A,(PRIBUF+5)
OR A
JR NZ,PRIN7
LD BC,5
LD HL,PRIBUF+4
LD DE,PRIBUF+5
LDDR
POP AF
SLA A
PUSH AF
JR PRIN6
PRIN7 LD B,6
LD C,13
LD HL,PRIBUF
; Output the contents of Pribuf to the displays, and set the digit blanking register, Out 144, appropriately.
PRIN8 LD A,(HL)
OUT (C),A
DEC C
INC HL
DJNZ PRIN8
POP AF
OUT (144),A
; Jump to the routine end.
JR PRINZ
PRIN9 LD IX,PRIBUF+5
; First round the number by checking the least significant digit, and if greater than or equal to 5, incrementing the last digit of the displayed number.
LD A,(IX+1)
CP 5
JR C,PRIN11
PRIN10 LD A,(IX)
INC A
LD (IX),A
CP 10
JR NZ,PRIN11
XOR A
LD (IX),A
DEC IX
JR PRIN10
PRIN11 LD HL,PRIBUF
LD A,(PRIEXP)
DEC A
; Create appropriate leading zeros by shifting the number right, until the exponent becomes zero.
PRIN12 OR A
JR Z,PRIN13
LD BC,5
LD HL,PRIBUF+4
LD DE,PRIBUF+5
LDDR
PUSH AF
XOR A
LD (PRIBUF),A
POP AF
INC A
JR PRIN12
PRIN13 LD A,(PRIBUF)
; Insert a decimal point after the leftermost zero, then return.
OR 16
LD (PRIBUF),A
RET
PRIN14 LD A,(PRIBUF+4)
; Multiply the number by a tenth until it is less than one, i.e the integer part is reduced to zero; for each multiplication increment the decimal exponent.
LD B,A
XOR A
SUB B
CP 4
RET C
LD BC,TENTH
LD DE,PRIBUF
LD HL,PRIBUF
CALL MULT
INC (IY)
JR PRIN14
PRIN15 LD A,(PRIBUF+4)
; Multiply the number by ten until any futher multiplications would make it greater than one; for each multiplication, decrement the decimal exponent.
CP 252
JR C,PRIN16
LD B,A
XOR A
SUB B
RET
PRIN16 LD BC,TEN
LD DE,PRIBUF
LD HL,PRIBUF
CALL MULT
DEC (IY)
JR PRIN15
PRIN17 LD A,1
; Signal print overflow error.
CALL ERR
JR PRINZ
; Subroutine Prin18 rounds the number, by checking the least significant digit and if greater than or equal to 5, incrementing the last digit of the displayed number.
PRIN18 LD IX,PRIBUF+5
LD A,(IX+1)
CP 5
JR C,PRIN20
PRIN19 LD A,(IX)
INC A
LD (IX),A
CP 10
JR NZ,PRIN20
; If incrementing the last digit resulted in a number > 10, it must be reset and the next digit incremented, and so on until no more overflows occur,
XOR A
LD (IX),A
DEC IX
JR PRIN19
PRIN20 LD HL,PRIBUF
LD A,(PRIEXP)
DEC A
; Calculate position of the decimal point in the print buffer and inser it by Or 16.
LD E,A
LD D,0
ADD HL,DE
LD A,16
OR (HL)
LD (HL),A
RET
PRIN21 LD A,2
; Signal print negative error.
CALL ERR
PRINZ CALL REGLD
; Terminate Print routine.
RET