Transcript ppt version

(revisited)
Write a program to calculate x**y, given x and y.
inputs: x (w – because of the multiply instruction)
y (b – it is a counter)
output: ans (l – because of the multiply instruction)
main
loop
skip
x
y
ans
org
move.w
move.w
move.b
beq
mulu
sub.b
bne
move.l
stop
dc.w
dc.b
ds.l
end
$400
x,D0
x,D2
y,D1
skip
D0,D2
#1,D1
loop
D2,ans
#$2700
5
3
1
main
get base
and make copy
get exponent
if zero, skip calc
do product
decrement counter
save x**y
We have some problems:
Valid flags for signed arithmetic
V = 1 → result incorrect
→ signed overflow
0 → result correct
Z = 1 → result is zero
0 → result is not zero
N = 1 → result is negative
0 → result is positive
(includes zero)
e.g. A – B
if N=0
A–B
if N=1
A–B
0
0
if N=V
if N≠V
B
B
A
A
if N=V and Z=0 A B
if N≠V or Z=1
A B
Decision Structures

in pseudo-code:
…
if condition then
… code A
end if
… code B

“if condition then”
decomposes into a series
of operations
1) test condition
→ sets flags
2) branch on the flags

… if-then
in assembly language
Decision Structures

to “test” condition:
… testing conditions
 arithmetic
CMP <ea>,Dn
 [destination] - [source]
 logical
e.g.
 shifts,
rotates
 data movement
 compare,
test
move.b
cmp.b
#5,D0
#6,D0
TST <ea>
 compare [destination] to 0
≡ [destination] – 0
e.g.
move.b
tst.b
#-1,D0
D0
Conditional Branch


…Bcc
M68000 Assembly Language [p16, N. Znotinas]
conditional branches, an alternate view:
BHI |
BCC |
BHS*|
BNE |
BVC |
BPL |
BGE |
BGT |
BLS
BCS
BLO*
BEQ
BVS
BMI
BLT
BLE
Br high
|Br low or same
Br C clear
|Br C set
Br high/same |Br low
Br not equal |Br equal
Br V clear
|Br V set
Br plus
|Br minus
Br ≥
|Br <
Br >
|Br ≤
* depends on assembler if synonym accepted
Decision Structures

in pseudo-code:
…
if condition then

… code A
else
… code B
end if
… code C
Assembly code logic should flow
from top to bottom like a waterfall.
… if-then-else
in assembly language
Testing for out-of-range data/results

input data and
calculated data may
be out-of-range,
i.e. too large to store
correctly

you are responsible
for testing for out-ofrange conditions

you would not test …
perform
arithmetic
input
operation
data
↓
result/data
in range? → error
↓
continue
processing
Testing for out-of-range results
e.g. ** technique 1
…
ADD.W D0,D1
B
Ans_OK
e.g. ** technique 2
…
ADD.W D0,D1
B
V_err
… code to put
… out err msg
… next code
… section
…
…
JMP
END_M
Ans_OK … next code
… section
…
END_M
JMP
END_M
V_err … code to put
… out err msg
…
END_M
Loops

in pseudo-code:
…
while condition do
… code A
end while
… code B
… conditional loops

in assembly language
e.g. If a 16 bit number is negative, generate the
absolute value of the number. Store the result in
the number’s original memory location.
e.g. Find the larger of two 32 bit numbers.
Reading:

M68000 Assembly Language [pdf, 92p; N. Znotinas]





Look at the Bcc command and relate the use of the condition
codes in the table to the relationships we developed in the last
two lectures. The relationships are the same but written in
different formats.
Look at the unconditional branch commands: BRA, JMP
review the operation of the various binary add (ADD, ADDA,
ADDI, ADDQ) and subtract (SUB, SUBA, SUBI, SUBQ)
instructions
review the operation of the test (TST) instruction and the
various forms of the compare (CMP, CMPA, CMPI) instruction
review the operation of the MULS|MULU (multiply) and
DIVS|DIVU (divide) instructions
Expectations:
 starting with assignment 4, we will expect arithmetic results to be
checked for out-of-range results when appropriate
 you can write any decision structure (if, if-then-else, if-elseif,
case) in assembly language
 you can write a simple looping structure in assembly language
 you can select the correct conditional branch for any given signed
or unsigned test