Presentation is loading. Please wait.

Presentation is loading. Please wait.

M68K Assembly Language Programming Bob Britton Chapter 6 Efficient Algorithm Development Notice: Slides in this series originally, provided by B.Britton,

Similar presentations


Presentation on theme: "M68K Assembly Language Programming Bob Britton Chapter 6 Efficient Algorithm Development Notice: Slides in this series originally, provided by B.Britton,"— Presentation transcript:

1 M68K Assembly Language Programming Bob Britton 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 011 1 1 110 1 0 110 0 1 000 0 0 x EOR yx OR yx AND yx y

4 Examples of AND Instruction For these examples, assume the following masks are stored in registers D3, and D4: D3 = $ffffffc0 = 111111111111111111111111111111111111111111000000 D4 = $0000003f = 000000000000000000000000000000000000000000111111 To move a copy of D0 into D1 with the lower 6 bits cleared, the instructions to accomplish this would be: move.lD0, D1 and.lD3, 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 = 111111111111111111111111111111111111111111000000 D4 = $0000003f = 000000000000000000000000000000000000000000111111 To move a copy of D0 into D1 with the lower 6 bits set to one, the instructions to accomplish this would be: move.lD0, D1 or.lD4, 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 = 111111111111111111111111111111111111111111000000 D4 = $0000003f = 000000000000000000000000000000000000000000111111 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.lD0, D1 eor.lD4, 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 : Programmer : * Due Date : mm dd, 2005Course: CSCI/EECE 221 * Last Modified : mm dd hh:mm *************************************************************** * * Overall Program Functional Description: * *************************************************************** * Register Usage in Main: * A0 = Address of... * D1 = value of... *************************************************************** * Pseudocode Description: *************************************************************** *

10 start org$1000 leaarray, A0* Initialize address Parameter move.l#4, D1* Length parameter bsrSum* Branch to subroutine move#17, D0* display string without CR & LF leamsg1, A1* load address of msg1 into A1 move.lD2, D1* Sum of positive returned in D2 trap #15* display the string & Value move#17, D0* display string without CR & LF leamsg2, A1* load address of msg2 into A1 move.lD3, D1* Sum of negative returned in D3 trap #15* display the string & Value move.b#9, D0 trap#15* Halt Simulator CREQU$0D LFEQU$0A arrayDC.L-400, 500, 800, -100 msg1DC.B' The sum of the positive values = ', 0 msg2DC.BCR, 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: *leaarray, A0 *move#4, D1 *bsr Sum *************************************************************** * Register Usage in Function: *A0 = address pointer into the array * D0 = a value read from the array in memory * D1 = Value N, loop counter. (counts down to zero) * D2 = return sum of positive values *D3 = return sum of negative values ***************************************************************

12 * Algorithmic Description in Pseudocode: *D2 = 0; *D3 = 0; *while( D1 > 0 )do *{D1 = D1 - 1; *D0 = Mem(A0); *A0 = A0 + 4; *If (D0 > 0) then * D2 = D2 + D0; *else * D3 = D3 + D0;} *Return ****************************************************** Sumclr.lD2 clr.lD3* Clear D2 and D3 Loop tst.lD1 bleReturn* If (D1 <= 0) Branch to Return sub.l#1, D1* Decrement loop count move.l(A0), D0* Get a value from the array add.l#4, A0* Increment array pointer to next long-word tst.lD0* Test the value in D0 bleNegative* Branch If value is negative add.lD0, D2* Add to the positive sum braLoop* Branch back for another iteration Negative add.lD0, D3* Add to the negative sum braLoop* Branch back for another iteration Returnrts* Return endstart* This marks the end of the source file

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 > 0; D1= D1- 1) { D0 = D0 + Mem(A0)+; } Mem(A0) = D0;

15 Exercise 6.1 (M68K Assembly Language) LabelOp-Code S1, S2/ Dest., Comments startorg$1000 lea chico, A0* Load address pointer clrD0 * Clear sum move#100, D1* Initialize loop count loop: add(A0)+, D0 * D0 = D0 + Mem[A0]+ bvserror subq#1, D1* Dec. loop count bgtloop * if (D1 > 0) branch to loop move D0, (A0) * Store the result errormove#9, D0 * end on completion or error trap#15* End of program chicoDS.W101 endstart

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 > 0; D0 =D0 -1) Mem(A1)+  Mem(A2)+ ;

18 Exercise 6.2 (M68K Assembly Language) LabelOp-Code S1, S2/ Dest., Comments org$1000 start lea SRC, A1* A1 = &SRC leaDEST,A2* A2 = &DEST move#100, D0* D0 = 100 loop move(A1)+, (A2)+ subq#1, D0* D0 = D0 - 1 bgtloop* if (D0 > 0) Branch to loop move#9, D0 trap#15 SRCDS.W100 * SRCDC.W$0a,loop,16,' We can fill memory with data',0 DESTDS.W100 endstart

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 < 0) D1 = 0 – D1; return;

21 Exercise 6.3 (M68K Assembly Language) LabelOp-Code S1, S2/ Dest., Comments org$1000 ABS:tst.lD1 bgereturn* If (D1 >= 0) branch to return neg.lD1* Negate D1) return:rts* Mem[SP]+ --> PC ######################################## startmove.l#-9876, D0 bsrABS move#3, D0* Output result trap#15 move.l#9876, D0 bsrABS 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 > 0; D1 = D1 - 1) {D7 = Mem(A0)+; D6 = D7 & 1; if (D7 >= 0 & D6 = 0) D2 = D2 + D7; if (D7 < 0 & D6 != 0) D3= D3 + D7; } return;

24 Exercise 6.4 (M68K Assembly Language) LabelOp-Code S1, S2/ Dest., Comments PENO clrD2* Initialize summing registers clrD3 LOOP move.l(A0)+, D7* Get a value from the array move.lD7, D6 and.l#1, D6* Extract LSB bneODD* Branch if odd tst.lD7* If the value is negative go to NEG bleCHK* If the value is negative go to CHK add.lD7, D2* Add value to positive even sum braCHK* Branch to CHK ODD tstD7 bgeCHK* If value is positive go to CHK add.lD7, D3* Add value to negative odd sum CHK sub#1, D1* Decrement loop counter bgtLOOP* If loop counter > 0 go to Loop rts* Return


Download ppt "M68K Assembly Language Programming Bob Britton Chapter 6 Efficient Algorithm Development Notice: Slides in this series originally, provided by B.Britton,"

Similar presentations


Ads by Google