Lecture notes 16: MCU Arithmetic

advertisement
CET270 – Intro. to Microprocessor Design
Notes 16
MCU ARITHMETIC
I. Concept
We have previously seen basic arithmetic instructions such as add, subtract, increment,
decrement, and negate. Besides basic mathematic instructions, most processors feature more
advanced math instructions such as multiplication and division. More advanced processors (i.e.
desktop MPUs) also feature a floating-point “coprocessor” to handle floating-point operations. In
this notes set, we look at additional arithmetic instructions available in the HC11 MCU.
II. Multiplication
1. MUL: multiplies 8-bit multiplicand in A by 8-bit multiplier in B then stores the 16-bit product
in D. This is an unsigned, integer multiply.
(example)
III. Division
2. IDIV: integer divides 16-bit dividend in D by 16-bit divisor in X. After division, the 16-bit
quotient is in X and the 16-bit remainder is in D. IDIV does not support quotients less than 1.
(example)
3. FDIV: same as IDIV, except a fractional division is performed. Dividends less than the divisor
are expected.
(example)
IV. Exchanges
Because the division instructions manipulate 16-bit values, it is common to use 16-bit register
exchanges along with them. The HC11 includes 2 exchange instructions:
4. XGDX: exchanges 16-bit values between D and X.
5. XGDY: exchanges 16-bit values between D and Y.
V. BCD operations
The HC11 includes 1 instruction which support Binary Coded Decimal addition. The purpose of
this instruction is modify the effect of ADDs to maintain decimal (base 10) numeric
representation.
6. DAA: Decimal Adjust Accumulator, immediately follows any 8-bit ADD instruction (not
ADDD) and one of 00, 06, $60, or $66 “correction factors” to maintain 0..9 representation.
This is the only HC11 instruction that uses the Half-carry (H) flag.
(example)
BCD storage: unpacked (1 digit per byte) vs. packed (1 digits in each nibble)
VI. Application Examples
1. Redo the BCD-to-binary conversion problem using the MULtiply instruction to convert any 5byte BCD number (up to 65535) to its 16-bit unsigned binary equivalent.
2. Redo the Binary-to-BCD conversion problem using the IDIV instruction to convert any 16-bit
unsigned binary number to its BCD equivalent.
1
CET270 – Intro. to Microprocessor Design
Notes 16
Integer Division Example: divide 8502 by 308
LDD
LDX
IDIV
#$2136
#$0134
(= 8502)
(= 308)
8502/308 = 27.6039 (quotient=27 and a remainder of 0.6039*308 = 186)
(27 = $1B, 186 =$BA)
Fractional Division: divide 0.2467 by 0.4578
LDD
LDX
FDIV
#2467
#4578
($09A3)
($11E2)
2467/4578 = 0.5389  ratio to $FFFF = 0.5389*65535 = 35316 = $89F4 (in X)
D keeps the denominator $09A3
2
CET270 – Intro. to Microprocessor Design
Notes 16
Solution 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
* CET270 arithmetic example 1
* Apr. 22, 2013
* Objective: perform ASCII to Binary conversion using MULtiplies.
EOT
EQU
4
ASCstr
Binval
Temp
ORG
FCB
DS
DS
0
"20149",EOT
2
2
;reserved for conversion result ($4EB5)
;temporary working variable
ORG
ldx
jsr
std
swi
$100
#ASCstr
Dec2Bin
Binval
;point X to ASCII input string
;convert ASCII Decimal to Binary
;save answer
Start
* Dec2Bin: convert string at (X) to binary, return result in D
Dec2Bin
clr
Temp
;zero working result
clr
Temp+1
; lo byte too!
d2bloop
ldab
,X
;get ASCII char
subb
#'0
;look for ASCII digits
bmi
d2bdone
;if below '0'
cmpb
#9
bgt
d2bdone
;if above '9'
pshb
;save new digit
ldd
asld
asld
addd
asld
std
d2bdone
clra
pulb
addd
std
inx
bra
ldd
rts
Temp
;16bit multiply x10
; using shift-shift-add-shift technique
Temp
Temp
;now add in new digit
Temp
Temp
d2bloop
Temp
;and update temp variable
;advance string pointer to next char
;continue until end of string
;fetch answer
;return to caller, answer in D
END
3
CET270 – Intro. to Microprocessor Design
Notes 16
Solution 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
* CET270 arithmetic example 2
* Apr. 22, 2013
* Objective: perform 16-bit Binary to BCD conversion using IDIV instruction.
Binval
BCDbuf
DigitCnt
Start
ORG
DW
DS
DS
0
20149
5
1
;sample binary number to convert
;reserved for conversion result
;working digit counter
ORG
ldd
ldx
jsr
swi
$100
Binval
#BCDbuf
Bin2BCD
;get binary number to convert
;point X to BCD buffer to hold result
;convert to BCD
* Bin2BCD: convert 16-bit binary value in D to its 5-digit BCD equivalent,
* storing the digits to a BCD buffer pointed to by X.
Bin2BCD
pshx
;save caller's X & Y
pshy
pshx
;move BCD buffer pointer to Y
puly
psha
;temp save A
ldaa
#5
;init loop counter
staa
DigitCnt
; to number of BCD digits to produce
pula
;recall A
b2bloop
ldx
#10
;divisor
idiv
;divide binary number in D by 10
stab
4,Y
;store 0-9 BCD value right-to-left
dey
;advance BCD pointer for next iteration
xgdx
;this quotient (X) becomes next dividend (D)
dec
DigitCnt
;got all 5 decimal digits yet?
bne
b2bloop
;if not
puly
;restore caller's X & Y
pulx
rts
;return to caller, answer in memory buffer
END
4
Download