ECE 2560 - Lecture 06 A First Program

advertisement
ECE 2560
L7 – A First Program
Department of Electrical and
Computer Engineering
The Ohio State University
ECE 3561 - Lecture 1
1
A First Program
The first program
 The algorithm
 HLL structures to assembler
 The coding of bubble sort


Will be working through slides and code composer in
class together.
ECE 3561 - Lecture 1
2
The program specification

“Anytime you write software you need a
specification for that software.”



;Joanne DeGroat
;This is comment
(Include your name)
What is the first program specification.

Write a MSP430 assembler language program
that implements the bubble sort algorithm to
sort 8 values in memory at location labeled by
xxx. The values are sorted using memory and
registers and then stored back to the locations.
ECE 3561 - Lecture 1
3
The algorithm

The bubble sort algorithm (4 locations
shown) – On 1st pass have n locations to
sort

On 2nd pass have n-1 locations to sort.
ECE 3561 - Lecture 1
4
Now code it



Start with a HLL pseudocode

n=val
;number of items in list

done=FALSE

While NOT done repeat

done=TRUE;

FOR i = 1 to n-1 Loop

IF list(i)>list(i+1) THEN

temp = list(i);

list(i) = list(i+1);

list(i+1) = temp;

done = FALSE;

END IF;

END Loop;

n=n-1;

IF n=1 THEN done=TRUE;

END While;
;exchange items
Now translate to assembler
Note: code repeats loop if any items are exchanged. If no exchanges – exit.
ECE 3561 - Lecture 1
5
Translating
How do you translate HLL structures to
assembler
 STRAIGHT LINE CODE






temp = list(i)
list(i) = list(i+1)
list(i+1) = temp
done=FALSE
Could be done using data memory or
registers and the mov instruction.
ECE 3561 - Lecture 1
6
IF THEN ELSE

Decision structures


Have to decide where the test variables are
Example: IF (A < B) THEN xxx ELSE yy



Decide where A and B are
Also look at the possible jumps
Code order






TEST ;set the CCR bits
Branch to else cond when F(or after, if no ELSE)
Code of THEN condition
Branch to after if ELSE
ELSE Code of ELSE condition
after continuation of code
ECE 3561 - Lecture 1
7
FOR Loop
Need location for loop control variables
 FOR i IN 1 to n LOOP

STATEMENTS in Loop
 END FOR;

Need location for i – probably memory
 Need location for n – probably memory

ECE 3561 - Lecture 1
8
FOR Loop assembler

In .data area



.word 0x0001
.word 0x0004
;the loop counter
;the loop limit
The coding - will run the code in the loop at least once






i
n
tol
CODE WITHIN loop
inc
i
cmp
i,n ;are we at the end?
;will compute n-i
JGE
tol
;BUT WHICH JUMP?
If it was FOR i = 1 to 4




First time i=1, at end before compare i=2 and 4-2=2
Then
i=2, at end i=3 and 4-3=1
Then
i=3, at end i=4 and 4-4=0
Then
i=4, at end i=5 and 4-5=-1
ECE 3561 - Lecture 1
9
The Jumps

JGE – is Jump if greater or equal

Finding the correct jump requires some thought.

Note: There is no jump if less than or equal.
There is JL, jump is less, and JEQ, jump if
equal.
How to accomplish jump if less than or equal?
Discussion


ECE 3561 - Lecture 1
10
While loop

Has the form




WHILE condition REPEAT
Some statement in here modifies condition
END WHILE;
Translating to assembler



Have to set up condition to produce T/F result
Say condition is simply NOT done
done is a 0 (FALSE) or 1 (True)
ECE 3561 - Lecture 1
11
In assembler

For the example
.data
 done .word


0
Code

rpt




tst
done
jne cont
BODY OF CODE
jmp rpt
cont
ECE 3561 - Lecture 1
12
Now code it

Straight code exchange items (not set up in loop)












;bls is the list in memory
;have i=element of list you wish to access
;will exchange with item i+1
mov i,R7
dec
R7
clrc
rlc
R7
;mult by 2 for word data
add
#bls,R7 ;R7 address of item(i)
mov @R7,temp ;list(i) to temp
mov 2(R7),0(R7) ;list(i)=list(i+1)
mov temp,2(R7) ; temp=list(i+1)
clr
done
ECE 3561 - Lecture 1
13
Now code the if

What is the test? List(i) > list(i+1)















Remember that cmp a,b computes b-a
and it does so in two’s complement arithmetic
The means that 0xFFFF (-1) is < 0x0001 (+1)
;i is value of loop element you wish to access
; in memory and current element
ifstmt
mov
i,R7
dec
R7
clrc
;clear the carry bit
rlc
R7
;rotate left with carry (x2)
add
#bls,R7
;address of element in R7
cmp
@R7,2(R7)
jge
noexch
4 statements to exchange the elements and set done
Noexch
Note that some the changes to the straight code come from having to set
up the compare.
ECE 3561 - Lecture 1
14
Put code in loop

Loop code








tol
;i in memory – current loop count
;n in memory – limit
;done is also in memory
mov
#1,i
STRAIGHT LINE CODE
inc
i
cmp
i,n
jge
tol
ECE 3561 - Lecture 1
15
Code the While Loop

Now code the while loop













; done is a boolean in memory
mov
#7,n
;set number of items in list -1
trpt
tst
done
jne
cont
mov
#1,done ;set done=TRUE
Loop code of previous slide
dec
n
cmp
i,n
jne
trpt
mov
#1,done ;set done=TRUE
jmp
trpt
cont
nop
; program should be done
Note change here was the you start with n-1 rather then n
ECE 3561 - Lecture 1
16
Try it out

Code this into assembler in code composer.
 This information on the slides in not 100% but it
is very close. It will be tested on the 430.
 See webpage for the assignment.

You can load the values into R8 to R15 to see
that they are sorted or use the memory browser
to show the values and see that they are sorted.
 In fact you can use the memory browser to
watch the bubble sort in action.
ECE 3561 - Lecture 1
17
Notes from after class

This emphasizes the point that I have been making in
class. Look at the documentation as the microcontrollers
all have subtle differences.
 No place does the documentation point out that the cmp
instruction treats data as 2’s complement values, i.e., - and
+ values. The documentation does indicate this but does
not state it explicitly. It says the function of the cmp is to
evaluate
dst + .NOT.src + 1 to set the CCR bits NVCZ
 In class the list was FF00h which represents 0100h, i.e.,
+256. This has 1 added to it, result=257. So Z not set, V
not set, C not set, and N not set.
 Many microcontrollers treat values in compares as
unsigned integer values.
ECE 3561 - Lecture 1
18
Further notes
In 16 bits you can have values from
-215 to +215-1 or -32768 to +32767
 In 8 bits the 2’s complement range is
-27 to 27-1 or -128 to +127


There will be a 1 in the msb of all 2’s
complement binary number that represent a
negative number.
ECE 3561 - Lecture 1
19
Download