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
Subtract 8 Bit BCD Numbers Program
.model small
.data
a db 32H
b db 17H
.code
mov ax, @data ; Initialize data section
mov ds, ax
mov al, a ; Load number1 in al
mov bl, b ; Load number2 in bl
sub al, bl ; subtract numbers and result in al
das ; adjust result to valid BCD number
mov ch, 02h ; Count of digits to be displayed
mov cl, 04h ; Count to roll by 4 bits
mov bh, al ; Result in reg bh
l2: rol bh, cl ; roll bl so that msb comes to lsb
mov dl, bh ; load dl with data to be displayed
and dl, 0fH ; get only lsb
cmp dl, 09 ; check if digit is 0-9 or letter A-F
jbe l4
add dl, 07 ; if letter add 37H else only add 30H
l4: add dl, 30H
mov ah, 02 ; Function 2 under INT 21H (Display character)
int 21H
dec ch ; Decrement Count
jnz l2
mov ah, 4cH ; Terminate Program
int 21H
end
For more assembly language codes, please visit this page.
Leave a Reply