You are on page 1of 4

BCD Numbers and ASCII Code

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

Upper Case values in hex


41
42
43

Lower Case values in hex


61
62
63

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

#$20 also works

OR

Exercise: Write the code to convert Upper case to Lower case.


Numbers in ASCII Code
Numbers are represented in ASCII by adding 30 in hex to the value of the number. The ASCII code for 9
is 39. Computer keyboards generate the ASCII code for a number when pressed.
Key
0
1
2
3
4
5
6
7
8
9

ASCII value
30
31
32
33
34
35
36
37
38
39

ASCII value in Binary


0011 0000
0011 0001
0011 0010
0011 0011
0011 0100
0011 0101
0011 0110
0011 0111
0011 1000
0011 1001

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

Unpacked BCD value


$02 $05
0000 0010
0000 0101

ASCII code value


$32 $35
0011 0010
0011 0101

ASCII to BCD Conversion


As a practical exercise consider the circumstance where 2 ASCII values are to be converted to Packed
BCD.
Key
4
7

ASCII value
34
37

Binary value
0011 0100
0011 0111

Unpacked BCD value


0000 0100
0000 0111

The following Assembler code converts ASCII to packed BCD

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

; reserve memory for packed BCD result

;load A with ASCII code for 4 => 34


;convert to unpacked BCD 34 => 04
;shift lower nibble to upper nibble
;shift left 4 times 04 => 40

;store upper nibble


;load A with ASCII code for 7 => 37
;convert to unpacked BCD 37 => 07
;combine lower and upper nibbles
;store packed BCD result of 47

Packed BCD value


0100 0111

An Alternate Programming Solution

packBCD:
ldaa
anda
tab
ldab
mul
ldaa
anda
aba
staa
swi
end

org $2100
ds
01
org $2000
#4
#$0F
#$10
#7
#$0F
packBCD

; reserve memory for packed BCD result


;load A with ASCII code for 4 => 34
;convert to unpacked BCD 34 => 04
;transfer A to B
;shift left 4 times by multiplying by $10 or 16
; 04 => 40
;load A with ASCII code for 7 => 37
;convert to unpacked BCD 37 => 07
;combine lower and upper nibbles 40+07=47
;store packed BCD result of 47

Packed BCD to ASCII Conversion


The following program converts a Packed value to two ASCII coded bytes.

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

; reserve memory for upper byte result


; reserve memory for lower byte result
;sample data to unpack

;load A with $39 packed BCD


;mask the lower nibble 39 => 30
;shift right 4 times 30 => 03

;convert to ASCII 03 => 33 - adda #$30 also works


;store in memory
;load A with $39 packed BCD
;mask the upper nibble 39 => 09
;convert to ASCII 09 => 39
- adda #$30 also works
;store in memory

You might also like