;TITLE "BINARY TO BCD CONVERTER" ;BY D. M. BROCKMAN, 11648 MILITARY RD. SO., SEATTLE, WA 98168 ; ;THIS SUBROUTINE CONVERTS THE 32-BIT BINARY NUMBER STORED ; AT 'BIN' TO A 10-DIGIT PACKED BCD NUMBER STORED AT 'BCD.' ; REGISTERS B, C, H, L, AND A ARE USED. ; ; This is a slightly optimized version for the Z80-Processor, ; Werner Cirsovius ; ; ;DEFINE STORAGE FOR NUMBERS: ; entry binbcd dseg bin: ds 1 ; LSB BYTE ds 2 ds 1 ; MSB BYTE binlen equ $-bin ; bcd: ds 1 ; LS DIGITS ds 3 ds 1 ; MS DIGITS bcdlen equ $-bcd ; ; ;INITIALIZE THE BCD RESULT BYTES TO ZERO: ; cseg binbcd: ld hl,bcd ; POINT AT RESULT ld b,bcdlen ; SET LOOP COUNTER xor a iloop: ld (hl),a ; STORE A ZERO inc hl ; POINT TO SEXT BYTE djnz iloop ; LOOP IF NOT DONE ; ;INITIALIZE THE BIT COUNTER TO EQUAL THE NUMBER OF ; BINARY BITS TO BE SHIFTED: ; ld b,binlen*8 ; NUMBER OF BITS TO SHIFT cloop: ; THE MAIN LOOP POINT ; ;INITIALIZE FOR A MULTIPLE PRECISION LEFT SHIFT ; OF THE BINARY NUMBER: ; ld hl,bin ; POINT AT NUMBER ld c,binlen ; NUMBER OF BYTES xor a ; CLEAR THE CARRY ; ;SHIFT THE BINARY NUMBER ONE PLACE LEFT AND LEAVE THE MSB ; IN THE CARRY: ; rloop: rl (hl) ; SHIFT LEFT BYTE inc hl ; POINT AT NEXT BYTE dec c ; DECREMENT BYTE COUNT jr nz,rloop ; LOOP IF NOT DONE ; ;INITIALIZE TO DOUBLE THE BCD RESULT REGISTER CONTENTS ; BY PERFORMING A MULTIPLE PRECISION BCD ADD: ; ld hl,bcd ; POINT AT RESULT ld c,bcdlen ; SETUP BYTE COUNTER ; ;DOUBLE THE RESULT BCD FASHION, ADDING IN THE CARRY BIT: ; bloop: ld a,(hl) ; GET BYTE adc a,(hl) ; ADD IT TO ITSELF daa ; BCD CONVERT ld (hl),a ; RESTORE inc hl ; POINT AT NEXT BYTE dec c ; DECREMENT BYTE COUNTER jr nz,bloop ; LOOP IF NOT DONE ; ;TEST TO SEE IF ALL BITS HAVE BEEN SHIFTED FROM BINARY ; NUMBER TO THE BCD RESULT. IF NOT, RETURN TO MAIN LOOP. ; djnz cloop ; LOOP IF NOT DONE ret end