;THE SUBROUTINES ALWAYS SAVE THE CONTENTS OF REGISTER A ;D E H L AND THE STATUS FLAGS. IF THE CONTENTS OF REGISTERS ;B C NEED TO BE SAVED THE CALLING ROUTINE MUST EXPLICITLY DO IT. ;******************BINARY TO BCD CONVERSION SUBROUTINE****************** ;THE SUBROUTINE CONVERTS A MULTI-BYTE BINARY NUMBER TO ITS EQUIVALENT ;MULTI-BYTE BINARY CODED DECIMAL (BCD) REPRESENTATION. THE BYTES ;OF BOTH NUMBERS ARE STORED IN MEMORY IN ASCENDING ORDER WITH ;THE LEAST SIGNIFICANT BYTE IN THE LOW END OF THE MEMORY STACK. THE ;REQUIRED PARAMETERS NEEDED TO TO BE PASSED ARE: ; H L GETS ADDRESS OF LOW ORDER BYTE OF BINARY NUMBER ; D GETS NUMBER OF BYTES IN BINARY NUMBER ; E GETS NUMBER OF BYTES IN BCD RESULT AREA ;THE SUBROUTINE ASSUMES THAT THE BINARY NUMBER WILL FIT IN THE SUPPLIED ;NUMBER OF BCD BYTES. THE ROUTINE WILL FILL IN THE BCD BYTES WITH ;LEADING ZEROS IF NECESSARY. bnbc: push af ; SAVE STATUS ld c,e ; COPY # BCD BYTES TO C push hl ; SAVE ADDR OF BINARY # ON STACK ld hl,bcdnl ; LOAD HL WITH ADDR OF BCD lab16: ld (hl),0 ; ZERO OUT MEMORY LOCATION inc hl ; INCREMENT HL dec c ; DECREMENT C jp nz,lab16 ; IF C=0 BRANCH FROM LOOP ld a,d ; PUT # OF BINARY BYTES IN A add a,a add a,a ; MULTIPLY BY 8 TO GET # OF BITS add a,a ld b,a ; STORE # OF BITS IN B xor a ; SET CARRY TO ZERO mloop: ld c,d ; PUT # OF BINARY BYTES IN C pop hl ; RESTORE ADDR OF BINARY # TO HL dec sp dec sp ; LEAVE HL ON STACK rloop: ld a,(hl) ; PUT BINARY BYTE IN A adc a,a ; SHIFT BYTE LEFT & FILL WITH CARRY ld (hl),a ; REPLACE BYTE dec c ; DECREMENT C jp z,lab17 ; BRANCH FROM LOOP IF LAST BYTE inc hl ; INCREMENT HL jp rloop ; JUMP TO START OF ROTATE LOOP lab17: pop hl ; RESTORE HL POINTER TO LOW ORDER BINARY BYTE dec sp dec sp ; LEAVE HL ON STACK jp nc,lab18 ; SKIP FILL WITH ONE IF NO CARRY inc (hl) ; FILL WITH ONE lab18: ld c,e ; PUT # OF BCD IN C ld hl,bcdnl ; LOAD HL WITH ADDR OF BCD lab20: ld a,0 ; ZERO OUT A adc a,(hl) ; ADD BCD BYTE TO A WITH CARRY daa ; CONVERT RESULT TO BCD ld (hl),a ; SAVE NEW BCD BYTE jp nc,lab19 ; SKIP REST IF NO CARRY inc hl ; POINT TO NEXT BCD BYTE dec c ; DECREMENT C jp nz,lab20 ; IF C=0 LEAVE LOOP lab19: ld c,e ; PUT # OF BCD BYTE IN C dec b ; DECREMENT BIT COUNT jp z,lab40 ; EXIT MAIN LOOP IF COUNT = 0 ld hl,bcdnl ; LOAD HL WITH ADDR OF BCD ; CARRY=0 ENTERING NEXT LOOP WHICH ; MULTIPLIES BCD # BY TWO lab21: ld a,(hl) ; PUT BCD BYTE IN A adc a,(hl) ; ADD BCD BYTE TO ITSELF WITH CARRY daa ; CONVERT RESULT TO BCD ld (hl),a ; SAVE NEW BCD BYTE inc hl ; POINT TO NEXT BCD BYTE dec c ; DECREMENT C jp nz,lab21 ; IF C=0 LEAVE LOOP jp mloop ; DO FOR ALL BITS OF BINARY # lab40: pop hl ; RESTORE HL pop af ; RESTORE STATUS ret ; RETURN ;******************BCD TO ASCII SUBROUTINE****************** ;THE SUBROUTINE TAKES A TWO DIGIT BCD NUMBER IN MEMORY POINTED TO BY ;HL AND CONVERTS IT TO ASCII. THE MOST SIGNIFICANT DIGIT IS PUT ;IN REGISTER B WHILE THE LEAST SIGNIFICANT DIGIT IS PUT IN REGISTER C. ascii: push af ; SAVE STATUS ld a,00001111b ; PUT LEAST SIGNIFICANT DIGIT MASK IN A and (hl) ; MASK OFF LEAST SIGNIFICANT DIGIT or 00110000b ; CONVERT TO ASCII ld c,a ; PUT IN REG C ld a,11110000b ; PUT MOST SIGNIFICANT DIGIT MASK IN A and (hl) ; MASK OFF MOST SIGNIFICANT DIGIT rrca rrca rrca rrca ; ROTATE RIGHT FOUR PLACES or 00110000b ; CONVERT TO ASCII ld b,a ; PUT IN REG B pop af ; RESTORE STATUS ret ; RETURN bcdnl: ds 31 ; START OF BCD NUMBER end