Implement a MIPS assembly language program that defines main, readArray and sear
ID: 3746556 • Letter: I
Question
Implement a MIPS assembly language program that defines main, readArray and searchElement procedures.
The readArray takes an array of integers as its parameter, reads in integers from a user to fill the array and also print each value as long as it is with the number of elements specified by the parameter "howMany" and "length".
The searchElement procedure takes parameters of an array of integers, and its length, and asks a user how many integers to read in and calls the readArray procedure. It also asks a user to enter an integer to search. Then it should go through the array to see if each number is same as the integer to search. If an element in the array is the same as the integer to search, double its content, by multiplying it by 2, and set the variable found to 1 so that it can print out a message to say whether it was found or not. Then it prints out the updated content of the array.
The main procedure asks a user how many times to call the searchElement procedure, and it repeats it based on the entered number.
Please see the following C program to understand how it should work.
If your program causes an infinite loop, press Control and 'C' keys at the same time to stop it.
Name your source code file assignment6.s.
The following shows how it looks like in a C program:
The following is a sample output (user input is in bold):
Enter an integer to specify how many times to repeat:
2
Specify how many numbers should be stored in the array (at most 9):
8
Enter an integer:
1
Enter an integer:
-12
Enter an integer:
53
Enter an integer:
-4
Enter an integer:
5
Enter an integer:
32
Enter an integer:
1
Enter an integer:
7
The array content:
1
-12
53
-4
5
32
1
7
Enter an integer to search:
1
Result Array Content:
2
-12
53
-4
5
32
2
7
The entered element was found
Specify how many numbers should be stored in the array (at most 9):
7
Enter an integer:
1
Enter an integer:
5
Enter an integer:
1
Enter an integer:
-5
Enter an integer:
-2
Enter an integer:
3
Enter an integer:
2
The array content:
1
5
1
-5
-2
3
2
Enter an integer to search:
8
Result Array Content:
1
5
1
-5
-2
3
2
The entered element was not found
Explanation / Answer
#Data section
.data
#Declare space for 8 integers
array: .space 32
#Declare the array length
length: .word 8
#Declare number of elements
howMany: .word 0
#Declare space to store odd/even integer removal
even: .space 4
#Declare the needed strings
str1: .asciiz "Enter an integer: "
str2: .asciiz "Enter zero to remove even numbers or non-zero to remove odd numbers: "
str3: .asciiz "Specify how many numbers should be stored in the array (at most 8): "
str4: .asciiz "Array Content: "
str5: .asciiz "After removing even integers: "
str6: .asciiz "After removing odd integers: "
#Declare the new line
newLine: .asciiz " "
#Text section
.text
#main
main:
#Print the string
li $v0, 4
la $a0, str2
syscall
#Get the odd/even integer removal choice
la $t1, even
li $v0, 5
syscall
#Store it in even variable
sw $v0, 0($t1)
#Call Function
jal removeEvenOrOdd
#Print the string
li $v0, 4
la $a0, str2
syscall
#Get the odd/even integer removal choice
li $v0, 5
syscall
la $t1, even
#Store it in even variable
sw $v0, 0($t1)
#Call Function
jal removeEvenOrOdd
#Stop the program
li $v0, 10
syscall
#Function that removes either odd/even integer from the array
removeEvenOrOdd:
#Decrease the stack space
addi $sp, $sp, -4
#Store the return address
sw $ra, 0($sp)
#Display str3
li $v0, 4
la $a0, str3
syscall
#Get the number of elements in the array
li $v0, 5
syscall
#Store it in howMany
la $t2, howMany
sw $v0, 0($t2)
#Call Function to read array elements
jal readArray
#Initialize the loop counter
li $t0, 0
#Load the maximum array length
lw $t1, length
#Load the current array length
lw $t2, howMany
#Load value
li $t4, 1
#Load base address of array
la $a1, array
#Load the user choice
lw $s0, even
#Check condition
beq $s0, 0, printEven
#Display string
li $v0, 4
la $a0, str6
syscall
#Go to loop loop2
j loop2
#print the string
printEven:
li $v0, 4
la $a0, str5
syscall
#define loop2
loop2:
#check condition
bge $t0, $t1, printArray2
bge $t0, $t2, printArray2
#Load array element
lw $t3, 0($a1)
#Check whether user wish to remove odd element
bne $s0, $0, checkOdd
#check number is even
andi $s1, $t3, 0x01
#If number is not even
bne $s1, $0, getNextElement
#If number is even, replace array content with 1
sw $t4, 0($a1)
#Go to getNextElement
j getNextElement
#Label
checkOdd:
#check number is odd
andi $s1, $t3, 0x01
#If number is not odd
bne $s1, $t4, getNextElement
#If number is even, replace array content with 0
sw $0, 0($a1)
#Label getNextElement
getNextElement:
#Get next element
addi $a1, $a1, 4
#Increment the counter
addi $t0, $t0,1
#Jump to loop2
j loop2
#Print the array after removal
printArray2:
#Set the loop counter
li $t0, 0
#Load the max length of the array
lw $t1, length
#Laod the current length of the array
lw $t2, howMany
#Load the array
la $a1, array
#Display string
li $v0, 4
la $a0, str4
syscall
#Label
printLoop2:
#Check condition
bge $t0, $t1, exitLoop2
#Check condition
bge $t0, $t2, exitLoop2
#Print the array content
li $v0, 1
lw $a0, 0($a1)
syscall
#Goto new line
li $v0, 4
la $a0,newLine
syscall
#Get the next element
addi $a1, $a1, 4
#Increment the loop counter
addi $t0, $t0,1
#Move to start of the loop
j printLoop2
#Loop to exit the print conditions
exitLoop2:
#Restore the return address
lw $ra, 0($sp)
#Increase the stack
addi $sp, $sp, 4
#Jump to main
jr $ra
#Function removeEvenOdd ends
#Define function readArray();
readArray:
#Initialize the loop counter
li $t0, 0
#Load the max length of the array
lw $t1, length
#Load the current length of the array
lw $t2, howMany
#Load the base address of the array
la $a1, array
#Loop
loop:
#Check condition
bge $t0, $t1, printArray
#Check condition
bge $t0, $t2, printArray
#Print the string
li $v0, 4
la $a0,str1
syscall
#Get the integer value and store it in array
li $v0, 5
syscall
sw $v0, 0($a1)
#increment address
addi $a1, $a1, 4
#increase by 1
addi $t0, $t0,1
#Go to loop
j loop
#label
printArray:
#initialize the counter
li $t0, 0
#Load the base address
la $a1, array
#Print the string
li $v0, 4
la $a0, str4
syscall
#Label
printLoop:
beq $t0, $t1, exitLoop
beq $t0, $t2, exitLoop
#Print the array element
li $v0, 1
lw $a0, 0($a1)
syscall
#Display new line
li $v0, 4
la $a0,newLine
syscall
#Increase the address
addi $a1, $a1, 4
#Increment the counter
addi $t0, $t0,1
#Go to printLoop
j printLoop
#Label
exitLoop:
#Return to removeEvenOdd
jr $ra
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.