Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

any help would be appreciated, thank you! Write a MIPS program to remove a given element from an array. Below is an example of the

any help would be appreciated, thank you!
Write a MIPS program to remove a given element from an array.
Below is an example of the C code segment to perform the above task. You may use a different code, but you have to write the code that you use as a comment before the MIPS code.
// Declaration of variables
int* A; // Base address of array A
int n; // Length of the array A
int val; // Given value to be removed from array
// Remove elements equal to val
int i =0;
while (i < n){
if (A[i]!= val)// If no match, proceed with next element
i++;
else {
for (int k = i; k < n -1; k++)// Shift elements if a match is found
A[k]= A[k+1];
n = n -1; // if element deleted, length of array decreases
Registers Variables
$s0 A
$s1 n
$s2 val
Addresses Contents
$s0 A[0]
$s0+4 A[1]
$s0+8 A[2]
......
$s0+4*(n-1) A[n-1]
You may use any temporary registers from $t0 to $t9 or saved registers from $s3 to $s7. Clearly specify your choice of registers and explain your code using comments.
Example Test: If the values of $s0 through $s2 and array elements are initialized in the simulator as:
(Use the '+' button under the Registers display to initialize register values for $s0, $s1, $s2 and the '+' button under the Memory display to initialize the initial array elements.)
Registers Data
$s04000
$s110
$s2-15
Addresses Contents
40004
40048
4008-15
40125
4016-10
4020-15
40240
4028-15
403240
4036-15
The resultant registers are:
Registers Data
$s04000
$s16
$s2-15
The resultant array is:
Addresses Contents
40004
40048
40085
4012-10
40160
402040
Note: Only the registers $s0, $s1, $s2 and the first '$s1' elements in memory will be checked by the automated tests.
This is what i have so far, but it wont compile:
# Change the C code here if you have used something different from the one given.
#int i =0;
#while (i < n){
# if (A[i]!= val)// If no match, proceed with next element
# i++;
# else {
# for (int k = i; k < n -1; k++)// Shift elements if a match is found
# A[k]= A[k+1];
# n = n -1; // if element deleted, length of array decreases
# }
#}
# Enter your MIPS code below.
# Write short comments explaining
# each line of your code
# Explain your use of labels
# MIPS code to remove a given element from an array
# Register usage:
# $s0- base address of array A
# $s1- length of array A (n)
# $s2- value to be removed from array (val)
# $t0- index i
# $t1- inner loop index k
# $t2- temporary value for comparison
# $t3, $t4, $t5- temporary registers for calculations
# Initialize index i to 0
addi $t0, $zero, 0 # i =0
loop:
# Check if i >= n
bge $t0, $s1, end_loop # if i >= n, exit loop
# Load A[i] into $t2
sll $t3, $t0,2 # t3= i *4(calculate offset)
add $t4, $s0, $t3 # t4= base_address + offset (address of A[i])
lw $t2,0($t4) # t2= A[i]
# Compare A[i] with val
bne $t2, $s2, next # if A[i]!= val, skip to next iteration
# If A[i]== val, shift elements to the left
add $t1, $t0,1 # k = i +1
shift_loop:
# Check if k >= n
bge $t1, $s1, update_length # if k >= n, exit shift loop
# Load A[k] into $t2
sll $t3, $t1,2 # t3= k *4(calculate offset)
add $t4, $s0, $t3 # t4= base_address + offset (address of A[k])
lw $t2,0($t4) # t2= A[k]
# Store A[k] at A[k-1]
sub $t5, $t1,1 # t5= k -1
sll $t3, $t5,2 # t3=(k-1)*4(calculate offset)
add $t4, $s0, $t3 # t4= base_address + offset (address of A[k-1])
sw $t2,0($t4) # A[k-1]= A[k]
# Increment k
addi $t1, $t1,1 # k = k +1
j shift_loop # repeat shift loop
update_length:
# Decrease the length of the array
addi $s1, $s1,-1 # n = n -1
# Do not increment i since elements are shifted
j loop # repeat main loop
next:
# Increment i
addi $t0, $t0,1 # i = i +1
j loop # repeat main loop
end_loop:
# End of program, infinite loop to terminate
j end_loop # stay in end loop

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions

Question

confidence intervals (Chapter 4)

Answered: 1 week ago