180 likes | 271 Views
ECE 2560. L7 – A First Program. Department of Electrical and Computer Engineering The Ohio State University. A First Program. The first program The algorithm HLL structures to assembler The coding of bubble sort
E N D
ECE 2560 L7 – A First Program Department of Electrical and Computer Engineering The Ohio State University ECE 3561 - Lecture 1
A First Program • The first program • The algorithm • HLL structures to assembler • The coding of bubble sort • UPDATED 2/6/2014 after Wednesday’s class Changed points look like this. ECE 3561 - Lecture 1
The program specification • “Anytime you write software you need a specification for that software.” • Joanne DeGroat • 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
The algorithm • The bubble sort algorithm (4 locations shown) • On 2nd pass have n-1 locations to sort. ECE 3561 - Lecture 1
Now code it • Start with a HLL pseudocode • n=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; • Now translate to assembler ECE 3561 - Lecture 1
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
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
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
FOR Loop assembler • In .data area • i .word 0x0001 ;the loop counter • n .word 0x0004 ;the loop limit • The coding - will run the code in the loop at least once • tol CODE WITHIN loop • inc i • cmpi,n ;are we at the end? • ;will comput 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
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
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
In assembler • For the example • .data • done .word 0 • Code • rpttst done • jnecont • BODY OF CODE • jmprpt • cont ECE 3561 - Lecture 1
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) ;list(i+1)=temp • clr done ECE 3561 - Lecture 1
Now code the if • What is the test? List(i) > list(i+1) • Remember that cmpa,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 • ifstmtmov 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) • jgenoexch • 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
Put code in loop • Loop code • ;i in memory – current loop count • ;n in memory – limit • ;done is also in memory • mov #1,i • tol STRAIGHT LINE CODE • inci • cmpi,n • jgetol ECE 3561 - Lecture 1
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 • trpttst done • jnecont • mov #1,done ;set done=TRUE • Loop code of previous slide • decn • cmpi,n • jnetrpt • mov #1,done ;set done=TRUE • jmptrpt • contnop ; program should be done • Note change here was the you start with n-1 rather then n ECE 3561 - Lecture 1
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
Notes from after clas • 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