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.
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.
ldaa #’a’
anda #$DF ;eora #$20 also works
OR
ldaa #’a’
suba #$20
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.
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
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
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