Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm still having issues with this mips program and it keeps looping and crashing my program. I will include the code as well as the

I'm still having issues with this mips program and it keeps looping and crashing my program. I will include the code as well as the required tasks down below.

# Author: # Date: # Description: Parse MIPS instructions based on opcode and print message # Do not remove this line # Data for the program goes here .data process: .asciiz " Now processing instruction " opcode_num: .asciiz "\tThe opcode value is: " newLine: .asciiz " " # number of test cases CASES: .word 5 # array of pointers (addresses) to the instructions instructions: .word 0x01095020, 0x1220002C, 0x3C011001, 0x24020004, 0x08100002 inst_0: .asciiz "\tR-Type Instruction " inst_other: .asciiz "\tUnsupported Instruction " inst_2_3: .asciiz "\tUnconditional Jump " inst_4_5: .asciiz "\tConditional Jump " # You may use this array of labels to print the different instructions type messages inst_types: .word inst_0, inst_other, inst_2_3, inst_2_3, inst_4_5, inst_4_5 # Code goes here .text main: la $a0, process lw $t1, CASES # load the number of test cases la $t2, instructions # load the base address of the array li $t3, 0 # loop counter loop_array: # Task 1: Loop over the array of instructions # Set registers and call: print_hex_info lw $a1, 0($t2) # Load the instruction value jal print_hex_info # Print the instruction value jal decode_instruction jal print_opcode_type # Update loop counter addi $t3, $t3, 1 add $t2, $t2, 4 bnez $t3, loop_array # Branch if the instruction is not 0 end_main: li $v0, 10 # 10 is the exit program syscall syscall # execute call ############################################################### ## Other procedures here ############################################################### # Print Message based on opcode type # # $a0 - Message to print # $a1 - Value to print # Uses $s0: address of string for $a0 (required) # Uses $s1: value from $a1 (required)

print_opcode_type: # Save registers to the stack subi $sp, $sp, 16 sw $ra, 12($sp) sw $s0, 8($sp) sw $s1, 4($sp)

# Procedure begins here move $s0, $v0 # Save opcode value to $s0

# Print instruction type message li $v0, 4 # syscall to print string la $a0, inst_types syscall

# Compare opcode and print corresponding message li $v0, 4 # syscall to print string beq $s0, 0, print_r_type beq $s0, 2, print_unconditional_branch beq $s0, 3, print_unconditional_branch beq $s0, 4, print_conditional_branch beq $s0, 5, print_conditional_branch j print_unsupported print_r_type: la $a0, inst_0 syscall j end_print_opcode_type print_unconditional_branch: la $a0, inst_2_3 syscall j end_print_opcode_type

print_conditional_branch: la $a0, inst_4_5 syscall j end_print_opcode_type

print_unsupported: la $a0, inst_other syscall

end_print_opcode_type: li $v0, 4 # syscall to print newline la $a0, newLine syscall

# Restore registers from the stack lw $ra, 12($sp) lw $s0, 8($sp) lw $s1, 4($sp) addi $sp, $sp, 16

jr $ra decode_instruction: # Saving registers to stack (follow the template for storing registers) subi $sp, $sp, 16 sw $ra, 12($sp) sw $s0, 8($sp) sw $s1, 4($sp) move $s0, $a0 # Save input parameter $a0 into $s0 srl $s1, $s0, 26 # Shift right to isolate the opcode (bits 31-26) andi $s1, $s1, 0x3f # Mask other bits to get the opcode value # Set return register $v0 with the opcode value move $v0, $s1

# Restoring registers from stack lw $ra, 12($sp) lw $s0, 8($sp) lw $s1, 4($sp) addi $sp, $sp, 16

jr $ra # Return to the calling function print_hex_info: ##### Begin Save registers to Stack subi $sp, $sp, 32 sw $ra, 28($sp) sw $s0, 24($sp) sw $s1, 20($sp) sw $s2, 16($sp) sw $s3, 12($sp) sw $s4, 8($sp) sw $s5, 4($sp) sw $s6, 0($sp)

move $s0, $a0 move $s1, $a1 li $v0, 4 # print message move $a0, $s0 syscall li $v0, 34 # print address in hex value move $a0, $s1 syscall li $v0, 4 # print message la $a0, newLine syscall

##### Begin Restore registers from Stack lw $ra, 28($sp) lw $s0, 24($sp) lw $s1, 20($sp) lw $s2, 16($sp) lw $s3, 12($sp) lw $s4, 8($sp) lw $s5, 4($sp) lw $s6, 0($sp) addi $sp, $sp, 32 ##### End Restore registers from Stack jr $ra Task 1: Loop over array of values

Your first task is to loop over the array of words representing MIPS Instructions. As you loop over the items, display their value using a message.

Begin working in the file template provided in the folder. To make your output messages more readable, use the print_hex_info provided in the starting code. This procedure takes two input parameters, the first is the string to print, and the second the value.

Starting code:

image text in transcribed

The given information is about functions given in the template:

############################################################### # Fetch instruction to correct procedure based on opcode for # instruction parsing. # # $a0 - input, 32-bit instruction to process # $v0 - output, instruction opcode (bits 31-26) value (required) # Uses $s0: for input parameter (required) # Uses $s1: for opcode value (required) decode_instruction: # Save registers to Stack # ... # Now your function begins here # ... # Task 3: Set/Values call procedure # Set return value

end_decode_instruction: # Restore registers from Stack # ... jr $ra

############################################################### # Process instruction: print instruction type # # $a0 - input, 32-bit instruction to process # Uses $s0: for input parameter (required) process_instruction: # Save registers to Stack # ...

# Now your function begins here # ... end_process_instruction: # Restore registers from Stack # ... jr $ra

############################################################### # Print Message based on opcode type # # $a0 - Message to print # $a1 - Value to print # Uses $s0: address of string for $a0 (required) # Uses $s1: value from $a1 (required) print_hex_info: # Save registers to Stack subu $sp, $sp, 32 # frame size = 32, just because... sw $ra, 28($sp) # preserve the Return Address. sw $fp, 24($sp) # preserve the Frame Pointer. sw $s0, 20($sp) # preserve $s0. sw $s1, 16($sp) # preserve $s1. #sw $s2, 12($sp) # preserve $s2. addu $fp, $sp, 32 # move Frame Pointer to base of frame.

# Now your function begins here move $s0, $a0 move $s1, $a1 li $v0, 4 # print message move $a0, $s0 syscall li $v0, 34 # print address in hex value move $a0, $s1 syscall li $v0, 4 # print message la $a0, newLine syscall

end_print_hex_info: # Restore registers from Stack lw $ra, 28($sp) # restore the Return Address. lw $fp, 24($sp) # restore the Frame Pointer. lw $s0, 20($sp) # restore $s0. lw $s1, 16($sp) # restore $s1. #lw $s2, 12($sp) # restore $s2. addu $sp, $sp, 32 # restore the Stack Pointer. jr $ra

image text in transcribed

Task 2: Parse MIPS Instruction

Your next task is to create a procedure called decode_instruction. Make sure you follow the template from Procedure Template.

This procedure will take the MIPS Instruction from Task 1 as input argument. Your procedure should parse the opcode from the input parameter and return it to the caller for display. See pseudocode sample

The procedure should have the following signature:

image text in transcribed

Note that this procedure will eventually call other procedures. For this reason, make sure you allocate some space in the stack and save all the required registers. You may follow the Procedure Template.

Task 2: Procedure Algorithm Logic

The instruction's opcode comes in the 6 most significant bits of the address (31-26).

Save input parameters $a0 and $a1 into $s0, and $s1.

Isolate opcode (bits 31-26) 1111 1100 0000 0000 0000 0000 0000 0000

Right shift 26 positions the opcode.

Set return register $v0 with value.

Test it in main

image text in transcribedimage text in transcribed

Task 3: Process MIPS Instruction

Your next task is to create another procedure called print_opcode_type. This procedure will take the MIPS Instruction opcode from Task 2 as input parameter. Your procedure should display the corresponding instruction type string based on the input.

For this assignment, you are only required to support branches and R-type instructions. Any other value for the opcode should display Unsupported Instruction. Your code should support the following choices:

image text in transcribed

Task 3: Procedure Algorithm Logic

The instruction's opcode comes in the 6 most significant bits of the address (31-26).

Save input parameters $a0 to $s0.

Based on the opcode value print the corresponding string message.

Note: You may use a "switch" statement to process all choices.

image text in transcribed

Test Process MIPS Instruction

To test this procedure, go back to the decode_procedure. In there and before you return the value, call the process_instruction with the opcode as input parameter.

image text in transcribed

Procedure Template

Use the following template for any procedure you write from now on. This template will store the $ra and $s registers in the stack so you can call nested procedures.

image text in transcribed \# Data for the program goes here . data process: . asciiz " Now processing instruction " newLine: . asciiz " " \# number of test cases CASES: , word 5 \# array of pointers (addresses) to the instructions instructions: . word 0x01095020, \# add \$t2, \$t0, \$t1 .word 0x1220002C, \# beqz \$s1, label .word 0x3c011001, \# lw \$a0, label(\$s0) .word 0x24020004, \# li \$v0, 4 .word 0x08100002 \# j label \# Code goes here . text main: \# Task 1: Loop over the array of instructions loop_array: \# Set registers and call: print_hex_info j loop_array \# end of loop end_main: li $v0,10 \# 10 is the exit program syscall syscall \# execute call Sample Output Task 1 Now processing instruction 0x01095020 Now processing instruction 0x3c011001 Now processing instruction 0x 24020004 Now processing instruction 0x08100002 -- program is finished running -- \# Fetch instruction to correct procedure based on opcode for \# instruction parsing. \# Argument parameters: \# \$a0 - input, 32-bit instruction to process (required) \# Return Value: \# \$v0 - output, instruction opcode (bits 31-26) value (required) \# Uses: \# \$s0: for input parameter (required) \# \$s1: for opcode value (required) decode_instruction: \# Save registers to Stack \# ... \# Your function "real" code begins here \# ... \# Isolate opcode (bits 31-26) 11111100000000000000000000000000 \# Task 3 (later): Set/Values call process_instruction_procedure \# Set return value end_decode_instruction: \# Restore registers from Stack \# ... jr \$ra \# Data for the program goes here . data \# ... opcode_num: .asciiz "\tThe opcode value is: " \# Code goes here .text main: \# ... loop_array: \# Set registers and call: print_hex_info for process string \# Task 2: \# Set registers and call: decode_instruction \# Set registers and call: print_hex_info for opcode_num string #... j loop_array \# end of loop end_main: li $ v0, 10 \# exit program syscall syscall Sample Output Task 2 Now processing instruction 0x01095020 R-Type Instruction Now processing instruction 0x1220002c Conditional Jump Now processing instruction 0x3c011001 Unsupported Instruction Now processing instruction 0x24020004 Unsupported Instruction Now processing instruction 0x08100002 Unconditional Jump -- program is finished running -- \begin{tabular}{|l|l|} \hline Opcode & \multicolumn{1}{|c|}{ Instruction Type } \\ \hline 0 & R-Type Instruction \\ \hline 2 or 3 & Unconditional Branch \\ \hline 4 or 5 & Conditional Branch \\ \hline other & Unsupported Instruction \\ \hline \end{tabular} \# Data for the program goes here . data \# ... inst_0: .asciiz "\tR-Type Instruction " inst_other: .asciiz "\tUnsupported Instruction " inst_2_3: .asciiz "\tUnconditional Jump " inst_4_5: .asciiz "\tConditional Jump " \# ... \# Print Opcode Type \# Argument parameters: \# \$a0 - input, 32-bit instruction to process \# Uses: print_opcode_type: \# Save registers to Stack \# ... \# Your function code begins here end_print_opcode_type: \# Restore registers from Stack \# ... jr \$ra \# Inside the decode_instruction procedure \# \# ... \# Isolate opcode (bits 31-26) 11111100000000000000000000000000 \# Task 3: Set/Values CALL process_instruction_procedure \# Set return value Sample Output Task 3 Now processing instruction 0x01095020 R-Type Instruction The opcode value is: 000000000 Now processing instruction 0x1220002c Conditional Jump The opcode value is: 000000004 Now processing instruction 0x3c011001 Unsupported Instruction The opcode value is: 00000000f Now processing instruction 0x24020004 Unsupported Instruction The opcode value is: 000000009 Now processing instruction 0x08100002 Unconditional Jump The opcode value is: 000000002 -- program is finished running -- \# Desc: PROCEDURE DESCRIPTION \# Argument parameters: \# \$a0 - IF APPLICABLE \# \$a1 - IF APPLICABLE \# \$a... - IF APPLICABLE \# Return Value: \# \$v0 - IF APPLICABLE \# Uses: \# \$s0: IF APPLICABLE (required) \# \$S....: IF APPLICABLE (required) procedure name: \#\#\#\# Begin Save registers to Stack subi \$sp, \$sp, 36 sw \$ra, 32(\$sp) sw $s0,28($sp) SW \$s1, 24(\$sp) sw $s2,20($sp) sw \$s3, 16(\$sp) sw \$s4, 12(\$sp) sw $s5,8($sp) sw $s6,4($sp) sw $s7,0($sp) \#\#\#\# End Save registers to Stack \# Now your function begins here \# move $s,$a0 \# If you are taking params #. end_procedure_name: \#\#\#\# Begin Restore registers from Stack lw \$ra, 32(\$sp) lw $s0,28($sp) lw \$s1, 24(\$sp) lw $s2,20($sp) lw \$s3, 16(\$sp) lw \$s4, 12(\$sp) lw $s5,8($sp) lw $s6,4($sp) lw $s7,0($sp) addi $sp,$sp,36 \#\#\#\# End Restore registers from Stack jr \$ra

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

6th Edition

1943153035, 978-1943153039

More Books

Students also viewed these Databases questions

Question

Define positive thinking and cite its benefits.

Answered: 1 week ago