Binary-coded Decimal or BCD numbers was used in many early decimal computers [1]. For example, any one decimal numeral can be represented by a four bit pattern is shown in Table 1. A digit cannot exceed than 9. Most of the assembly languages process the arithmetic operation in hexadecimal/binary numbers, thus the arithmetic operation might be exceeds than 9 and the result will show in hexadecimal numbers which the value can be AH to FH.
Table 1
Decimal
Digit |
BCD
8 4 2 1 |
0
|
0 0 0 0
|
1
|
0 0 0 1
|
2
|
0 0 1 0
|
3
|
0 0 1 1
|
4
|
0 1 0 0
|
5
|
0 1 0 1
|
6
|
0 1 1 0
|
7
|
0 1 1 1
|
8
|
1 0 0 0
|
9
|
1 0 0 1
|
BCD makes the addition easier to execute in assembly language although it is not widely used in the past. Following example gives the appearance of executing decimal addition.
However, a problem occurs when the HCS12 adds two BCD digits and produces a sum greater than nine. The sum is inappropriate in the decimal number system, as the following three examples illustrate:
A sum in the BCD format is incorrect if it is greater than 9 or if there is a carry to the next higher nibble [2]. Incorrect BCD sums can be adjusted by adding 6 to them. To correct the examples, do the following [2]:
1. Add 6 to every sum digit greater than nine.
2. Add 6 to every sum digit that had a carry of one to the next higher digit.
Some microprocessors provide the detection and correction as HCS12, it provides a decimal adjust accumulator A instruction, DAA, which takes care of all these detailed detection and correction operations [2].
Basic algorithm for BCD addition is shown in Table 2. The example shows the addition involves 16-bit data for 8-bit microprocessor. It can be also applied to more than 8-bit microprocessor by adjusting the value. As example, WWXXH is the data to be added to YYZZH. The data WWH, XXH, YYH and ZZH are in the 8-bit memory. Sum of the BCD addition is stored at memory location, which higher than the data to be added. The illustration of a memory is shown in Table 3.
Table 2
Table 3
Memory location
|
Data in memory
|
#0
|
WWH
|
#1
|
XXH
|
#2
|
YYH
|
#3
|
ZZH
|
#4
|
Memory1
|
#5
|
Memory2
|
It is simply to write the program in HCS12 assembly language when the algorithm is developed. The algorithm can also be applied to other types of assembly languages. If the addition needs to be performed in more than 16-bit, branch instruction is necessary to be included in the algorithm. Based on the Table 2, the instructions in HCS12 as follows:
WW equ 0
XX equ 1
YY equ 2
ZZ equ 3
Memory1 equ 4
Memory2 equ 5
Org $800
Ldaa XX
Adda ZZ
Daa
Staa memory2
Ldaa WW
Adca YY
Daa
Staa memory1
end
Written by:
Siti Sara Rais
Adca YY
Daa
Staa memory1
end
Written by:
Siti Sara Rais
[2] H.-W. Huang. (2009). Introduction to the 68HC12 Microcontroller.
#technical article
ReplyDelete