Microsoft 10 Basic_Arithmetic_Instructionsx Basic Arithmetic Instructions

User Manual:

Open the PDF directly: View PDF PDF.
Page Count: 15

DownloadMicrosoft  - 10-Basic_Arithmetic_Instructionsx 10-Basic Arithmetic Instructions
Open PDF In BrowserView PDF
Lecture 10:
Basic Arithmetic Instructions

Today’s Goals
•

R i
Review
Additi
Addition and
dS
Subtraction
bt ti

•

Use Multiple Precision arithmetic to add and subtract large
numbers.
b

•

Practice writing assembly programs.

Addition and Subtraction
From Lecture 8
•

•

8 bit addition



ABA: (A) + (B)  A; Note that there is no AAB instruction!
ADDA: (A) + (M)  A



ADDB (B) + (M)  B
ADDB:




ADCA: (A) + (M) + C  A
ADCB: (B) + (M) + C  B

• ADDA $1000
• ADDB #10

8 bit subtraction






•

We will use ADCA(B) and
SBCA(B)
( ) to do multiprecision addition or
subtraction

SBA: (A) – (B)  A; Subtract B from A (Note: not SAB instruction!)
SUBA: (A) – (M)  A; Subtract M from A
SUBB: (B) – (M)  B
SBCA: (A) – (M) – C  A
SBCB: (B) – (M) – C  B
There is a pattern that make you

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
ABX:
ABY: (B) + (Y)  Y

be easy to remember the
instructions!!!

1. The last letter in these
instructions is the destination!
2. Also it comes to the first in the
operation

Precision?
•

The term
Th
t
precision
i i
i often
is
ft used
d tto refer
f to
t the
th size
i off a unit
it
of data manipulated by the processor.

•

Single-precision
Si
l
i i
refers
f
to
t instructions
i t
ti
that
th t manipulate
i l t one
byte at a time.
 ADDA, ADDB, ABA, SUBA, SUBB, SBA

•

Double-precision refers to two-byte operation.
 ADDD,, SUBD
 ABX: (B) + (X)  X,

•

Multi-precision
p

ABY: (B) + (Y)  Y

 Adding and subtracting numbers longer than single precision
introduce an issue.
 Carries and borrows need to propagate through a number.
number

Example
Adding two quadruple-precision numbers
•

Multi-precision
M
lti
i i
addition
dditi
iis performed
f
d one b
byte
t att a ti
time,
beginning with the least significant byte.
• 92FF45B716
• 6D325D8816

1

1

ORG $1200
num1 DC.B $92, $FF, $45, $B7
num2 DC.B $6D, $32, $5D, $88
ans DS.B 4

1

92 FF 45 B7
6D 32 5D 88
+ ----------00 31 A3 3F

ORG $2000
LDAA num1+3
;
ADDA num2+3
;
STAA ans+3
;
LDAA num1+2
;
ADCA num2+2
;
STAA ans+2
;
LDAA num1+1
;
ADCA num2+1
;
STAA ans+1
;
LDAA num1 ; 10
ADCA num2 ; 11
STAA ans ; 12
SWI
; 13

1
2
3
4
5
6
7
8
9

Program Trace
ORG $1200
num1 DC.B $92,$FF,$45,$B7
num2 DC.B $6D,$32,$5D,$88
ans DS.B 4
ORG $2000
LDAA num1+3
;
ADDA num2+3
;
STAA ans+3
;
LDAA num1+2
;
ADCA num2+2
;
STAA ans+2
;
LDAA num1+1
;
ADCA num2+1
;
STAA ans+1
;
LDAA num1 ; 10
ADCA num2 ; 11
STAA ans ; 12
SWI
; 13

1
2
3
4
5
6
7
8
9

Trace

Line

PC

A

N

Z

V

C

1

1

2003

B7

1

0

0

-

2

2

2006

3F

0

0

1

1

3

3

2009

3F

0

0

0

1

4

4

200C

45

0

0

0

1

5

5

200F

A3
3

1

0

1

0

6

6

2012

A3

1

0

0

0

7

7

2015

FF

1

0

0

0

8

8

2018

31

0

0

0

0

9

9

201B

31

0

0

0

1

10

10

201E

92

1

0

0

1

11

11

2021

00

0

1

0

1

12

12

2022

00

0

1

0

1

13

13

2024

-

-

-

-

-

Another Example
•

Calculate
C
l l t a two-byte
t
b t sum off an array off one-byte
b t unsigned
i
d
numbers.

•

Requirements
R
i
t
 Variable ovflow should be $00 if the sum is valid. Otherwise, $ff.
 The address of the array
y of one-byte
y unsigned
g
integers
g
is
supplied at $1030.
 The length of the array is a one-byte value supplied in $1032.
 Ovflow must be assigned to address $1040.
$1040
 The sum is returned in locations $1041 and $1042.

Flowchart
SEQ
Start

1: clear sum and
overflow

2: point to the
start of the array

3: get copy of
array length
WDO
true

9: End

SEQ

ITE

4: There are no
more elements
false
5: sum <=
sum + element

true

6 no overflow
6:
fl
occurs
false
7: indicate
overflow

8: prepare for
next loop

;---------------------------------------------------------------------; variable/data section
org $1030
array
ds.w 1
; address of the array
length
ds.b 1
; length of the array

ovflow
fl
sum

org $1040
d b1
ds.b
; overflow
fl
fl
flag. $00 = valid,
lid $ff = i
invalid
lid
ds.w 1
; 2-byte sim of unsigned numbers in the array

;---------------------------------------------------------------------; code section
org $2000
movw #0,sum
; 1. clear sum
movb #0,ovflow
;
clear ovflow
ldd #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?
clra
l
ldab 0,X
; load an element to B
addd sum
; 5. sum = sum + element
std sum
; store D to sum
bcc sum_ok
sum ok
; 6
6. no overflow?
movb #$ff,ovflow
; 7. indicate overflow
sum_ok
inx
; 8. prepare for next loop
dey
;
"
bra loop
; go to "loop"
done swi

Changes for Two-Byte Length
•

H
How
likely
lik l iis unsigned
i
d overflow
fl
iin th
the original
i i l program??

 Cannot happen. The largest possible sum is $FE01 ($FF * $FF).

•

Wh modifications
What
difi i
are needed
d d to h
handle
dl two-byte
b
llength?
h?
 Replace DS.B 1 with DS.W 1
 Replace
p
LDAB,, TFR with LDY

;---------------------------------------------------------------------; variable/data section
org $1030
array
ds.w 1
; address of the array
length
ds.w 1
; length of the array

ovflow
fl
sum

org $1040
d b1
ds.b
; overflow
fl
fl
flag. $00 = valid,
lid $ff = i
invalid
lid
ds.w 1
; 2-byte sim of unsigned numbers in the array

;---------------------------------------------------------------------; code section
org $2000
movw #0,sum
; 1. clear sum
movb #0,ovflow
;
clear ovflow
ldd #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
ldy length
; 3. get copy of array length
l
loop
b
beq
d
done
; 4.
4 no more elements?
l
?
clra
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?
movb #$ff,ovflow
; 7. indicate overflow
sum_ok
inx
; 8. prepare for next loop
dey
;
"
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
ldd #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.
4 no more elements?
clra
ldab 0,X
; load an element to B
bpl skip
; check if B is positive
ldaa #$ff
; extend the sign bit if B is negative
skip
addd sum
; 5. sum = sum + element
bvc sum_ok
; 6. no overflow?
movb #$ff,ovflow
; 7. indicate overflow
sum_ok
; std clears the v bit so std is moved to here
std sum
; store D to sum
inx
; 8. prepare for next loop
dey
;
"
bra loop
; go to "loop"
done swi

Questions?

Wrap-up
What we’ve learned
•

Multiple-precision
M
lti l
i i
arithmetic
ith ti to
t add
dd and
d subtract
bt t llarge
numbers.

•

M
More
practice
ti writing
iti
programs in
i assembly
bl

What to Come
•

Ad
Advanced
d arithmetic
ith ti iinstructions
t
ti

•

Boolean logic instructions



Source Exif Data:
File Type                       : PDF
File Type Extension             : pdf
MIME Type                       : application/pdf
PDF Version                     : 1.5
Linearized                      : Yes
Author                          : Jaerock
Create Date                     : 2010:01:31 16:59:37-05:00
Modify Date                     : 2010:01:31 16:59:37-05:00
XMP Toolkit                     : Adobe XMP Core 4.2.1-c041 52.342996, 2008/05/07-20:48:00
Creator Tool                    : PScript5.dll Version 5.2.2
Producer                        : Acrobat Distiller 9.0.0 (Windows)
Format                          : application/pdf
Title                           : Microsoft PowerPoint - 10-Basic_Arithmetic_Instructions.pptx
Creator                         : Jaerock
Document ID                     : uuid:71d8a677-f4a4-453a-83a6-26d9e7582e2d
Instance ID                     : uuid:cb99a81e-d65d-4c37-a66b-4b9cecda8cdf
Page Count                      : 15
EXIF Metadata provided by EXIF.tools

Navigation menu