Professional Documents
Culture Documents
BCD Numbers
Binary Coded Decimal (BCD) as the name implies is a way of representing Decimal numbers in a 4 bit
binary code. BCD numbers are useful when sending data to display devices for example. The numbers 0
through 9 are the only valid BCD values. Notice in the table that the binary and BCD values are the
same for the numbers 0 to 9. When we exceed the value of 9 in BCD each digit in the BCD number is
now represented by a 4 bit binary value.
Number
0
1
2
3
4
5
6
7
8
9
10
11
Binary
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010 invalid BCD number
1011 invalid BCD number
BCD
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
0001 0000
0001 0001
ASCII Code
Computers must have some way to represent the common symbols (letters, numbers, and other
characters) and store them in memory. ASCII code was developed to do this task. ASCII code tables are
easy to find on the Internet. A 7 bit ASCII code is the only standardized version of this code. There are 8
bit ASCII codes but none has been adopted as an official standard. Unicode is a 16 bit extension of ASCII
code.
Some simple rules exist for understanding ASCII code
Letters in ASCII Code
Letters are represented sequentially for example
Letter
A
B
C
etc
The difference between the hex values for a lower case letter and the same upper case letter is always
20 hex.
Lower case to upper case conversion can be done in Assembler
ldaa
anda
#a
#$DF
ldaa
suba
#a
#$20
;eora
OR
ASCII value
30
31
32
33
34
35
36
37
38
39
BCD value
0000 0000
0000 0001
0000 0010
0000 0011
0000 0100
0000 0101
0000 0110
0000 0111
0000 1000
0000 1001
An ASCII value in binary can be converted to a BCD value in Assembler in two ways
Assume Register A contains ASCII value in binary. The following methods convert it to BCD
Method 1
ldaa
anda
#$35
#$0F
ldaa
suba
#$35
#$30
Method 2
Packed BCD
Packed BCD is a way of representing 2 BCD digits in an 8 bit value.
Packed BCD value
$25
0010 0101
ASCII value
34
37
Binary value
0011 0100
0011 0111
packBCD:
ldaa
anda
lsla
lsla
lsla
lsla
staa
ldaa
anda
oraa
staa
swi
end
org $2100
ds
01
org $2000
#4
#$0F
packBCD
#7
#$0F
packBCD
packBCD
packBCD:
ldaa
anda
tab
ldab
mul
ldaa
anda
aba
staa
swi
end
org $2100
ds
01
org $2000
#4
#$0F
#$10
#7
#$0F
packBCD
High_Byte:
Low_Byte:
PackBCD:
ldaa
anda
lsra
lsra
lsra
lsra
oraa
staa
ldaa
anda
oraa
staa
swi
end
org $2100
ds
01
ds
01
equ
$39
org $2000
#PackBCD
#$F0
#$30
High_Byte
#PackBCD
#$0F
#$30
Low_Byte