Subtract 8 bit BCD numbers
Introduction
Binary Coded Decimal Number system uses binary numbers to represent a decimal number.
Here Each decimal digit is represented by either 4-digit or 8-digit bits.
BCD is extensively used to display the numeric values in all electronic systems containing only digital logic without microprocessors.
BCD is more powerful than the binary representations. It helps in rounding off decimal values more effectively into human readable formats.
The Disadvantages of BDC are some operations will be more complex to implement. Also BCD consumes around 20% more space compared to binary representation.
Below is the BCD representation chart for decimal digits from 0-15
Decimal Number | Binary Number | Binary Coded Decimal Number (BCD) |
0 | 0000 | 0000 |
1 | 0001 | 0001 |
2 | 0010 | 0010 |
3 | 0011 | 0011 |
4 | 0100 | 0100 |
5 | 0101 | 0101 |
6 | 0110 | 0110 |
7 | 0111 | 0111 |
8 | 1000 | 1000 |
9 | 1001 | 1001 |
10 | 1010 | 0001 0000 |
11 | 1011 | 0001 0001 |
12 | 1100 | 0001 0010 |
13 | 1101 | 0001 0011 |
14 | 1110 | 0001 0100 |
15 | 1111 | 0001 0101 |
Algorithm to subtract 8-bit BCD numbers
Step I : Initialize the data memory.
Step II : Get the first BCD number in AL.
Step III : Get the second BCD number in BL.
Step IV : Subtract the two BCD numbers.
Step V : Adjust result to valid BCD number.
Step VI : Display the result.
Step VII : Stop.
Subtract 8 Bit BCD Numbers Program
1 |
<span style="font-size: 14pt; color: #000000;"> <br/>.model small <br/> .data <br/> a db 32H <br/> b db 17H <br/> .code <br/> mov ax, @data ; Initialize data section <br/> mov ds, ax <br/> mov al, a ; Load number1 in al <br/> mov bl, b ; Load number2 in bl <br/> sub al, bl ; subtract numbers and result in al <br/> das ; adjust result to valid BCD number <br/> mov ch, 02h ; Count of digits to be displayed <br/> mov cl, 04h ; Count to roll by 4 bits <br/> mov bh, al ; Result in reg bh <br/> l2: rol bh, cl ; roll bl so that msb comes to lsb <br/> mov dl, bh ; load dl with data to be displayed <br/> and dl, 0fH ; get only lsb <br/> cmp dl, 09 ; check if digit is 0-9 or letter A-F <br/> jbe l4 <br/> add dl, 07 ; if letter add 37H else only add 30H <br/> l4: add dl, 30H <br/> mov ah, 02 ; Function 2 under INT 21H (Display character) <br/> int 21H <br/> dec ch ; Decrement Count <br/> jnz l2 <br/> mov ah, 4cH ; Terminate Program <br/> int 21H <br/> end <br/></span> |
For more assembly language codes, please visit this page.
Source projectgeek.com