09 Assembly Example
09 Assembly Example
Todays Topics
Fl Flowcharts h t Practice assembly programming
Flowcharts
There are other Th th more sophisticate hi ti t methods th d of f representing ti programs. Flowcharts Fl h t work k well ll f for software ft written itt in i assembly bl code d level. We will W ill see l logical i lb building ildi bl blocks k i in fl flowchart h f formats along l with assembly code templates.
Flowcharts
Meaning of symbols
Start or End
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
flow
Decision
Process
Flowcharts
If-Then-Else
Set S t CCR bit bits f for decision d i i Bxx Process A Process B code
Color of Pill is RED?
If(PillColor == RED) RealWorld Else Matrix Example: ldaa PillColor cmpa #RED bne lmatrix RealWorld bra lskip lmatrix: Matrix lskip: ...
Flowcharts
While-Do
Set S t CCR bit bits f for 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 Example: loop: DoSomething ldaa Result beq ldone bra loop ldone:
DoSomething
FALSE
Flowcharts
Case
nTemp < 20 FALSE nTemp < 70 FALSE nTemp < 100 FALSE Alarm TRUE T TurnOnCooler O C l TRUE FlashIndicator TRUE
TurnOnHeater
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 not Do t refer 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
Convert C t an array of f 4-byte 4 b t Bi Big-Endian E di values l t to an array of f 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.
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
TRUE
End
Start
1: Point to the first BE item 2: Point to the first LE item 3: Make a copy of the length
Loop
TRUE
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
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
; 3
Start
1: Point to the first BE item 2: Point to the first LE item 3: Make a copy of the length
org Bend ds.w LEnd ds.w Length ds.w 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
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
TRUE
Loop
Done
End
Note:Youcansave10(=3+2x2 +3)cycles.
Questions?
Wrap-up
What weve learned Fl Flowcharts h t
Templates will be greatly helpful.
What to Come
A ith ti instructions Arithmetic i t ti Logic instructions