; Bin2ToAsc5 ; ========== ; converts a 16-bit-binary to a 5 digit ASCII-coded decimal ; In: 16-bit-binary in rBin1H:L, Z points to the highest ; of 5 ASCII digits, where the result goes to ; Out: Z points to the beginning of the ASCII string, lea- ; ding zeros are filled with blanks ; Used registers: rBin1H:L (content is not changed), ; rBin2H:L (content is changed), A ; Called subroutines: Bin2ToBcd5 ; ;.CSEG ;.include "../includes/tn45def.inc" .def rBin1L = r5 ; .def rBin1H = r6 ; .def rBin2L = r7 ; .def rBin2H = r8 ; ;.def A = r16 ; Bin2ToAsc5: rcall Bin2ToBcd5 ; convert binary to BCD ldi A,4 ; Counter is 4 leading digits mov rBin2L,A Bin2ToAsc5a: ld A,z ; read a BCD digit tst A ; check if leading zero brne Bin2ToAsc5b ; No, found digit >0 ldi A,' ' ; overwrite with blank st z+,A ; store and set to next position dec rBin2L ; decrement counter brne Bin2ToAsc5a ; further leading blanks ld A,z ; Read the last BCD Bin2ToAsc5b: inc rBin2L ; one more char Bin2ToAsc5c: subi A,-'0' ; Add ASCII-0 st z+,A ; store and inc pointer ld A,z ; read next char dec rBin2L ; more chars? brne Bin2ToAsc5c ; yes, go on ;ldi A,0xFF ;st Z+,A ; store 0xFF and inc pointer ;sbiw ZL,5 ; Pointer to beginning of the BCD ret ; done ; ========= Bin2ToAsc5_: rcall Bin2ToBcd5 ; convert binary to BCD ldi A,4 ; Counter is 4 leading digits mov rBin2L,A Bin2ToAsc5a_: ld A,z ; read a BCD digit tst A ; check if leading zero brne Bin2ToAsc5b_ ; No, found digit >0 ldi A,' ' ; overwrite with blank st z+,A ; store and set to next position dec rBin2L ; decrement counter brne Bin2ToAsc5a_ ; further leading blanks ld A,z ; Read the last BCD Bin2ToAsc5b_: inc rBin2L ; one more char Bin2ToAsc5c_: subi A,-'0' ; Add ASCII-0 st z+,A ; store and inc pointer ld A,z ; read next char dec rBin2L ; more chars? brne Bin2ToAsc5c_ ; yes, go on ;ldi A,0xFF ;st Z+,A ; store 0xFF and inc pointer ;sbiw ZL,5 ; Pointer to beginning of the BCD ret ; done ; Bin2ToBcd5 ; ========== ; converts a 16-bit-binary to a 5-digit-BCD ; In: 16-bit-binary in rBin1H:L, Z points to first digit ; where the result goes to ; Out: 5-digit-BCD, Z points to first BCD-digit ; Used registers: rBin1H:L (unchanged), rBin2H:L (changed), ; A ; Called subroutines: Bin2ToDigit ; Bin2ToBcd5: push rBin1H ; Save number push rBin1L ldi A, HIGH(10000) ; Start with tenthousands mov rBin2H,A ldi A, LOW(10000) mov rBin2L,A rcall Bin2ToDigit ; Calculate digit ldi A, HIGH(1000) ; Next with thousands mov rBin2H,A ldi A, LOW(1000) mov rBin2L,A rcall Bin2ToDigit ; Calculate digit ldi A, HIGH(100) ; Next with hundreds mov rBin2H,A ldi A, LOW(100) mov rBin2L,A rcall Bin2ToDigit ; Calculate digit ldi A, HIGH(10) ; Next with tens mov rBin2H,A ldi A, LOW(10) mov rBin2L,A rcall Bin2ToDigit ; Calculate digit st z,rBin1L ; Remainder are ones sbiw ZL,4 ; Put pointer to first BCD pop rBin1L ; Restore original binary pop rBin1H ret ; and return ; ; Bin2ToDigit ; =========== ; converts one decimal digit by continued subraction of a ; binary coded decimal ; Used by: Bin2ToBcd5, Bin2ToAsc5, Bin2ToAsc ; In: 16-bit-binary in rBin1H:rBin2L, binary coded decimal in ; rBin2H:rBin2L, Z points to current BCD digit ; Out: Result in Z, Z incremented ; Used registers: rBin1H:rBin2L (holds remainder of the binary), ; rBin2H:rBin2L (unchanged), A ; Called subroutines: - ; Bin2ToDigit: clr A ; digit count is zero Bin2ToDigita: cp rBin1H,rBin2H ; Number bigger than decimal? brcs Bin2ToDigitc ; MSB smaller than decimal brne Bin2ToDigitb ; MSB bigger than decimal cp rBin1L,rBin2L ; LSB bigger or equal decimal brcs Bin2ToDigitc ; LSB smaller than decimal Bin2ToDigitb: sub rBin1L,rBin2L ; Subtract LSB decimal sbc rBin1H,rBin2H ; Subtract MSB decimal inc A ; Increment digit count rjmp Bin2ToDigita ; Next loop Bin2ToDigitc: st z+,A ; Save digit and increment ret ; done ; Asc2DCB ;Converts Two ASCII-coded by DCB ;In: 16-bit-binary in rBin1H:rBin2L ASCII. For example: 37:30 ;Out: Result A=70 ldi A,0xF0 swap rBin1H and rBin1H,A SWAP A and rBin1L,A mov A,rBin1L and A,rBin1H ret