Anda di halaman 1dari 16

Lecture 9:

Assembly Language
g g Example

Todays Topics

Fl
Flowcharts
h t

Practice assembly programming

Flowcharts

There are other


Th
th more sophisticate
hi ti t methods
th d off representing
ti
programs.

Flowcharts
Fl
h t work
k well
ll ffor software
ft
written
itt in
i assembly
bl code
d
level.

We will
W
ill see llogical
i lb
building
ildi
bl
blocks
k iin fl
flowchart
h
fformats along
l
with assembly code templates.

Flowcharts
Meaning of symbols

There are many more symbols


Th
b l other
th than
th
these.

Sh
Shapes
may be
b different.
diff
t

In this course, those four symbols are


pretty much
h all
ll we need.
d

Start or End

flow
Decision

Process

Flowcharts
If-Then-Else
Sett CCR bit
S
bits ffor decision
d i i
Bxx Process A
Process B code
Color of Pill
is RED?

TRUE
FALSE
Matrix

RealWorld

If(PillColor == RED)
RealWorld
Else
Matrix
Example:
ldaa
PillColor
cmpa
#RED
bne
lmatrix
RealWorld
bra
lskip
lmatrix: Matrix
lskip: ...

Flowcharts
While-Do
Sett CCR bit
S
bits ffor decision
d i i
Bxx past BRA
Process code
Set CCR bits for decision
BRA to Bxx
KnowThis?
TRUE

FALSE
S

StudyMore

Example:
ldaa
KnowThis
lloop: cmpa
#YES
beq
lnext
StudyMore
ldaa
KnowThis
bra
lloop
p
lnext: NextStep

Flowcharts
Repeat-Until
Process code
P
d
Set CCR bits
Bxx to Process code

DoSomething

Is the Job
done?
TRUE

FALSE

Example:
loop: DoSomething
ldaa
Result
beq
ldone
bra
loop
ldone:

Flowcharts
Case
nTemp
< 20

TRUE

TurnOnHeater

FALSE
nTemp
< 70

TRUE

FlashIndicator

FALSE
nTemp
< 100
FALSE
Alarm

TRUE

T
TurnOnCooler
O C l

Set CCR bits


Bxx to Process 1
Set CCR bits
Bxx to Process 2

Set CCR bits


Bxx to Process N
Default Process code
BRA past Process N code
Process 1 code
BRA past process N code
Process 2 code
BRA past Process N code

Process N code
Example:
ldaa nTemp
cmpa #20
bhs
l1
TurnOnHeater
bra
lres
l1: cmpa #70
bhi
l2
FlashIndicator
bra
lres
l2:
cmpa #100
bhi
l3
TurnOnCooler
bra
lres
l3: Alarm
lres:

Flowchart Guidelines

D nott refer
Do
f to
t registers
i t
in
i the
th flowchart
fl
h t

Arrows should never cross

They will not need to if the flowchart represents a structured


program

The purpose is
Th
i to
t remove any questions
ti
about
b th
how to
t
program and understand the algorithm, and this usually
determines when the flowchart contains enough detail.

Assembly Example

Convertt an array off 4-byte


C
4 b t Bi
Big-Endian
E di values
l
tto an array off
Little-Endian values.

L t
Let

$1000 hold the address of the array of Big-Endian values,


y for the Little-Endian values,,
$1002 hold the address of the array
$1004 hold the two-byte length of numbers to convert.

Write an assembly
yp
program
g
to implement
p
these requirements.
q

Start

1: Point to the first BE item


2: Point to the first LE item
3: Make a copy of the length

4. There are no
more elements
FALSE
5: Copy 1st BE byte to 4th LE byte
6: Copy 2nd BE byte to 3rd LE byte
7: Copy 3rd BE byte to 2nd LE byte
8: Copy 4th BE byte to 1st LE byte
9: Point to next elements & dec length

End

TRUE

Start

Bend
LEnd
Length
TmpLen

1: Point to the first BE item


2: Point to the first LE item
3: Make a copy of the length

4. There are no
more elements

Loop
TRUE

FALSE
5: Copy 1st BE byte to 4th LE byte
6: Copy 2nd BE byte to 3rd LE byte
7: Copy 3rd BE byte to 2nd LE byte
8: Copy 4th BE byte to 1st LE byte
9: Point to next elements & dec length

End

Done

org
ds.w
ds.w
ds w
ds.w
ds.w
org
ldx
ldy
ldd
std
beq
ldaa
staa
ldaa
staa
ldaa
staa
ldaa
staa
inx
inx
inx
inx
iny
iny
iny
iny
ldd
subd
bra
swi

$1000
1
1
1
1
$2000
BEnd
Lend
Length
TmpLen
Done
0 x
0,x
3,y
1,x
2,y
2 x
2,x
1,y
3,x
0,y

; 3
;
;
;
;
;
;
;
;
;

3
2
3
2
3
2
3
2
1

; 1

TmpLen
#$0001
Loop

; 3

Start

org
Bend
ds.w
LEnd
ds.w
Length ds.w

1: Point to the first BE item


2: Point to the first LE item
3: Make a copy of the length

4. There are no
more elements

TRUE

Loop

FALSE
5: Copy 1st BE byte to 4th LE byte
6: Copy 2nd BE byte to 3rd LE byte
7: Copy 3rd BE byte to 2nd LE byte

Done

org
ldx
ldy
ldd
beq
movb
movb
movb
movb
leax
leay
subd
bd
bra
swi

$1000
1
1
1
$2000
BEnd
Lend
Length
Done
0,x,3,y ; 5
1,x,2,y
2,x,1,y
3,x,0,y
4,x
; 2
4,y
#1
; 2
Loop

8: Copy 4th BE byte to 1st LE byte


9: Point to next elements & dec length

End

Note:Youcansave10(=3+2x2 +3)cycles.

Questions?

Wrap-up
What weve learned

Fl
Flowcharts
h t

Templates will be greatly helpful.

A
Assembly
bl program example
l

What to Come

A ith ti instructions
Arithmetic
i t
ti

Logic instructions

Anda mungkin juga menyukai