Efficient Algorithm Development - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Efficient Algorithm Development

Description:

Efficient Algorithm Development – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 25
Provided by: ecstCs
Category:

less

Transcript and Presenter's Notes

Title: Efficient Algorithm Development


1
Chapter 6 Efficient Algorithm Development
Notice Slides in this series originally,
provided by B.Britton, have been modified by
R.Renner to reflect Spring 2006 course content.
2
Classroom Exercises to Strengthen Your Mental
Muscle
  • The class is presented with a challenging
    assembly language programming exercise.Everyone
    individually develops a pseudo- code solution to
    the exercise.
  • (5 minutes)Groups compare their pseudo-code
    and refine their algorithms.
  • (5 minutes)Everyone individually develops
    an assembly language solution to the
    problem.Groups review and compare each others
    code, looking for the
  • most efficient code in terms of the Figure of
    Merit (FM)

3
Logical Operators
4
Examples of AND Instruction
  • For these examples, assume the following masks
    are stored in registers D3, and D4
  • D3 ffffffc0 11111111111111111111111111111111
    1111111111000000
  • D4 0000003f 000000000000000000000000000000000
    000000000111111
  • To move a copy of D0 into D1 with the lower 6
    bits cleared, the instructions to accomplish this
    would be
  • move.l D0, D1
  • and.l D3, D1 D3 D1 ? D1

5
Example of OR instruction
  • For these examples, assume the following masks
    are stored in registers D3, and D4
  • D3 ffffffc0 11111111111111111111111111111111
    1111111111000000
  • D4 0000003f 000000000000000000000000000000000
    000000000111111
  • To move a copy of D0 into D1 with the lower 6
    bits set to one, the instructions to accomplish
    this would be
  • move.l D0, D1
  • or.l D4, D1 D4 D1 ? D1

6
Example of EOR instruction
  • For these examples, assume the following masks
    are stored in registers D3, and D4
  • D3 ffffffc0 1111111111111111111111111111111
    11111111111000000
  • D4 0000003f 000000000000000000000000000000000
    000000000111111
  • Suppose we want to complement the lower 6 bits of
    register D0, but leave all the other bits
    unchanged. the instructions to accomplish this
    would be
  • move.l D0, D1
  • eor.l D4, D1 D4 D1 ? D1

7
  • Suppose we want to know if the values in two
    registers D7 and D1 have the same sign. The
    instructions to accomplish this are
  • EOR D1, D7 The Most Significant Bit
    (MSB) of D7 will be
  • set to a 1 if D7 D1 have
    different signs
  • bge same_sign Branch if Positive ( if MSD of
    D7 is now a zero)

8
Functional Descriptions of Code Modules
  • A functional description will provide the
    information anyone needs to know if they are
    searching for a function that would be useful is
    solving some larger programming assignment. The
    functional description only describes what the
    function does, not how it is done. The functional
    description must explain how arguments are passed
    to the function and how results are returned. The
    following are example functional descriptions
  • Hexout(D1 value)
  • A 32-bit binary value is passed to the function
    in register D1 and the hexadecimal equivalent is
    displayed right justified.
  • Decout(D1 value)
  • A 32-bit binary value is passed to the function
    in register D1 and the decimal equivalent is
    displayed right justified.
  • Decin(D1 value, D0 status)
  • Reads a string of decimal digits from the
    keyboard and returns the 32-bit binary equivalent
    in register D1. If the string does not correctly
    represent a decimal number error status value 1
    is returned in register D0 otherwise the status
    value returned is 0 for a valid decimal number.

9
Headers
  • Example Main Program Header
  • Program 3 ltdescriptive namegt Programmer lt
    your namegt
  • Due Date mm dd, 2005 Course CSCI/EECE
    221
  • Last Modified mm dd hhmm

  • Overall Program Functional Description

  • Register Usage in Main
  • A0 Address of ...
  • D1 value of ...

  • Pseudocode Description


10
  • start org 1000
  • lea array, A0 Initialize address
    Parameter
  • move.l 4, D1 Length parameter
  • bsr Sum Branch to subroutine
  • move 17, D0 display string without CR LF
  • lea msg1, A1 load address of msg1 into A1
  • move.l D2, D1 Sum of positive returned in D2
  • trap 15 display the string Value
  • move 17, D0 display string without CR LF
  • lea msg2, A1 load address of msg2 into A1
  • move.l D3, D1 Sum of negative returned in D3
  • trap 15 display the string Value
  • move.b 9, D0
  • trap 15 Halt Simulator
  • CR EQU 0D
  • LF EQU 0A
  • array DC.L -400, 500, 800, -100
  • msg1 DC.B ' The sum of the positive values ', 0
  • msg2 DC.B CR, LF, ' The sum of the negative
    values ', 0

11
  • Example Function Header
  • Function Name Sum(X, N, SP, SN)
  • Least Modified month day hour minute

  • Functional Description
  • This function will find the sum of the positive
    values SP and
  • and the sum of the negative values SN in an
    array X of words of length N.

  • X is the address of an array passed to the
    function in A0.
  • N is the length of the array passed to the
    function in D1.
  • The function returns two values
  • (1) Sum of the positive elements SP passed
    back in D2.
  • (2) Sum of the negative elements SN passed
    back in D3.

  • Example Calling Sequence
  • lea array, A0
  • move 4, D1
  • bsr Sum


12
  • Algorithmic Description in Pseudocode
  • D2 0
  • D3 0
  • while( D1 gt 0 )do
  • D1 D1 - 1
  • D0 Mem(A0)
  • A0 A0 4
  • If (D0 gt 0) then
  • D2 D2 D0
  • else
  • D3 D3 D0
  • Return

  • Sum clr.l D2
  • clr.l D3 Clear D2 and D3
  • Loop
  • tst.l D1
  • ble Return If (D1 lt 0) Branch to Return
  • sub.l 1, D1 Decrement loop count

13
Exercise 6.1
  • Write a M68K assembly language program to
  • find the Sum of the first 100 words of data in
  • the memory data segment with the label chico.
  • Store the resulting sum in the next memory
  • location beyond the end of the array chico.

14
Exercise 6.1 (Pseudo Code)
  • A0 Address of "chico"
  • D0 0
  • For (D1 100 D1 gt 0 D1 D1- 1)
  • D0 D0 Mem(A0)
  • Mem(A0) D0

15
Exercise 6.1 (M68K Assembly Language)
  • Label Op-Code S1, S2/ Dest., Comments
  • start org 1000
  • lea chico, A0 Load address pointer
  • clr D0 Clear sum
  • move 100, D1 Initialize loop count
  • loop
  • add (A0), D0 D0 D0 MemA0
  • bvs error
  • subq 1, D1 Dec. loop count
  • bgt loop if (D1 gt 0) branch to loop
  • move D0, (A0) Store the result
  • error move 9, D0 end on completion or error
  • trap 15 End of program
  • chico DS.W 101
  • end start

16
Exercise 6.2
  • Write an efficient segment of M68K assembly
    language code to transfer a block of 100 words
    starting at memory location SRC to another area
    of memory beginning at memory location DEST.

17
Exercise 6.2 (Pseudo Code)
  • A1 SRC means Address of
  • A2 DEST
  • for (D0 100 D0 gt 0 D0 D0 -1)
  • Mem(A1) ? Mem(A2)

18
Exercise 6.2 (M68K Assembly Language)
  • Label Op-Code S1, S2/ Dest., Comments
  • org 1000
  • start
  • lea SRC, A1 A1 SRC
  • lea DEST,A2 A2 DEST
  • move 100, D0 D0 100
  • loop
  • move (A1), (A2)
  • subq 1, D0 D0 D0 - 1
  • bgt loop if (D0 gt 0) Branch to loop
  • move 9, D0
  • trap 15
  • SRC DS.W 100
  • SRC DC.W 0a,loop,16,' We can fill memory with
    data',0
  • DEST DS.W 100
  • end start

19
Exercise 6.3
  • Write a M68K function which accepts an integer
    word in register D0 and returns its absolute
    value in D0.
  • Also show an example code segment that calls
    the ABS function twice, to test the function.

20
Exercise 6.3 (Pseudo Code)
  • Function ABS(D1)
  • if (D1 lt 0) D1 0 D1
  • return

21
Exercise 6.3 (M68K Assembly Language)
  • Label Op-Code S1, S2/ Dest., Comments
  • org 1000
  • ABS tst.l D1
  • bge return If (D1 gt 0) branch to return
  • neg.l D1 Negate D1)
  • return rts MemSP --gt PC
  • start move.l -9876, D0
  • bsr ABS
  • move 3, D0 Output result
  • trap 15
  • move.l 9876, D0
  • bsr ABS
  • move 3, D0 Output result
  • trap 15
  • move 9, D0 End of program
  • trap 15

22
Exercise 6.4
  • Write a function PENO (X, N, SP, SN) that will
  • find the sum of the positive even values and the
    negative odd values in an array X of length N.
  • "X" the address of an array, passed through A0.
  • "N" is the length of the array, passed through
    D0.
  • The function should return two values
  • (1) The sum of all the positive even elements in
  • the array, passed back through D1.
  • (2) The sum of all the negative odd elements in
  • the array, passed back through D2.

23
Exercise 6.4 (Pseudo Code)
  • D2 0
  • D3 0
  • for ( D1 gt 0 D1 D1 - 1)
  • D7 Mem(A0)
  • D6 D7 1
  • if (D7 gt 0 D6 0) D2 D2 D7
  • if (D7 lt 0 D6 ! 0) D3 D3 D7
  • return

24
Exercise 6.4 (M68K Assembly Language)
  • Label Op-Code S1, S2/ Dest., Comments
  • PENO
  • clr D2 Initialize summing registers
  • clr D3
  • LOOP
  • move.l (A0), D7 Get a value from the array
  • move.l D7, D6
  • and.l 1, D6 Extract LSB
  • bne ODD Branch if odd
  • tst.l D7 If the value is negative go to NEG
  • ble CHK If the value is negative go to CHK
  • add.l D7, D2 Add value to positive even sum
  • bra CHK Branch to CHK
  • ODD
  • tst D7
  • bge CHK If value is positive go to CHK
  • add.l D7, D3 Add value to negative odd sum
  • CHK
  • sub 1, D1 Decrement loop counter
Write a Comment
User Comments (0)
About PowerShow.com