M68K Assembly Language Programming Bob Britton Chapter 6 Efficient Algorithm Development Notice:...
-
date post
30-Jan-2016 -
Category
Documents
-
view
221 -
download
0
Transcript of M68K Assembly Language Programming Bob Britton Chapter 6 Efficient Algorithm Development Notice:...
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.
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):
Logical Operators
011 1 1
110 1 0
110 0 1
000 0 0
x EOR yx OR yx AND y x y
Examples of AND InstructionFor 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.l D0, D1
and.l D3, D1 * D3 & D1 D1
Example of OR instructionFor 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.l D0, D1
or.l D4, D1 * D4 | D1 D1
Example of EOR instructionFor 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.l D0, D1
eor.l D4, D1 * D4 ^ D1 D1
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)
Functional Descriptions of Code ModulesA 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.
Headers
• ****************** Example Main Program Header ************• * Program # 3 : <descriptive name> Programmer : < your name>• * Due Date : mm dd, 2005 Course: 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:• ***************************************************************
*
start org $1000
lea array, A0 * Initialize address Parameter
move.l#4, D1 * Length parameter
bsr Sum * Branch to subroutinemove #17, D0 * display string without CR & LF
lea msg1, 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
lea msg2, 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
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
******************** 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**************************************************************** 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***************************************************************
* 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
******************************************************
Sum clr.l D2
clr.l D3 * Clear D2 and D3
Loop
tst.l D1
ble Return * 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.l D0 * Test the value in D0
ble Negative * Branch If value is negative
add.l D0, D2 * Add to the positive sum
bra Loop * Branch back for another iteration
Negative
add.l D0, D3 * Add to the negative sum
bra Loop * Branch back for another iteration
Return rts * Return
end start * This marks the end of the source file
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.
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;
Exercise 6.1 (M68K Assembly Language)
Label Op-Code S1, S2/ Dest., Comments
start org $1000lea chico, A0 * Load address pointerclr D0 * Clear summove #100, D1 * Initialize loop count
loop:
add (A0)+, D0 * D0 = D0 + Mem[A0]+bvs errorsubq #1, D1 * Dec. loop countbgt loop * if (D1 > 0) branch to loopmove D0, (A0) * Store the result
errormove #9, D0 * end on completion or error trap #15 * End of programchico DS.W 101
end start
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”.
Exercise 6.2 (Pseudo Code)
A1= &SRC; # “&” means “Address of”A2= &DEST; for (D0 = 100; D0 > 0; D0 =D0 -1)
Mem(A1)+ Mem(A2)+ ;
Exercise 6.2 (M68K Assembly Language)Label Op-Code S1, S2/ Dest., Comments
org $1000start
lea SRC, A1 * A1 = &SRClea DEST,A2 * A2 = &DESTmove #100, D0 * D0 = 100
loopmove (A1)+, (A2)+subq#1, D0 * D0 = D0 - 1bgt loop * if (D0 > 0) Branch to loopmove #9, D0trap #15
SRC DS.W 100* SRC DC.W $0a,loop,16,' We can fill memory with data',0DEST DS.W 100
end start
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.
Exercise 6.3 (Pseudo Code)
Function ABS(D1);if (D1 < 0) D1 = 0 – D1;return;
Exercise 6.3 (M68K Assembly Language)Label Op-Code S1, S2/ Dest., Comments
org $1000ABS: tst.l D1
bge return * If (D1 >= 0) branch to returnneg.lD1 * Negate D1)
return: rts * Mem[SP]+ --> PC########################################start move.l #-9876, D0
bsr ABSmove #3, D0 * Output resulttrap #15move.l #9876, D0bsr ABSmove #3, D0 * Output resulttrap #15move #9, D0 * End of programtrap #15
Exercise 6.4Write 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.
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;
Exercise 6.4 (M68K Assembly Language)Label Op-Code S1, S2/ Dest., Comments PENO
clr D2 * Initialize summing registersclr D3
LOOPmove.l (A0)+, D7 * Get a value from the arraymove.l D7, D6and.l #1, D6 * Extract LSBbne ODD * Branch if oddtst.l D7 * If the value is negative go to NEGble CHK * If the value is negative go to CHKadd.l D7, D2 * Add value to positive even sumbra CHK * Branch to CHK
ODDtst D7bge CHK * If value is positive go to CHKadd.l D7, D3 * Add value to negative odd sum
CHKsub #1, D1 * Decrement loop counterbgt LOOP * If loop counter > 0 go to Looprts * Return