############################################################## # simulator.s -- skeleton file for a simulated MIPS machine # # Project 2 # ECE15B # YOUR NAME # YOUR PERM NUMBER # ############################################################## .data registers: .space 128 # 32 words for registers static: .space 2048 # 512 words. This stores # the programs you run on your # virtual machine and # also includes stack area str1: .asciiz "The return value: " str2: .asciiz "The number of instructions executed: " str3: .asciiz "R-type: " str4: .asciiz "I-type: " str5: .asciiz "J-type: " retn: .asciiz "\n" .text .globl main main: addi $sp, $sp, -32 sw $ra, 20($sp) sw $fp, 16($sp) addiu $fp, $sp, 28 # this code reads in the input file--a list of integers # (one per line) representing a MIPS assembly language # program, hand-assembled. It stops when it sees a 0, i.e., # sll $0, $0, 0 or NOP) The code is stored at the beginning # of static segment allocated above, one integer per word # (one instruction per word) la $t0, static # $t0 = pointer to beginning of # static space, where your # code will be stored. loop1: li $v0, 5 # code for read_int syscall # $v0 gets integer beq $v0, $0, EO_prog sw $v0, 0($t0) # place instruction in code space addi $t0, $t0, 4 # increment to next code space j loop1 EO_prog: sw $v0, 0($t0) # place the NOP in the code space # as well to signal the end of program la $a0, registers la $a1, static addi $a2, $a1, 2044 # stack pointer points to highest # memory in the static memory area la $a3, static # $a3 can be used as the pointer # to your instructions, so it # is initialized to point to the # first one. jal sim_mips # Call the MIPS simulator move $t0, $v0 la $a0, str1 # "The return value: " li $v0, 4 syscall move $a0, $t0 li $v0, 1 syscall la $a0, retn li $v0, 4 syscall la $a0, str2 # "The number of instructions ... " li $v0, 4 syscall move $a0, $v1 li $v0, 1 syscall la $a0, retn li $v0, 4 syscall lw $ra, 20($sp) lw $fp, 16($sp) addi $sp, $sp, 32 jr $ra # exit out of main sim_mips: # Arguments passed in: # $a0 = pointer to space for your registers (access # 0($a0) up to 124($a0) # $a1 = pointer to lowest address in your static # memory area (2048 bytes available) # $a2 = pointer to the top of the stack (also the # highest memory in the static memory area) # $a3 = pointer to the first instruction in the program # (actually contains same value as $a1, since # code is loaded into lowest memory addresses). # Recall that you do not need to load the # instructions in! The shell takes care of this # for you. # # Register allocation: # You should probably assign certain SPIM registers # to act as your simulated machine's PC, etc. # For clarity's sake, note the assignments here. # # Virtual machine # If you need more storage area for your # *machine*, use assembler directives to # allocate space. ##### ADD YOUR CODE HERE !!!!!!!!!!!! # Here are some guidelines about how you might want to # approach this problem: # # 1. Fetch the instruction from memory. # To facilitate this, you will need to implement a # "virtual" PC. The PC points to the address of the next # instruction. # # 2. Decode the instruction # a. Determine the opcode (and function field ... if # applicable) # b. Read the rs and rt registers. # # 3. Now you know the kind of instruction you're # executing. Use your opcode (and function field) # to branch to a code block that will do what the # opcode (and function field) dictates. # # Reminders: # =============== # 1. Don't forget to increment your virtual PC after each # instruction fetch. # # 2. Remember that jumps change your virtual PC and that # branches *may* change your virtual PC. # # 3. Before you return to the shell, make sure you load # the real $v0 register with the value stored in you # virtual $v0 register. #