3D1-Microprocessor Systems 1 Lecture 16: Subroutines and Stack – Examples and Review Questions In this lecture we will work on two programs using the stack to pass parameters between a subroutine and its caller. Your attention is drawn to the precautions you should take when using the stack and the system stack pointer for passing parameters. You will also work on writing ‘well behaved’ subroutines. Learning Outcomes: On completion of this lecture, you will be able Use the stack to pass parameters to and from a subroutine; Take adequate precautions when using the stack, Write a “well behaved” subroutine is. 16.1 Review Questions What is special about A7, What is it used for and what special precaution should you take when using it? Describe how the 68000 uses the stack in subroutines. What advantages and drawbacks are there to the 68000’s way of calling and returning from subroutines? Explain what a “well behaved” subroutine is. Explain what happens when a subroutine is called, i.e. how the JSR/BSR and RTS instructions operate. What is a subroutine parameter and how would you pass parameters to and from a subroutine? When you are writing a subroutine, what precaution should you take to ensure that the subroutine does not damage the caller’s environment? 16-1 3D1-Microprocessor Systems 1 16.2 Example 1: Write a complete program to count all the vowels in a NUL terminated character string using a well behaved subroutine. Define a suitable character string and also a longword result space. RSLT ORG DS.L ORG vowels DC.B char DC.B ORG LEA $1FFC 1 $2000 'aeiouAEIOU' 'Nul Terminated Character String',$0 $4000 char,A1 Load the character’s Effective Address into A1 MOVE.L #0,RSLT Clear accummulator loop1 MOVE.B pointer BEQ LEA A0 MOVE.L MOVE.L BSR BRA (A1)+,D1 make local copy of char and incr. char exit vowels,A0 exit if is the NUL char has been moved Load the vowels’ Effective Address into #DoStuff,-(A7) #9,-(A7) Loop2 loop1 Push address of loop subroutine to stack Push counter initial value to stack call the search subroutine repeat until end of string exit #0 return control to Monitor (A0)+,D1 test if the current char a vowel and return if not vowel, return to calling point TRAP DoStuff CMP.B incr. pointer BNE incr MOVE.L RSLT,D0 ADDQ.L #1,D0 MOVE.L D0,RSLT return RTS Loop2 loop * *increment vowel counter * return to calling point MOVEM.L D0-D7/A0-A6,-(A7) save register on the stack LEA (A7),A1 MOVE.L 64(A1),D2 MOVE.L 68(A1),A2 use A1 as local stack pointer get counter initial value from the stack get address of loop subroutine JSR DBRA call loop subroutine repeat until counter down to -1 (A2) D2,loop MOVEM.L (A7)+,D0-D7/A0-A6 restore register RTS return to calling point END 16-2 3D1-Microprocessor Systems 1 16.3 Example 2: Write a complete, well-behaved subroutine to translate a sequence of ASCII characters, meant to represent an unsigned decimal number, to an unsigned binary longword. op1 num main ORG DS.W ORG DC.B $2000 1 $2100 '651 ',$0 ORG LEA PEA BSR TRAP $4000 num,A0 op1 load string’s address into A0 Push the reference of op1 o stack GetNum Call subroutine GetNum #0 return control of CPU to Monitor ORG $3000 D0-D2/A0-A2,-(A7) save working registers on stack GetNum MOVEM.L loop exit Reserve 1 longword for result Define input value CLR LEA MOVE.B CMP.B BMI CMP.B BPL SUB.B MOVE.L LSL.L LSL.L ADD.L ADD.L BRA LEA MOVEA.L MOVE.L D1 (A7),A1 (A0)+,D0 #$30,D0 exit #$3A,D0 exit #$30,D0 D1,D2 #3,D1 #1,D2 D2,D1 D0,D1 loop 28(A1),A2 (A2),A2 D1,(A2) clear accumulator D1 MOVEM.L RTS (A7)+,D0-D2/A0-A2 Restore working registers return to caller copy addr top of stack in A1 (local stack pointer) copy ascii char in D0 * * *if char not a digit then exit * else subtract $30 from ascii code save D1 in D2 multiply contents of D1 by 8 multiply contents of D2 by 2 [D1]+[D2] (product of by 10 mult) add [D0] and [D1] goto loop Get pointer to address of op1 Get actual address of op1 Modify op1 in the calling routine END 16.4 Conclusion You now know how to use the stack to pass parameters to and from subroutine. You should also be aware of the precautions to take when writing ‘well behaved’ and ‘ecological’ subroutines. REFERENCES Dr. Mike Brady, Microprocessor Systems 1, dept of Computer Science, Trinity College Dublin: http://www.tcd.ie/Engineering/Courses/BAI/JS_Subjects/3D1/ 16-3 3D1-Microprocessor Systems 1 Look on the Web. Look at http://www.mee.tcd.ie/~assambc/3D1 16-4