3D1-Microprocessor Systems 1 Lecture 5: Introduction to Assembly Language Programming - Examples In this lecture, we will write programs to return the average of an array of word integers. Learning Outcomes: On completion of this lecture, you will be able to: Write simple sequential statements in 68k assembly language; Implement conditional and loop statements. 5.1 Write an assembly language program to find the (integer) average value of ten unsigned word integers. The numbers are stored in an array starting at $2000.The result should be placed in $1FFE. You'll need the unsigned divide instruction - DIVU. The destination must be a data register. The source can be anything sensible. This instruction will divide the longword destination by the word source, replacing the longword destination with two words. The least significant word is the integer result, and the most significant word is the integer remainder. *label RSLT A B C D E F G H I J instructions operands comments ORG DS.W $1FFE 1 result location reserve 1 longword in memory ORG MOVEQ.L ADD.W ADD.L ADD.L ADD.L ADD.L ADD.L ADD.L ADD.L ADD.L ADD.L $2000 #0, D0 A, D0 B, D0 C, D0 D, D0 E, D0 F, D0 G, D0 H, D0 I, D0 J, D0 origin of program initialise D0 to zero * * * * * * accumulate the sum into D0 * * * * DIVU #10, DO get the average MOVE.W D0,RSLT save result TRAP #0 return control of the CPU to the Monitor (OS) ORG DC.W DC.W DC.W DC.W DC.W DC.W DC.W DC.W DC.W DC.W $1100 1 2 3 4 5 6 7 8 9 10 origin of data * * * * * example of data entry * * * * * END 5-1 3D1-Microprocessor Systems 1 5.2 How would you calculate the average of an array of, say, a 1000 unsigned word integers? *label opcode operands comments N START EQU EQU 1000 $2000 set array size the array starts at address $2000 RSLT ORG DS.W $1FFE 1 result location reserves 1 word in memory for the result ORG MOVEQ MOVE.L MOVE.L MOVE.W ADD.L DBRA DIVU MOVE.W $1000 #0,D1 #START,A0 #N-1,D0 (A0)+,D2 D2,D1 D0,SUM #N,D1 D1,RSLT returns the integer average of an array of N integers clear accumulator A0 points at the start of the array init. number counter: loop to be run from 999 to 0 make local copy of a number & point at the next one accumulate the longworld sum to avoid overflow and errors repeat the loop as long [D0] is positive div D1 by N store integer part in memory TRAP #0 returns control of the CPU to the Monitor (OS) SUM END 5.3 Conclusion To calculate the sum of a large number of values in a practical way, we need to get the same sequence of instructions executed multiple times -- we need to control the flow of program execution. It’s no use executing exactly the same operations over and over. For example, we have to be able to get a different element (actually the next element) each time. Here we have managed to vary the address an instruction uses while the program is executing. REFERENCES Dr. Mike Brady, Microprocessor Systems 1, dept of Computer Science, Trinity College Dublin: http://www.tcd.ie/Engineering/Courses/BAI/JS_Subjects/3D1/ Look on the Web at http://www.mee.tcd.ie/~assambc/3D1 5-2