;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 PSW ;SAVE STATUS MOV C,E ;COPY # BCD BYTES TO C PUSH H ;SAVE ADDR OF BINARY # ON STACK LXI H,BCDNL ;LOAD HL WITH ADDR OF BCD LAB16: MVI M,0 ;ZERO OUT MEMORY LOCATION INX H ;INCREMENT HL DCR C ;DECREMENT C JNZ LAB16 ;IF C=0 BRANCH FROM LOOP MOV A,D ;PUT # OF BINARY BYTES IN A ADD A ADD A ;MULTIPLY BY 8 TO GET # OF BITS ADD A MOV B,A ;STORE # OF BITS IN B XRA A ;SET CARRY TO ZERO MLOOP: MOV C,D ;PUT # OF BINARY BYTES IN C POP H ;RESTORE ADDR OF BINARY # TO HL DCX SP DCX SP ;LEAVE HL ON STACK RLOOP: MOV A,M ;PUT BINARY BYTE IN A ADC A ;SHIFT BYTE LEFT & FILL WITH CARRY MOV M,A ;REPLACE BYTE DCR C ;DECREMENT C JZ LAB17 ;BRANCH FROM LOOP IF LAST BYTE INX H ;INCREMENT HL JMP RLOOP ;JUMP TO START OF ROTATE LOOP LAB17: POP H ;RESTORE HL POINTER TO LOW ORDER BINARY BYTE DCX SP DCX SP ;LEAVE HL ON STACK JNC LAB18 ;SKIP FILL WITH ONE IF NO CARRY INR M ;FILL WITH ONE LAB18: MOV C,E ;PUT # OF BCD IN C LXI H,BCDNL ;LOAD HL WITH ADDR OF BCD LAB20: MVI A,0 ;ZERO OUT A ADC M ;ADD BCD BYTE TO A WITH CARRY DAA ;CONVERT RESULT TO BCD MOV M,A ;SAVE NEW BCD BYTE JNC LAB19 ;SKIP REST IF NO CARRY INX H ;POINT TO NEXT BCD BYTE DCR C ;DECREMENT C JNZ LAB20 ;IF C=0 LEAVE LOOP LAB19: MOV C,E ;PUT # OF BCD BYTE IN C DCR B ;DECREMENT BIT COUNT JZ LAB40 ;EXIT MAIN LOOP IF COUNT = 0 LXI H,BCDNL ;LOAD HL WITH ADDR OF BCD ;CARRY=0 ENTERING NEXT LOOP WHICH ;MULTIPLIES BCD # BY TWO LAB21: MOV A,M ;PUT BCD BYTE IN A ADC M ;ADD BCD BYTE TO ITSELF WITH CARRY DAA ;CONVERT RESULT TO BCD MOV M,A ;SAVE NEW BCD BYTE INX H ;POINT TO NEXT BCD BYTE DCR C ;DECREMENT C JNZ LAB21 ;IF C=0 LEAVE LOOP JMP MLOOP ;DO FOR ALL BITS OF BINARY # LAB40: POP H ;RESTORE HL POP PSW ;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 PSW ;SAVE STATUS MVI A,00001111B ;PUT LEAST SIGNIFICANT DIGIT MASK IN A ANA M ;MASK OFF LEAST SIGNIFICANT DIGIT ORI 00110000B ;CONVERT TO ASCII MOV C,A ;PUT IN REG C MVI A,11110000B ;PUT MOST SIGNIFICANT DIGIT MASK IN A ANA M ;MASK OFF MOST SIGNIFICANT DIGIT RRC RRC RRC RRC ;ROTATE RIGHT FOUR PLACES ORI 00110000B ;CONVERT TO ASCII MOV B,A ;PUT IN REG B POP PSW ;RESTORE STATUS RET ;RETURN BCDNL: DS 31 ;START OF BCD NUMBER END