c code
while (save[i]==k)
i+=1;
put i in $s3, k in $s5 and address of k in $s6
mips code
loop: sll $t1, $s3, 2
add $t1, $t1, $s6
lw $t0, 0($t1)
bne $t0, $s5, Exit
addi $s3, $s3, 1
j loop
Exit:
Your save array/pointer must be of some 4-byte type (ints?). Therefore to load save[i] from memory, the index i needs to be translated to a byte offset within the array, and then added to the base address of that array. This is done by multiplying i by four:
sll $t1, $s3, 2
and then adding save:
add $t1, $t1, $s6
This doesn't look like an optimized build though. Usually the compiler can re-write this code to advance a temporary pointer in increments of four directly, thus avoiding two of the instructions in that loop.
I am trying to achieve the following via MIPS assembly:
for(int i=0; i<n ; ++i){
arr2[i] = arr1[i];
}
$t2 stores the address of starting point of arr1 and $t3 stores the starting point of arr2
$t1 stores the value n upto which the loop runs.
My assembly code is as follows:
#save the value of $t2 and $t3
move $t8, $t2
move $t9, $t3
#init i=0
move $s7, $zero
copyArrayLoop: beq $s7, $t1, endCopyLoop
sw $t2, 0($t3)
addi $t2, $t2, 4
addi $t3, $t3, 4
addi $s7, $s7, 1
j copyArrayLoop
endCopyLoop: move $t2, $t8
move $t3, $t9
however, this copies the address of values stored at $t2 as it increments, and not the value in memory at address denoted by $t2, can anyone point what's going wrong here?
This is the C code i am using to make the quick sort on the MIPS, on the Mars editor i am having issues when running these code
#include <stdio.h>
void QuickSort(int *array, int first, int last){
int q;
if (first<last){
int value = array[last];
int i = first-1;
int tmp;
int j = first;
while(j<=last){
if (array[j] <= value) {
i++;
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
j++;
}
q = i;
QuickSort(array,first,q-1);
QuickSort(array,q+1,last);
}
}
And this is my MIPS translation so far, i am getting an infinite loop
.data
numArray: 30, 15, 11, 40, 75, 80, 70, 60
first: 0
last: 7
.text
main:
la $a0, numArray
lw $a1, first
lw $a2, last
jal quick_sort
li $v0, 10
syscall
quick_sort:
subi $sp, $sp, 4 #reserving memory in the stack
sw $ra, 4($sp) #storing the return adress in the stack
xor $t0, $t0, $t0 #int q
bge $a1, $a2, label1 #if (first<last)
sll $t1, $a2, 2 #int value = array[last];
add $t1, $t1, $a0 #callculating array[last] in $t1
lw $t1, 0($t1) #array[last] = array direction + 4 * last
or $t2, $t1, $zero #$t2 will be value
subi $t3, $a1, 1 #int i = first-1;
xor $t4, $t4, $t4 #int tmp
or $t5, $a1, $zero #int j=first
j label2 #while(j<=last)
label3: sll $t6, $a2, 2 #calculating array[j] adress
add $t6, $t6, $a0
lw $t7, 0($t6) #calculating array[j] value
bgt $t7, $t1, label4 #if (array[j] <= value)
addi $t3, $t3, 1 #i++
sll $s0, $t3, 2 #calculating array[i] adress
add $s0, $s0, $a0
lw $s1, 0($s0) #calculating array[i] value
or $t4, $s1, $zero #tmp = array[i]
sw $t7, 0($s0) #array[i] = array[j];
sw $t4, 0($t6) #array[j] = tmp;
label4: addi $t5, $t5, 1 #end if; j++
label2: ble $t5, $a2, label3 #while condition
or $t0, $t3, $zero #q = i
lw $a1, first #first value on the second parameter
subi $a2, $t0, 1 #q-1
jal quick_sort #QuickSort(array,first,q-1)
addi $a1, $t0, 1 #q+1
lw $a2, last #last value on the third parameter
jal quick_sort #QuickSort(array,q+1,last);
label1: lw $ra, 4($sp) #Recovering the return address from the stack
addi $sp, $sp, 4 #releasing memory
jr $ra #going to the return address
Maybe i need to store something more on the stack or something that i am missing, thanks for your help, anything that you see strange there please let me know to check it.
EDIT:
As pointed out by Minn in a comment, I missed the two lines following:
sll $t1, $a2, 2 #int value = array[last];
So while this single line does not match the comment in it, the loading of the value seems correct.
END EDIT
I am not familiar with this specific assembly, but the problem seems to be what is known as "register clobbering" :
According to documentation jal only stores the return address, but does not store any of the registers.
lw $a1, first #first value on the second parameter
subi $a2, $t0, 1 #q-1
jal quick_sort #QuickSort(array,first,q-1)
addi $a1, $t0, 1 #q+1
lw $a2, last #last value on the third parameter
jal quick_sort
You are using $t0 as q local variable, but you never save it on the stack.
After the first call jal quick_sort #QuickSort(array,first,q-1) the value of $t0 would be different, but you use it immediately in the line addi $a1, $t0, 1 #q+1 as if it never changed, and then pass the result to the second call to QuickSort.
The C equivalent to this error would be to make q global and add q = 0; at the beginning of the function.
You must remember, that when working in assembly and using registers for local variables, you must save their state to the stack before calling any other function from your current function, otherwise you will lose state and your code will not work as expected!
To be honest, this is my first time seeing this particular assembly language, so there might be other errors I missed, but these are the most obvious ones so they were easy to spot.
so my professor put this problem in his slides and this was his answer, not understanding how he converted it into MIPS so if anyone could help explain this that would be great.
Variable i is in $s3, k is in $s5, and the base address of save[] is in $s6
He gave us this C code:
while( save[i] == k ) {
i += 1;
}
And gave us this MIPS code in response:
Loop: sll $t1, $s3, 2
add $t1, $t1, $s6
lw $t0, 0($t1)
bne $t0, $s5, Exit
addi $s3, $s3, 1
j Loop
Exit:
Loop: sll $t1, $s3, 2 # $t1 = 4*i (this is the offset to get to the ith element in your array)
add $t1, $t1, $s6 # $t1 = 4*i + base addr of save (getting the address of save[i])
lw $t0, 0($t1) # $t0 = save[i] (actually loading 4B from address of save[i], so getting the actual number here)
bne $t0, $s5, Exit # branch to Exit if save[i] != k
addi $s3, $s3, 1 # i++
j Loop
Exit:
Things to note here:
save is an int array, so each element is 4B. That's why the offset is 4*i.
I'm new at MIPS and have been trying to copy elements from one array to another. I'm unsure about how to go about this. It doesn't really matter what size the array is but lets just say for the sake of doing it that its size 10. I am little weak with MIPS loops and am kind of confused on how to proceed.
add $s0, $zero, $zero
add $t0, $zero, $zero
lui $s0, 0x1001
ori $s0,$s0,0
lui $t0, 0x1001
ori $t0, $t0, 0x0040
There my initialization with $s0 being the address first element in the first array and $t0 being the address of the first element in the 2nd one.
I do not believe the code you have provided is correct, but assuming it is, you would do something like this:
xor $t1, $t1, $t1 ; Zero out $t1
lw $t2, array_length ; Load the length of the array in $t2
loop_start:
lb $t3, $s0 ; Load the next byte from $s0 into $t3
sb $t3, $t0 ; Store the by in $t3 into $t0
addi $s0, $s0, 1 ; Move to the next byte in the source
addi $t0, $t0, 1 ; Move to the next byte in the destination
addi $t1, $t1, 1 ; increment the counter
blt $t1, $t2, loop_start ; Jump to the start of the loop of there are more bytes
Disclaimer: I have not programmed in MIPS since college so this code may not be 100% accurate, but I believe it will give you a place to start.