# ============================================================= # main PROCEDURE TEMPLATE # 4b # # Use with "proc_template4b.asm" as the template for other procedures # # Based on Slide 37 of Lecture 9 (Procedures and Stacks) # (main is simpler than other procedures because it does not have to # clean up anything before exiting) # # Assumptions: # # - main calls other procedures with no more than 4 arguments ($a0-$a3) # - any local variables needed are put into registers (not memory) # - no values are put in temporaries that must be preserved across a call from main # to another procedure # # ============================================================= .data 0x0 # # declare global variables here .text 0x3000 .globl main main: ori $sp, $0, 0x3000 # Initialize stack pointer to the top word below .text # The first value on stack will actually go at 0x2ffc # because $sp is decremented first. addi $fp, $sp, -4 # Set $fp to the start of main's stack frame # ============================================================= # No need to create room for temporaries to be protected. # ============================================================= # ============================================================= # BODY OF main # ... # ... # ... # ... CODE FOR main HERE # ===================================================== # main CALLS proc1 # # Suppose main needs to call proc1, but there are no # temporaries that need to be protected for this call. # # Suppose there are four arguments to send to proc1: # (0, 10, 20, 30). Here's how to do it. ori $a0, $0, 0 # Put 0 in $a0 ori $a1, $0, 10 # Put 10 in $a1 ori $a2, $0, 20 # Put 20 in $a2 ori $a3, $0, 30 # Put 30 in $a3 jal proc1 # call proc1 # valued returned by proc1 will be in $v0-$v1 # ===================================================== # ... MORE CODE FOR main HERE # ... # ... # END OF BODY OF main # ============================================================= exit_from_main: ori $v0, $0, 10 # System call code 10 for exit syscall # Exit the program end_of_main: