Question: 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
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
| 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 |
|---|---|
| $s0 | 4000 |
| $s1 | 10 |
| $s2 | -15 |
| Addresses | Contents |
|---|---|
| 4000 | 4 |
| 4004 | 8 |
| 4008 | -15 |
| 4012 | 5 |
| 4016 | -10 |
| 4020 | -15 |
| 4024 | 0 |
| 4028 | -15 |
| 4032 | 40 |
| 4036 | -15 |
The resultant registers are:
| Registers | Data |
|---|---|
| $s0 | 4000 |
| $s1 | 6 |
| $s2 | -15 |
The resultant array is:
| Addresses | Contents |
|---|---|
| 4000 | 4 |
| 4004 | 8 |
| 4008 | 5 |
| 4012 | -10 |
| 4016 | 0 |
| 4020 | 40 |
THIS WAS MY CODE AND IT DIDNT WORK!!!!!!!!!:
int A[100]; int n; int val; #int i = 0; #while (i
li $s0, 4000 li $s1, 0 li $s2, -15 beq $s1, n, end sll $t0, $s1, 2 add $t0, $t0, $s0 lw $t1, 0($t0) beq $t1, $s2, remove addi $s1, $s1, 1 j loop
sll $t2, $s1, 2 add $t2, $t2, $s0 sll $t3, $s1, 2 addi $t3, $t3, 4 add $t3, $t3, $s0
beq $s1, n, end2 lw $t4, 0($t3) sw $t4, 0($t2) addi $s1, $s1, 1 sll $t2, $s1, 2 add $t2, $t2, $s0 sll $t3, $s1, 2 addi $t3, $t3, 4 add $t3, $t3, $s0 j loop2
li $s1, n subi $s1, $s1, 1

Line 1: Unrecognized syntax near: [100]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
