Transcript ppt version
Decimal Arithmetic
(alternative to binary arithmetic)
numbers are stored as a series of base 10 digits
binary coded decimal (BCD) or
decimal arithmetic
two formats:
1.
unpacked BCD → 8 bits = 1 decimal digit in low nibble
e.g. 1,98510
= %0000 0001 0000 1001 0000 1000 0000 0101
= $01090805
2.
packed BCD
→ 8 bits = 2 decimal digits
e.g. 1,98510
= %0001 1001 1000 0101
= $1985
Why Binary Coded Decimal (BCD)?
input/output from/to systems is typically ASCII
keypad systems (numbers only) are typically BCD
key press → ASCII or BCD digit
BCD arithmetic operations:
work on one byte at a time
assume packed BCD
→ 8 bits = 2 decimal digits
unpacked BCD only used in transition from ASCII to BCD
→ 8 bits = 1 decimal digit in low nibble
Why Binary Coded Decimal (BCD)?
if binary arithmetic:
if decimal arithmetic:
input → ASCII
ASCII to BCD
BCD to binary
calc in binary
binary to BCD
BCD to ASCII
ASCII → output
input → ASCII
ASCII to BCD
BCD to ASCII
ASCII → output
Decimal Arithmetic
operations: ABCD, SBCD, NBCD
→ byte only (2 BCD digits at a time)
start
at the least significant byte and work to
most significant byte
carry handled by using the X flag
ABCD
(source)10 + (dest)10 + X → dest10
e.g.
1985
3459
Decimal Arithmetic
Example 1
1000 0101
0101 1001
Decimal Adjust Rules:
1. If sum > 9,
then add 6
2. If carry from bit 3
to bit 4, then add 6
to the source
of the carry.
… what really happens
Example 2
0000 1000
0000 1001
Processors either:
1. provide BCD instructions (like 68000)
2. provide a decimal adjust instruction and
a decimal carry in CCR (like Intel x86)
• step 1 = binary add
• step 2 = decimal adjust
Flags for BCD
clear X (extended carry) before first BCD
operation so that carry is handled correctly
N and V are undefined for decimal arithmetic
C and X is set if decimal carry/borrow occurs
sign is explicitly handled during the operation
can be used to construct result if overflow
Z is cleared if the result is nonzero; otherwise
unchanged
for addition, set Z to 1 before first BCD operation to
obtain correct Z
no requirement to initialize Z if it is not important to the
problem
Decimal Arithmetic
e.g. Add two numbers (bcdn1, bcdn2) and store result (bcdn2).
org
$1000
start
clr.w
D2
move.b length,D2
subq.w #1,D2
lea
bcdn1+4,A1
lea
bcdn2+4,A0
move.w #$04,CCR
loop
abcd.b -(A1),-(A0)
dbra
D2,loop
stop
#$2700
bcdn1 DC.L
$36701985
bcdn2
DC.L
$12663459
length
DC.B
end
4
start
BCD complements
9’s complement
… signed numbers
1’s complement
flip all of the bits
e.g. 0011 –> 1100
or, alternatively,
≡ 1111 – 0011
10’s complement
2’s complement
1’s
complement +1
e.g. 0011 –> 1101
or, alternatively,
≡ ???? – 0011
Decimal Arithmetic … subtraction
e.g. 9999
- 4930
e.g. 1817
-4930
e.g. 0000
- ____
9999
- ____
Reading:
Mind-boggling math: BCD (binary coded decimal) [pdf]
[C. Maxfield -- EDN, 9/7/2005]
More mind-boggling math: Adding and subtracting unsigned BCD [pdf]
[C. Maxfield -- EDN, 9/21/2005]
Even more mind-boggling math: Working with signed BCD [pdf]
[C. Maxfield -- EDN, 10/5/2005]
NOTE: for the above references, the local PDF version is complete while the
online EDN version is missing the diagrams.
M68000 Assembly Language [pdf, 92p; N. Znotinas]
review the decimal arithmetic (ABCD, SBCD, NBCD) instructions
Expectations:
you can write programs to do decimal arithmetic for
numbers of unlimited width
you understand 9s and 10s complement
you understand how NBCD works
you know how to output negative BCD numbers as a
negative decimal number (minus sign followed by the
magnitude of the negative number)