Microsoft 10 Basic_Arithmetic_Instructionsx Basic Arithmetic Instructions
User Manual:
Open the PDF directly: View PDF .
Page Count: 15
Lecture 10:
Lecture
10:
Basic Arithmetic Instructions
Today’s Goals
R i Additi d S bt ti
•
R
ev
i
ew
Additi
on an
d
S
u
bt
rac
ti
on
•Use Multiple Precision arithmetic to add and subtract large
b
num
b
ers.
•Practice writing assembly programs.
Addition and Subtraction
•
8 bit addition
From Lecture 8
We will use ADCA(B) and
SBCA
(
B
)
to do multi-
•
8
bit
addition
ABA: (A) + (B) A; Note that there is no AAB instruction!
ADDA: (A) + (M) A
•ADDA $1000
ADDB (B) (M)
B
()
precision addition or
subtraction
ADDB
:
(B)
+
(M)
B
•ADDB #10
ADCA: (A) + (M) + C A
ADCB: (B) + (M) + C B
•8 bit subtraction
SBA: (A) – (B) A; Subtract B from A (Note: not SAB instruction!)
SUBA: (A)
–
(M)
A; Subtract M from A
SUBA:
(A)
(M)
A;
Subtract
M
from
A
SUBB: (B) – (M) B
SBCA: (A) – (M) – C A
SBCB: (B) – (M) – C BThere is a pattern that make you
be easy to remember the
•16 bit addition and subtraction
ADDD: (A:B) + (M:M+1) A:B
SUBD: (A:B) – (M:M+1) A:B
ABX (B) (X)
X
be
easy
to
remember
the
instructions!!!
1. The last letter in these
instructions is the destination!
ABX
:
(B)
+
(X)
X
ABY: (B) + (Y) Y2. Also it comes to the first in the
operation
Precision?
Th t
ii
ift dt ft
th i f it
•
Th
e
t
erm prec
i
s
i
on
i
s o
ft
en use
d
t
o re
f
er
t
o
th
e s
i
ze o
f
a un
it
of data manipulated by the processor.
Si l
ii
f t i t ti th t i l t
•
Si
ng
l
e-prec
i
s
i
on re
f
ers
t
o
i
ns
t
ruc
ti
ons
th
a
t
man
i
pu
l
a
t
e one
byte at a time.
ADDA, ADDB, ABA, SUBA, SUBB, SBA
•Double-precision refers to two-byte operation.
ADDD
,
SUBD
,
ABX: (B) + (X) X, ABY: (B) + (Y) Y
•Multi-
p
recision
p
Adding and subtracting numbers longer than single precision
introduce an issue.
Carries
and
borrows
need to
propagate
through a number
Carries
and
borrows
need
to
propagate
through
a
number
.
Example
Mlti
i i dditi i f d b t t ti
Adding two quadruple-precision numbers
•
M
u
lti
-prec
i
s
i
on a
dditi
on
i
s per
f
orme
d
one
b
y
t
e a
t
a
ti
me,
beginning with the least significant byte.
•92FF45B716 ORG $1200
•6D325D8816 num1
D
C.B
$
92, $FF, $45, $B7
num2 DC.B $6D, $32, $5D, $88
ans DS.B 4
1
1
1
O
RG
$
2000
LDAA num1+3 ; 1
ADDA num2+3 ; 2
STAA ans+3 ; 3
92 FF 45 B7
L
DAA
n
um1+2
;
4
ADCA num2+2 ; 5
STAA ans+2 ; 6
LDAA num1+1 ; 7
6D 32 5D 88
+
-----------
ADCA
n
um2+1
;
8
STAA ans+1 ; 9
LDAA num1 ; 10
ADCA num2 ; 11
+
-----------
3
F
A
3
3
100
S
TAA
a
ns
;
12
SWI ; 13
Program Trace
Trace Line PC A N Z V C
1 1 2003
2 2 2006
3
3
2009
B7100-
3F0011
3F
0
0
0
1
ORG $1200
num1 DC.B $92,$FF,$45,$B7
num2 DC.B $6D,$32,$5D,$88
ans DS.B 4
3
3
2009
4 4 200C
5 5 200F
3F
0
0
0
1
450001
A
3
1
0
1
0
ORG $2000
LDAA num1+3 ; 1
ADDA num2+3 ; 2
6 6 2012
7 7 2015
3
0
0
A31000
FF1000
STAA ans+3 ; 3
LDAA num1+2 ; 4
ADCA num2+2 ; 5
STAA ans+2 ; 6
8 8 2018
9 9 201B
10
10
201E
310000
310001
92
1
0
0
1
LDAA num1+1 ; 7
ADCA num2+1 ; 8
STAA ans+1 ; 9
LDAA num1 ; 10
10
10
201E
11 11 2021
12 12 2022
92
1
0
0
1
000101
000101
ADCA num2 ; 11
STAA ans ; 12
SWI ; 13
13 13 2024 -----
Another Example
Cl lt
t
bt
ff
bt
id
•
C
a
l
cu
l
a
t
e a
t
wo-
b
y
t
esum o
f
an array o
f
one-
b
y
t
e uns
i
gne
d
numbers.
Ri t
•
R
equ
i
remen
t
s
Variable
ovflow
should be $00 if the sum is valid. Otherwise, $ff.
The address of the arra
y
of one-b
y
te unsi
g
ned inte
g
ers is
y
yg g
supplied at $1030.
The length of the array is a one-byte value supplied in $1032.
Ovflow
must be assigned to address
$1040
Ovflow
must
be
assigned
to
address
$1040
.
The sum is returned in locations $1041 and $1042.
Flowchart
SEQ
Start 1:clearsumand
overflow
2:pointtothe
startofthearray
WDO
3:getcopyof
arraylength
4:Thereareno
moreelement
s
9:End
SEQ
true
false
WDO
6
fl
ITE
SEQ
true
5:sum<=
sum+element
7:indicate
6
:noover
fl
ow
occurs
true
false
overflow
8:preparefor
nextloop
;----------------------------------------------------------------------
; variable/data section
o
rg $1030
array ds.w 1 ; address of the array
length ds.b 1 ; length of the array
org $1040
fl
db
1
fl fl $00 lid $ff i lid
ov
fl
ow
d
s.
b
1
; over
fl
ow
fl
ag.
$00
= va
lid
,
$ff
=
i
nva
lid
sum ds.w 1 ; 2-byte sim of unsigned numbers in the array
;----------------------------------------------------------------------
; code section
;
code
section
org $2000
movw #0,sum ; 1. clear sum
movb #0,ovflow ; clear ovflow
l
dd #0
;
clear A and B
ldx array ; 2. point to the start of the array
ldab length
tfr D,Y ; 3. get copy of array length
loop beq done ; 4. no more elements?
l
cl
ra
ldab 0,X ; load an element to B
addd sum ; 5. sum = sum + element
std sum ; store D to sum
bcc
sum ok
; 6 no overflow?
bcc
sum_ok
;
6
.
no
overflow?
movb #$ff,ovflow ; 7. indicate overflow
sum_ok inx ; 8. prepare for next loop
dey ; "
b
ra loop
;
g
o to "loop"
done swi
Changes for Two-Byte Length
H lik l i i d fl i th i i l ?
•
H
ow
lik
e
l
y
i
s uns
i
gne
d
over
fl
ow
i
n
th
e or
i
g
i
na
l
program
?
Cannot happen. The largest possible sum is $FE01 ($FF * $FF).
Wh difi i d d h dl
bl h?
•
Wh
at mo
difi
cat
i
ons are nee
d
e
d
to
h
an
dl
e two-
b
yte
l
engt
h?
Replace DS.B 1 with DS.W 1
Re
p
lace LDAB
,
TFR with LDY
p
,
;----------------------------------------------------------------------
; variable/data section
o
rg $1030
array ds.w 1 ; address of the array
lengthlength ds.wds.w 11; length of the array
org $1040
fl
db
1
fl fl $00 lid $ff i lid
ov
fl
ow
d
s.
b
1
; over
fl
ow
fl
ag.
$00
= va
lid
,
$ff
=
i
nva
lid
sum ds.w 1 ; 2-byte sim of unsigned numbers in the array
;----------------------------------------------------------------------
; code section
;
code
section
org $2000
movw #0,sum ; 1. clear sum
movb #0,ovflow ; clear ovflow
l
dd #0
;
clear A and B
ldx array ; 2. point to the start of the array
; ldab length
; tfr D,Y ; 3. get copy of array length
ldyldy lengthlength ; 3. get copy of array length
l
b
d
4l?
l
oop
b
eq
d
one
;
4
. no more e
l
ements
?
clra
ldab 0,X ; load an element to B
addd sum ; 5. sum = sum + element
std
sum
;
store D to sum
std
sum
;
store
D
to
sum
bcc sum_ok ; 6. no overflow?
movb #$ff,ovflow ; 7. indicate overflow
sum_ok inx ; 8. prepare for next loop
d
ey
;
"
bra loop ; go to "loop"
done swi
Changes for Signed Numbers
;
----------------------------------------------------------------------
;
; program
org $2000
movw #0,sum ; 1. clear sum
movb #0,ovflow ; clear ovflow
l
dd #0
;
clear A and B
ldx array ; 2. point to the start of the array
ldab length
tfr D,Y ; 3. get copy of array length
loop
beq
done
;
4 no more elements?
loop
beq
done
;
4
.
no
more
elements?
clra
ldab 0,X ; load an element to B
bplbpl skipskip ; check if B is positive
ldaaldaa
#$ff#$ff
;
extend the sign bit if B is negative
ldaaldaa
#$ff#$ff
;
extend
the
sign
bit
if
B
is
negative
skipskip
addd sum ; 5. sum = sum + element
bvcbvc sum_ok ; 6. no overflow?
movb #$ff,ovflow ; 7. indicate overflow
sum
_
ok
; std clears the v bit so std is moved to here
stdstd sumsum ; store D to sum
inx ; 8. prepare for next loop
dey
"
dey
;
"
bra loop ; go to "loop"
done swi
Questions?
Wrap-up
Mltil
ii
ith ti t dd d bt t l
What we’ve learned
•
M
u
lti
p
l
e-prec
i
s
i
on ar
ith
me
ti
c
t
o a
dd
an
d
su
bt
rac
t
l
arge
numbers.
Mti
iti i bl
•
M
ore prac
ti
ce wr
iti
ng programs
i
n assem
bl
y
What to Come
Ad d ith ti i t ti
•
Ad
vance
d
ar
ith
me
ti
c
i
ns
t
ruc
ti
ons
•Boolean logic instructions