Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 3561 - Lecture 1 1 L7 – A First Program Department of Electrical and Computer Engineering The Ohio State University ECE 2560.

Similar presentations


Presentation on theme: "ECE 3561 - Lecture 1 1 L7 – A First Program Department of Electrical and Computer Engineering The Ohio State University ECE 2560."— Presentation transcript:

1 ECE 3561 - Lecture 1 1 L7 – A First Program Department of Electrical and Computer Engineering The Ohio State University ECE 2560

2 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

3 The program specification “Anytime you write software you need a specification for that software.” ;Joanne DeGroat (Include your name) ;This is comment 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

4 The algorithm The bubble sort algorithm (4 locations shown) – On 1 st pass have n locations to sort On 2 nd pass have n-1 locations to sort. ECE 3561 - Lecture 1 4

5 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 ;exchange items 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 Note: code repeats loop if any items are exchanged. If no exchanges – exit. ECE 3561 - Lecture 1 5

6 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

7 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

8 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

9 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 inci 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

10 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

11 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

12 In assembler For the example.data done.word 0 Code rpttstdone jne cont BODY OF CODE jmprpt cont ECE 3561 - Lecture 1 12

13 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 decR7 clrc rlcR7 ;mult by 2 for word data add#bls,R7 ;R7 address of item(i) mov@R7,temp ;list(i) to temp mov2(R7),0(R7) ;list(i)=list(i+1) movtemp,2(R7) ; temp=list(i+1) clrdone ECE 3561 - Lecture 1 13

14 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 movi,R7 decR7 clrc;clear the carry bit rlcR7;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 14

15 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 cmp i,n jgetol ECE 3561 - Lecture 1 15

16 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 tstdone jnecont mov#1,done ;set done=TRUE Loop code of previous slide decn cmpi,n jnetrpt mov#1,done ;set done=TRUE jmptrpt cont nop; program should be done Note change here was the you start with n-1 rather then n ECE 3561 - Lecture 1 16

17 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

18 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

19 Further notes In 16 bits you can have values from -2 15 to +2 15 -1 or -32768 to +32767 In 8 bits the 2’s complement range is -2 7 to 2 7 -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 ppt "ECE 3561 - Lecture 1 1 L7 – A First Program Department of Electrical and Computer Engineering The Ohio State University ECE 2560."

Similar presentations


Ads by Google