next up previous contents

[ENGN3213 Home]

Subroutines

As you know from computer science courses, a subroutine is a coherent sequence of instructions to carry out a well defined function, and can be called many times. Subroutines are used for reasons of efficiency and logical structure.

Let's look at a simple example to see how subroutines work. Start BSVC, load the setup file, and load the file clab61s.s:

00000000                    1  *				clab61s.s
00000000                    2  	ORG	$0 
00000000  00008000          3  	DC.L	$8000     Stack pointer value after a reset
00000004  00002000          4  	DC.L	START     Program counter value after a reset
00000008                    5  
00000008                    6  
00002000                    7  	ORG	$2000     Start at location 2000 Hex
00002000                    8  
00002000  4280              9  START    CLR.L   D0      clear D0
00002002  6100 0006        10           BSR     GET_IT  execute subroutine GET_IT
00002006  1200             11           MOVE.B  D0,D1   do something with it
00002008  4848             12           BREAK
0000200A                   13  
0000200A  1039 00002012    14  GET_IT   MOVE.B  LETTER,D0 beginning of subroutine
00002010  4E75             15  	        RTS               return to caller
00002012                   16  
00002012                   17  
00002012  41               18  LETTER   DC.B    'A'   letter A stored in memory
00002013                   19

This program has a very simple main program which calls a subroutine GET-IT once at line 10. When the processor encounters the BSR instruction, it pushes the return address onto the stack, increments PC by $200A-($2002 + 2), and thereby executes the subroutine code. When the RTS instruction is encountered, it pops the stack and continues executing the main program.

Exercise.

1.
Explain what the subroutine and main program do.

2.
Open the memory viewer and program listing window of the BSVC, and view the memory locations immediately below the initial stack pointer PC=$8000.

3.
By single stepping through the code, fill in the following table as program execution progresses indicating the contents of the stack and registers. Observe closely what happens as each instruction is executed.

  at reset 9 10          
7FFA 00              
7FFB 00              
7FFC 00              
7FFD 00              
7FFE 00              
7FFF 00              
8000 00              
PC 2000              
SP 8000              
D0 00              
D1 00              

4.
What is the value of the return address stored in stack? How many bits is it? How many memory locations does it occupy?


next up previous contents

[ENGN3213 Home]

ANU Engineering - ENGN3213