24.361 Lab. 4 31 ECE 3610 MICROPROCESSING SYSTEMS Laboratory 4 LAB 4: ASSEMBLER DIRECTIVES, THE STACK, SUBROUTINES, AND BUBBLE SORTING 1 INTRODUCTION This lab deals with the use of the stack and subroutines in an assembly language program. The objective of the lab is to develop a subroutine that performs a bubble sort of a list of signed numbers, sorting the numbers from smallest to largest. The bubblesort subroutine is to be designed as a library routine, so that it can be called by any HCS12 assembly language program. A calling program must be written to test the subroutine. This lab also deals with using assembler directives. 2 PREPARATION Review the lectures on the stack, subroutines, and assembler directives. 3 REQUIREMENTS AND DESIGN CONSTRAINTS 3.1 The bubblesort subroutine shall sort a list of signed numbers, sorting the numbers from smallest to largest. 3.2 The size of the list and the starting address of the list shall be passed to the bubblesort subroutine via the stack. 3.3 The bubblesort subroutine shall clean the stack prior to returning to the calling program. 3.4 The minimum size of the list shall be 2 bytes. 3.5 The maximum size of the list shall be 255 bytes. 3.6 The bubblesort subroutine shall preserve the registers it uses. 3.7 The calling program shall identify the starting address of the stack in the source code using an appropriate label and the appropriate assembler directives. 3.8 The calling program shall allocate space in USER RAM for the list of numbers using the appropriate assembler directives.
24.361 Lab. 4 32 3.9 The calling program shall allocate space for the variables starting address and size using the appropriate assembler directives and appropriate labels. 3.10 The calling program shall reference the starting address and size variables using labels. 3.11 Except for the information passed to the stack, the subroutine shall not assume to have any knowledge about the calling program s memory environment, including the starting address and size variables. 3.12 The subroutine shall not use any absolute memory locations. The subroutine can use the stack for temporary variables it may need. 4 ALGORITHM Figure 1 shows an algorithm to sort a list of numbers. The list is 5 bytes long. Without loss of generality, for simplicity, the numbers are all positive and are shown in decreasing order. The register is used as a pointer into the list, as well as an indication when to stop the inner loop. At the start of each outer loop, is initialized to the Starting Address (SA). Each time through the loop, is incremented. When = Ending Address (), this is when the inner loop is done. Notice that the is decremented each time through the outer loop. This is because when one outer loop has been completed, the largest number has been bubbled down to the bottom of the list. And the number at the bottom of the list does not need to be checked again in the next outer loop. The variable is also used to indicate when the outer loop is done, i.e., when the = SA, then the sorting is complete. Each time through the inner loop, two numbers are compared. If the first number is greater than the second, then they are swapped. The following is a piece of code that does the compare and swap: ldaa $00,x ldab $,x cba ble skip staa $,x stab $00,x skip continue,...
24.361 Lab. 4 33 Inner Loop 1 2 3 4 Outer Loop =SA 1 = 2 =SA = 3 =SA = 4 =SA = = SA =, done = Compare and swap if greater SA = Starting Address = Ending Address Inner loop is executed if not equal to Outer loop is executed if not equal to SA Fig. 1. Suggested algorithm for performing bubble sort.
24.361 Lab. 4 34 5 DELIVERABLES Draw a flow chart that represents the algorithm described in Fig. 1. In the flow chart, also identify which register(s) or memory location(s) your design will use to implement the variables: SA and. Show all detail in the flow chart. Submit the flow chart to the TA for marking. Show your working bubble sort program to your T.A. Submit your commented source code. The source code should specify how the subroutine expects to receive the input parameters, the size of the parameters, and the order in which they are sent. The source code should comment the instructions used to clean the stack. The Section Appendix A: Points to Ponder must be answered in your log book and submitted to the TA for marking by the next day following the lab. APPENDI A: POINTS TO PONDER 1. Does the assembler generate any machine code for the statement: ROMStart EQU $4000 If so, what is the machine code? What is the purpose of the EQU? What is the difference between an assembler directive and an assembly language statement? 2. Does the assembler generate any machine code for the statement: ORG RAMStart If so, what is the machine code? What is the significance of RAMStart? What is the value of RAMStart? 3. What is the maximum size of the stack? In other words, how big does the stack in the bubble- Sort subroutine grow? Is there any danger of the stack over-writing the calling program s data area? 4. Draw a memory map, showing the starting and ending addresses of all objects in USER RAM used by the calling program and the bubblesort subroutine. 5. How does the calling program pass the size of the list and the starting address of the list to the subroutine bubblesort? What other possible methods are there for passing this data to the subroutine? What are the advantages/disadvantages of the alternative methods of passing parameters to a subroutine?
24.361 Lab. 4 35 6. Does the bubblesort subroutine pass a result back to the calling program? What is the result of executing the bubblesort subroutine? 7. What objects in USER RAM are shared by the calling program and the bubblesort subroutine? 8. A design constraint was that the bubblesort subroutine must clean the stack. What is the meaning of the statement clean the stack, and explain how you implemented it. 9. Note that the calling program may also clean the stack by simply re-initializing the stack pointer to the start of the stack. Is it better to have the calling program or the subroutine to clean the stack? Explain your reason(s). 10. A design constraint was that the calling program must use labels to identify the starting address and size of the list. In addition, the bubblesort subroutine will use labels to identify branch targets. The question is: is the assembler able to assemble all of the assembly language statements, in particular, the ones that use labels, in one pass through the source code? If not, then what is the minimum number of passes through the source code that is required so that the assembler can fully assemble all assembly language statements? 11. Complete the following statement: the assembler is a -pass assembler. 12. Describe how the PSHA instruction operates in the HCS12 microprocessor used in the lab. Does this differ in any way compared with the way the PSHA instruction operates and described in the Lecture Notes? Explain any advantages or disadvantages between the two methods.