Answer:
#Data section
.data
#Set align
.align 2
#Declare row 1
M1: .word 1, 2, 3
#Declare row 2 elements
M2: .word 4, 5, 6
#Declare row 3 elements
M3: .word 7, 8, 9
#Declare row 4 elements
M4: .word 10, 11, 12
#Declare row 5 elements
M5: .word 13, 14, 15
#Create 5x3 array
M: .word M1, M2, M3, M4, M5
#Set number of rows
nRows: .word 5
#Set number of columns
nCols: .word 3
#Declare string menu
menu: .asciiz "\\ The following are the choices:\\"
#Declare string for option 1
op1: .asciiz "1. Replace a value:\\"
#Define string for option 2
op2: .asciiz "2. Calculate the sum of all values\\"
#Define string for option 3
op3: .asciiz "3. Print out the 2D array \\"
#Define string for option 4
op4: .asciiz "4. Exit\\"
#Define string for prompt
urOpt: .asciiz "Enter your option:"
#Define string for row
prompt1: .asciiz "Enter row (1-5):"
#Define string for column input
prompt2: .asciiz "Enter column (1-3):"
#Define string to get the replace value
getVal: .asciiz "Enter the new value:"
#Define strinct to display sum
sumStr: .asciiz "\\The sum is :"
#Define string
dispStr: .asciiz "The 2D array is:\\"
#Define string to print new line
newLine: .asciiz "\\"
#Define string to put comma
comma: .asciiz ","
#text section
.text
#Main
main:
#Block prints the 2D array
#label
print2DArray:
#Load integer to print string in $v0
li $v0, 4
#Load the address of string to display
la $a0, dispStr
#Display the string
syscall
#Load the base address of the row 1
la $s0, M1
#Load the nRows
lw $t0, nRows
#Initialize the counter for outer loop
li $t7, 0
#Outer loop begins
outLoop1:
#Check condition
beq $t7, $t0, displayMenu
#Load the integer to print string
li $v0, 4
#Load the base address of newLine
la $a0, newLine
#Display newLine
syscall
#Initialize counter for inner loop
li $t2, 0
#Set the limit
lw $t3, nCols
#Decrement value
addi $t3, $t3, -1
#inner loop starts
inLoop1:
#Check condition
beq $t2, $t3, exitInLoop1
#Load the integer to print number
li $v0, 1
#Load the value
lw $a0, ($s0)
#Display the integer
syscall
#Load the integer to print string
li $v0, 4
#Load the base address of the string comma to display
la $a0, comma
#Display comma
syscall
#Increment inner loop counter value
addi $t2, $t2, 1
#Move to next element
addi $s0, $s0, 4
#jump to inner loop
j inLoop1
#Exit from inner loop
exitInLoop1:
#Load integer to print last column value
li $v0, 1
#Load the load column value
lw $a0, ($s0)
#Print value
syscall
#Move to next element
addi $s0, $s0, 4
#Increment outer loop counter
addi $t7, $t7, 1
#Jump to start of the outer loop
j outLoop1
#Prints the menus to the user
displayMenu:
#Load integer value to print string
li $v0, 4
#Load the address of the string "menu"
la $a0, menu
#Print the string
syscall
#Load the integer value to print string
li $v0, 4
#Load the address
la $a0, op1
#Print the string
syscall
#Load the integer value to print string
li $v0, 4
#Load the address
la $a0, op2
#Print string
syscall
#Load integer value to print string
li $v0, 4
#Load address of op3
la $a0, op3
#Print string
syscall
#Load integer value to print string
li $v0, 4
#Load the address
la $a0, op4
#Print string
syscall
#Load integer value to print sting
li $v0, 4
#Load address
la $a0, urOpt
#Print string
syscall
#Load the integer to read int value
li $v0, 5
#Read value
syscall
#Move value
move $t0, $v0
#Initialize values
li $t1, 1
li $t2, 2
li $t3, 3
li $t4, 4
#Check user wishes option 1
beq $t0, $t1, replaceBlock
#Check user wishes option 2
beq $t0, $t2, sumBlock
#Check user wishes option 3
beq $t0, $t3, print2DArray
#Check user wishes to exit
beq $t0, $t4, EndProgram
#Jump to start of the menu
j displayMenu
#Block to replace a value in the 2d array
replaceBlock:
#Load integer
li $v0, 4
#load address
la $a0, prompt1
#Print string
syscall
#Read row value
li $v0, 5
syscall
#Store the row value in $t6
move $t6, $v0
#Get the index
addi $t6, $t6, -1
#Load integer
li $v0, 4
#Load address
la $a0, prompt2
#Print string
syscall
#Read column value
li $v0, 5
syscall
#Store the column value in $t7
move $t7, $v0
#Get the index for the column
addi $t7, $t7, -1
#Load integer
li $v0, 4
#Load address
la $a0, getVal
#Print string
syscall
#Read the new value
li $v0, 5
syscall
#Store the new value in $t5
move $t5, $v0
#Load the base address of M
la $s0, M
#Get M[i]
#two times left shift
sll $t1, $t6, 2
#address of pointer M[i]
add $t1, $t1, $s0
#Get address of M[i]
lw $t3, ($t1)
#Get M[i][j]
#two times left shift
sll $t4, $t7, 2
#Get address of M[i][j]
add $t4, $t3, $t4
Step-by-step explanation:
See continuation of the code attached
also, See output attached