L7 - PowerPoint PPT Presentation

About This Presentation
Title:

L7

Description:

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

Number of Views:26
Avg rating:3.0/5.0
Slides: 20
Provided by: Joann276
Category:
Tags: assembler | pass

less

Transcript and Presenter's Notes

Title: L7


1
L7 A First Program
ECE 2560
  • Department of Electrical and
  • Computer Engineering
  • The Ohio State University

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.

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.

4
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.

5
Now code it
  • Start with a HLL pseudocode
  • nval number of items in list
  • doneFALSE
  • While NOT done repeat
  • doneTRUE
  • FOR i 1 to n-1 Loop
  • IF list(i)gtlist(i1) THEN exchange
    items
  • temp list(i)
  • list(i) list(i1)
  • list(i1) temp
  • done FALSE
  • END IF
  • END Loop
  • nn-1
  • IF n1 THEN doneTRUE
  • END While
  • Now translate to assembler
  • Note code repeats loop if any items are
    exchanged. If no exchanges exit.

6
Translating
  • How do you translate HLL structures to assembler
  • STRAIGHT LINE CODE
  • temp list(i)
  • list(i) list(i1)
  • list(i1) temp
  • doneFALSE
  • Could be done using data memory or registers and
    the mov instruction.

7
IF THEN ELSE
  • Decision structures
  • Have to decide where the test variables are
  • Example IF (A lt 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

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

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
  • 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 i1, at end before compare i2 and
    4-22
  • Then i2, at end i3 and 4-31
  • Then i3, at end i4 and 4-40
  • Then i4, at end i5 and 4-5-1

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

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)

12
In assembler
  • For the example
  • .data
  • done .word 0
  • Code
  • rpt tst done
  • jne cont
  • BODY OF CODE
  • jmp rpt
  • cont

13
Now code it
  • Straight code exchange items (not set up in
    loop)
  • bls is the list in memory
  • have ielement of list you wish to
    access
  • will exchange with item i1
  • mov i,R7
  • dec R7
  • clrc
  • rlc R7 mult by 2 for word data
  • add bls,R7 R7 address of item(i)
  • mov _at_R7,temp list(i) to temp
  • mov 2(R7),0(R7) list(i)list(i1)
  • mov temp,2(R7) templist(i1)
  • clr done

14
Now code the if
  • What is the test? List(i) gt list(i1)
  • Remember that cmp a,b computes b-a
  • and it does so in twos complement arithmetic
  • The means that 0xFFFF (-1) is lt 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 _at_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.

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
  • inc i
  • cmp i,n
  • jge tol

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 tst done
  • jne cont
  • mov 1,done set doneTRUE
  • Loop code of previous slide
  • dec n
  • cmp i,n
  • jne trpt
  • mov 1,done set doneTRUE
  • jmp trpt
  • cont nop program should be done
  • Note change here was the you start with n-1
    rather then n

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.

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 2s 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,
    result257. So Z not set, V not set, C not set,
    and N not set.
  • Many microcontrollers treat values in compares as
    unsigned integer values.

19
Further notes
  • In 16 bits you can have values from -215
    to 215-1 or -32768 to 32767
  • In 8 bits the 2s complement range is -27 to
    27-1 or -128 to 127
  • There will be a 1 in the msb of all 2s
    complement binary number that represent a
    negative number.
Write a Comment
User Comments (0)
About PowerShow.com