Vous êtes sur la page 1sur 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 Binary 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 – invalid BCD number 0001 0000
11 1011 – invalid BCD number 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 Upper Case values in hex Lower Case values in hex


“A” 41 61
“B” 42 62
“C” 43 63
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 #’a’
anda #$DF ;eora #$20 also works
OR
ldaa #’a’
suba #$20

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 ASCII value ASCII value in Binary BCD value


0 30 0011 0000 0000 0000
1 31 0011 0001 0000 0001
2 32 0011 0010 0000 0010
3 33 0011 0011 0000 0011
4 34 0011 0100 0000 0100
5 35 0011 0101 0000 0101
6 36 0011 0110 0000 0110
7 37 0011 0111 0000 0111
8 38 0011 1000 0000 1000
9 39 0011 1001 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 #$35
anda #$0F

Method 2
ldaa #$35
suba #$30
Packed BCD
Packed BCD is a way of representing 2 BCD digits in an 8 bit value.

Packed BCD value Unpacked BCD value ASCII code value


$25 $02 $05 $32 $35
0010 0101 0000 0010 0011 0010
0000 0101 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 ASCII value Binary value Unpacked BCD value Packed BCD value
4 34 0011 0100 0000 0100
7 37 0011 0111 0000 0111 0100 0111

The following Assembler code converts ASCII to packed BCD

org $2100
packBCD: ds 01 ; reserve memory for packed BCD result

org $2000
ldaa #’4’ ;load A with ASCII code for ‘4’ => 34
anda #$0F ;convert to unpacked BCD 34 => 04
lsla ;shift lower nibble to upper nibble
lsla ;shift left 4 times 04 => 40
lsla
lsla
staa packBCD ;store upper nibble
ldaa #’7’ ;load A with ASCII code for ‘7’ => 37
anda #$0F ;convert to unpacked BCD 37 => 07
oraa packBCD ;combine lower and upper nibbles
staa packBCD ;store packed BCD result of 47
swi
end
An Alternate Programming Solution

org $2100
packBCD: ds 01 ; reserve memory for packed BCD result
org $2000
ldaa #’4’ ;load A with ASCII code for ‘4’ => 34
anda #$0F ;convert to unpacked BCD 34 => 04
tab ;transfer A to B
ldab #$10 ;shift left 4 times by multiplying by $10 or 16
mul ; 04 => 40
ldaa #’7’ ;load A with ASCII code for ‘7’ => 37
anda #$0F ;convert to unpacked BCD 37 => 07
aba ;combine lower and upper nibbles – 40+07=47
staa packBCD ;store packed BCD result of 47
swi
end

Packed BCD to ASCII Conversion


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

org $2100
High_Byte: ds 01 ; reserve memory for upper byte result
Low_Byte: ds 01 ; reserve memory for lower byte result
PackBCD: equ $39 ;sample data to unpack

org $2000
ldaa #PackBCD ;load A with $39 – packed BCD
anda #$F0 ;mask the lower nibble 39 => 30
lsra ;shift right 4 times 30 => 03
lsra
lsra
lsra
oraa #$30 ;convert to ASCII 03 => 33 - adda #$30 also works
staa High_Byte ;store in memory
ldaa #PackBCD ;load A with $39 – packed BCD
anda #$0F ;mask the upper nibble 39 => 09
oraa #$30 ;convert to ASCII 09 => 39 - adda #$30 also works
staa Low_Byte ;store in memory
swi
end