Im Magazin „BYTE" wurde in der Ausgabe August 1981 der folgende Artikel abgedruckt.
In einigen Public Domain Bibliotheken waren auch BCD-Routinen zu finden. Dieser Artikel zeigt eine einfache Art der Wandlung auf.

Binary-to-BCD Converter Program for the 8080


D M Brockman, 11648 Military Rd S, Seattle WA 98168
Conversion of multiple-precision binary numbers to BCD (binary-coded decimal) for printing or display output is often encountered in assembly-language programming. The algorithm described here makes use of the 8080's DAA (decimal-adjust accumulator) instruction to implement the BCD analogy of a binary shift-left register. With this algorithm, a binary number is converted to packed BCD (2 digits per byte) by shifting the binary number, MSB (most significant bit) first, bit by bit, into the least significant end of the BCD register. (See figure 1.) When the last binary bit has been shifted in, the result is in the BCD register.
Figure 1:Contents of the BCD and binary registers during the conversion process using the program shown in listing 1.
To understand how the BCD register operates, consider a conventional binary-shift register. When shifted left, the numeric content doubles and the carry input (if any) comes in at the least significant end. The same thing happens when the BCD register is "shifted." The numeric doubling is accomplished by adding the BCD register to itself using BCD arithmetic. The carry is brought in by using the ADC (add with carry) instruction for the first "add."
A typical implementation of the algorithm is shown in listing 1 in the form of an 8080 assembly-language listing. Looping constructs have been employed to minimize size. For maximum speed, the loops should be "unrolled." The implementation converts a 32-bit (4-byte) binary number into a 10-digit (5-byte) packed BCD result. The number of bytes converted can be easily changed by altering the looping counters. Note that the number of BCD result bytes required is given by:

Number of BCD bytes = INT(0.5 + 1.204 X (Number of binary bytes))

An advantage of this algorithm is that the conversion time is independent of the size of the number being converted. The sample routine in listing 1 requires 13,944 machine cycles to execute. The results of a typical run of the sample routine are shown in table 1.
Memory
Address
Before
Run
After
Run
 
BIN:
 
02003A00
0201F600
02022300
02038100
 
BCD:
 
0204XX58
0205XX76
0206XX61
0207XX66
0208XX21
Table 1:A sample run of the binary-to-BCD converter program shown in listing 1. All numbers shown are hexadecimal. XX indicates a don't care.

Listing 1: A binary-to-BCD Converter program for the Intel 8080 microprocessor.
                ;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.
                ;
                ;
                ;DEFINE STORAGE FOR NUMBERS:
                ;
 0200           	ORG	200H
 0200           BIN:	DS	1	;LSB BYTE
 0201           	DS	2
 0203           	DS	1	;MSB BYTE
                ;
 0204           BCD:    DS	1	;LS DIGITS
 0205           	DS	3
 0208           	DS	1	;MS DIGITS
                ;
                ;
                ;INITIALIZE THE BCD RESULT BYTES TO ZERO:
                ;
 0100           	ORG	100H
 0100 210402    BINBCD:	LXI	H,BCD	;POINT AT RESULT
 0103 0605      	MVI	B,5	;SET LOOP COUNTER
 0105 3600      ILOOP:	MVI	M,0	;STORE A ZERO
 0107 23        	INX	H	;POINT TO SEXT BYTE
 0108 05        	DCR	B	;DECREMENT LOOP COUNTER
 0109 C20501    	JNZ	ILOOP	;LOOP IF NOT DONE
                ;
                ;INITIALIZE THE BIT COUNTER TO EQUAL THE NUMBER OF
                ;  BINARY BITS TO BE SHIFTED:
                ;
 010C 0620      	MVI	B,32	;NUMBER OF BITS TO SHIFT
                CLOOP:			;THE MAIN LOOP POINT
                ;
                ;INITIALIZE FOR A MULTIPLE PRECISION LEFT SHIFT
                ;  OF THE BINARY NUMBER:
                ;
 010E 210002    	LXI	H,BIN	;POINT AT NUMBER
 0111 0E04      	MVI	C,4	;NUMBER OF BYTES
 0113 AF        	XRA	A	;CLEAR THE CARRY
                ;
                ;SHIFT THE BINARY NUMBER ONE PLACE LEFT AND LEAVE THE MSB
                ;  IN THE CARRY:
                ;
 0114 7E        RLOOP:	MOV	A,M	;GET BYTE
 0115 17        	RAL		;SHIFT LEFT
 0116 77        	MOV	M,A	;STORE BYTE
 0117 23        	INX	H	;POINT AT NEXT BYTE
 0118 0D        	DCR	C	;DECREMENT BYTE COUNT
 0119 C21401    	JNZ	RLOOP	;LOOP IF NOT DONE
                ;
                ;INITIALIZE TO DOUBLE THE BCD RESULT REGISTER CONTENTS
                ;  BY PERFORMING A MULTIPLE PRECISION BCD ADD:
                ;
 011C 210402    	LXI	H,BCD	;POINT AT RESULT
 011F 0E05      	MVI	C,5	;SETUP BYTE COUNTER
                ;
                ;DOUBLE THE RESULT BCD FASHION, ADDING IN THE CARRY BIT:
                ;
 0121 7E        BLOOP:	MOV	A,M	;GET BYTE
 0122 8E        	ADC	M	;ADD IT TO ITSELF
 0123 27        	DAA		;BCD CONVERT
 0124 77        	MOV	M,A	;RESTORE
 0125 23        	INX	H	;POINT AT NEXT BYTE
 0126 0D        	DCR	C	;DECREMENT BYTE COUNTER
 0127 C22101    	JNZ	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.
                ;
 012A 05        	DCR	B	;DECREMENT BIT COUNTER
 012B C20E01    	JNZ	CLOOP	;LOOP IF NOT DONE
 012E C9        	RET
                
 012F           	END

A>
[Hier als Z80-Quelltext]

Eingescanned von Werner Cirsovius
September 2002
© BYTE Publications Inc.