QUICKSORT IN MIPS - c++

I have written a quicksort algorrithm in MIPS assembly according to C++ code. In C++, it works very well but in MIPS, it doesn't work. I debugged it, and problem is recursion. This is the algorithm:
QuickSort(Data[], l , r)
{
// If the first index less or equal than the last index
if l <= r:
{
// Create a Key/Pivot Element
key = Data[r]
// Create temp Variables to loop through array
i = l;
j = r;
while i <= j:
{
while Data[i] < key AND i < r:
i = i + 1
while Data[j] > key AND j > 0:
j = j - 1
if i <= j:
{
swap Data[i], Data[j]
i = i + 1
j = j + 1
}
}
if l < j:
QuickSort(Data, l, j);
if r > i:
QuickSort(Data, i, r);
}
}
This is MIPS code. It works in some cases. ex: array = {6, 5, 4, 3, 2, 1}.
MIPS code:
#-function QuickSort(arr, left,right)
#parameter
# $a0: array
# $a1: left
# $a2: right
QuickSort:
subu $sp, $sp, 16
sw $a0, 0($sp)
sw $a1, 4($sp)
sw $a2, 8($sp)
sw $ra, 12($sp)
la $s0, 0($a0)
move $s1, $a1
move $s2, $a2
bgt $s1, $s2, done
sll $t3, $s2, 2
add $t3, $s0,$t3
lw $t2, 0($t3)
move $t0, $s1
move $t1, $s2
WhileOuter:
While_i:
sll $t3, $t0, 2
add $t3, $s0, $t3
lw $t4, 0($t3)
bge $t4, $t2, EndWhile_i
bge $t0, $s2, EndWhile_i
addi $t0, $t0, 1
j While_i
EndWhile_i:
While_j:
sll $t3, $t1, 2
add $t3, $s0, $t3
lw $t4, 0($t3)
ble $t4, $t2, EndWhile_j
blez $t1, EndWhile_j
addi $t1, $t1, -1
j While_j
EndWhile_j:
bgt $t0, $t1, EndWhileOuter
#swap arr[i], arr[j]
sll $t4, $t0, 2
sll $t5, $t1, 2
add $s3, $s0, $t4
add $s4, $s0, $t5
lw $t4, 0($s3)
lw $t5, 0($s4)
sw $t4, 0($s4)
sw $t5, 0($s3)
addi $t0, $t0, 1
addi $t1, $t1, -1
j WhileOuter
EndWhileOuter:
bge $s1, $t1, call2
lw $a1, 4($sp)
move $a2, $t1
move $a0, $s0
jal QuickSort
call2:
ble $s2, $t0, done
move $a1, $t0
lw $a2, 8($sp)
move $a0, $s0
jal QuickSort
done:
addu $sp, $sp, 16
lw $a0, 0($sp)
lw $a1, 4($sp)
lw $a2, 8($sp)
lw $ra, 12($sp)
jr $ra
Can anyone find the error(s) in this code? Thanks for any help.

You are using the saved registers $s0, $s1, $s2, but without following the requirement to preserve the values in those registers for the caller.
Thus, the callers of QuickSort are not guaranteed their $s registers will be preserved.
You aren't showing the rest of the code, e.g. the main.
However, we know that QuickSort calls itself, and, after the first recursive call to itself, it relies on the $s0 & $s2 registers, which should be ok, but we know they aren't properly preserved by QuickSort.
You need to more closely analyze your register usage and requirements. The following code runs after the first (recursive) call to QuickSort. It rightfully expects $s0 and $s2 to be preserved, but also expects $t0 to be preserved — which is a temporary register meaning it is not preserved by a call, so this is an error.
jal QuickSort # this call wipes out $t0
call2:
ble $s2, $t0, done # what's supposed to be in $t0 here?
move $a1, $t0
lw $a2, 8($sp)
move $a0, $s0
You have no requirement for saved register $s1 and should have chosen a temporary register for that usage instead. I would have just used the variable in its original $a1 register.
You're saving $a0 register to memory but not using that memory location's value.
It is either missing or you've changed the location of the outer while loop's exit condition. It is no longer at the top of the loop. It now reads like this:
while true:
{
while Data[i] < key AND i < r:
i = i + 1
while Data[j] > key AND j > 0:
j = j - 1
if i > j break;
The Python code does
if i <= j:
{
swap Data[i], Data[j]
i = i + 1
j = j + 1
}
whereas after the swap the assembly code does i++ yet j-- .
You're using $s3 and $s4 but for simple temporary usages — use $t registers for these purposes instead.

Related

left most bit of a number mips

# 1 - Request a number
# 2 - Read the number
# 3 - Display Binary msg
# 4 - Display the number as binary
# 5 - Find the left-most bit (loop)
# 6 - Display the result msg
# 7 - Display the position
.data
prompt: .asciiz "Enter a 16 bit number: "
binaryMsg: .asciiz "\nBinary: "
results: .asciiz "\nThe left most bit is in position(zero based): "
.text
.globl main
main:
li $v0, 4
la $a0, prompt
syscall
li $v0, 5
syscall
move $a1, $v0
li $v0, 4
la $a0, binaryMsg
syscall
jal prtbin
# New Line
li $v0, 11
li $a0, 10
syscall
jal main
prtbin:
add $t0, $zero, $a1 # put our input ($a1) into $t0
add $t1, $zero, $zero # Zero out $t1
addi $t3, $zero, 1 # load 1 as a mask
sll $t3, $t3, 15 # move the mask to appropriate position
addi $t4, $zero, 16 # loop counter
loop:
and $t1, $t0, $t3 # and the input with the mask
beq $t1, $zero, print # Branch to print if its 0
add $t1, $zero, $zero # Zero out $t1
addi $t1, $zero, 1 # Put a 1 in $t1
j print
print:
li $v0, 1
move $a0, $t1
syscall
srl $t3, $t3, 1
addi $t4, $t4, -1
bne $t4, $zero, loop
li $v0, 4
la $a0, results
syscall
li $v0, 10
syscall
I'm building a program that takes a 16 bit number converts it to binary and gets the left most bit and outputs that position of it. I'm having two issues one being I cant seem to get it work without looping back to the main and cant seem to get the left most bit and output that into the results. any help will be much appreciated.
update
I figured out how to get it from looping but now I still cant seem to find out how to get the left most bit and display that

Mips code with PCspim exception 7

i have written a code that permutes a string in all its possible ways. The original code is in C++, and the task of the exercise is to implement the code in C++ in MIPS.For example, if you insert "abc" the result is [abc,acb,bca,bac,cba,cab]. So in C++ looks like this:
void Permutaciones(char * cad, int l) {
char c;
int i, j;
int n = strlen(cad);
for(i = 0; i < n-l; i++) {
if(n-l > 2) Permutaciones(cad, l+1);
else cout << cad << ", ";
c = cad[l];
cad[l] = cad[l+i+1];
cad[l+i+1] = c;
if(l+i == n-1) {
for(j = l; j < n; j++) cad[j] = cad[j+1];
cad[n] = 0;
}
}
}
and here's my code in MIPS
.data
str: .asciiz "abc"
coma: .asciiz ", "
.text
main:
la $a0, str
li $a1, 0
jal perm
li $v0, 10
syscall
length:
lb $t0, 0($a0)
beq $t0, $0, preperm
addi $v0, $v0, 1
addi $a0, $a0, 1
j length
preperm:
subu $a0, $a0, $v0
jr $ra
perm:
subu $sp, $sp, 32
sw $ra, 0($sp) #salvo ra
sw $a0, 4($sp) #salvo cad
sw $a1, 8($sp) #salvo L
jal length
sw $v0, 12($sp) #salvo n
subu $v0, $v0, $v0
lw $a0, 4($sp) #rescato cad
lw $a1, 8($sp) #rescato L
lw $t0, 12($sp) #rescato n
li $t2, 0 #$t2=i=0
for1:
sub $t1, $t0, $a1 # $t1= n- L
bge $t2, $t1, fin1 # i >= n- L => fin1
li $t3, 2 # $t3=2 (constante)
bgt $t1, $t3, recursion #n-L >2 => recursion
mostrar:
li $v0, 4
syscall
la $a0, coma
syscall
subu $v0, $v0, $v0
jal nomostrar
recursion:
sw $t2, 16($sp) #salvo i
addi $a1, $a1, 1 #l++
jal perm #recurso
nomostrar:
lw $a0, 4($sp) #rescato cad
lw $a1, 8($sp) #rescato L
lw $t0, 12($sp) #rescato n
lw $t2, 16($sp) #rescato i
add $t4, $a0, $a1 #$t4 = direccion cad[L]
lb $t5, 0($t4) #$t5=cad[L]
add $t6, $a0, $a1
add $t6, $t6, $t2
addi $t6, $t6, 1 #$t6=direccion cad [L+i+1]
lb $t7, 0($t6) #$t7 = cad [L+i+1]
sb $t7, 0($t4)
sb $t5, 0($t6) #swap (cad[L],cad [L+i+1])
add $t7, $a1, $t2 #$t7 = L+i
subu $t5, $t0, 1 #$t5 = n-1
bne $t7, $t5, cola #L+i != n-1 => cola
or $t8, $a1, $0 #t8= j = L
for2:
bge $t8, $t0, cola # j >= n => cola
add $t5, $a0, $t8 #$t5 = dirección de cad[j]
addi $t1, $t5,1 #$t1 = dirección de cad[j+1]
lb $t7, 0($t1) #$t7 = cad[j+1] propiamente dicho
sb $t7, 0($t5) # cad[ j ] = cad[j+1]
addi $t8, $t8, 1 # j++
j for2 # repetir
cola:
add $t8, $a0, $t0 #$t8 = dirección de cad[n]
sb $0, 0($t8) # cad[n] = '\0'
addi $t2, $t2, 1 # i++
sw $t2, 16($sp)
j for1 # repetir
fin1:
lw $ra, 0($sp) #rescato ra
sw $0, 16($sp)
addi $sp, $sp,32 #libero memoria
jr $ra #retorno
So, the problem is that when i run the program in PCspim using F5 i get the first output "abc" then i get Exception 7 Bad data address two times, and then it shows some more letters of the string. But when I use the step by step function (F10), the code works perfectly. Please someone help me, because i`m losing my mind.

If greater than or equal in MIPS

Prompt for and input two integers “a” and “b” using syscalls
Display one of the following statements depending on if a>b, or a=b or a
You entered a greater than b
You entered an equal to b
You entered a less than b
I have to get this prompt and I tried so hard to get it done. This is where I'm stucked, I'd really appreciate your help.
.data
p1: .asciiz "Please enter the first number ? "
p2: .asciiz " Please enter the second number? "
ans1: .asciiz " \nYou entered a greater than b "
ans2: .asciiz " \nYou entered a equal to b "
ans3: .asciiz " \nYou entered a less than b "
.text
.globl main
main:
li $v0, 4 #system call code for print_str
la $a0, p1 #address of string to print
syscall #print the first prompt
li $v0, 5 #system call code for read_int
syscall #read first integer
move $t1, $v0 #store it till later
li $v0, 4 #system call code for print_str
la $a0, p2 #address of string to print
syscall #prints the second prompt
li $v0, 5 #system call code for read_int
syscall #read first integer
move $t2, $v0 #store it till later
slt $t1,$s1,$s0 # checks if $s0 > $s1
beq $t1,1,label1
I really don't know how to use branch statements, and it's really confusing. I would like to know how to fix it.
Why do you read the numbers into $t1 and $t2 then compare $s1 and $s0? Where is it confusing?
Simply use slt and beq/bne, that'll cover all comparison cases you need.
Suppose a is in $s0, b is in $s1
a < b:
slt $t0, $s0, $s1
bne $t0, $zero, a_lt_b # $t0 == 1 != 0 if a < b
a = b:
beq $s0, $s1, a_eq_b # nothing more to explain
a > b:
slt $t0, $s1, $s0
bne $t0, $zero, b_lt_a # $t0 == 1 != 0 if b < a
a >= b:
slt $t0, $s0, $s1
beq $t0, $zero, a_ge_b # $t0 == 0 if a >= b or !(a < b)
a <= b:
slt $t0, $s1, $s0
beq $t0, $zero, b_ge_a # $t0 == 0 if b >= a or !(b < a)

MIPS : loop goes to infinity

I am trying to convert this c++ function to mips. I think I am having problem in loop because when I run it, it gives me 13..1.17.5.. but my output should be two ip addresses : 130.52.0.10 and 171.9.50.186
C++ function code :
void IPtoDD(int arg0, char *arg1)
{
int temp, numChar, shift = 24;
for (int i=0; i<4; i++) {
temp = arg0 >> shift;
temp = temp & 0x000000ff;
numChar = byteToDec(temp,arg1);
arg1 += numChar;
*arg1++ = '.';
shift -= 8;
}
arg1--;
*arg1 = 0;
return;
}
MIPS code :
IPtoDD: addi $sp, $sp, -20
sw $ra, ($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
sw $s2, 12($sp)
sw $s3, 16($sp)
move $s0, $a0
move $s1, $a1
li $s3, 24 #s3=shift
li $s2, 0 #s2=i
li $t5, 0 #t5=temp
li $t3, 0
move $s1, $a1 #s1=*arg1
loop: srl $t5, $s0, $s3 #t3= numChar
and $t5, $t5, 0xff #t4= (*arg1)
move $a0, $t5
move $a1, $s1
jal byteToDec
move $t3, $v0
add $s1, $s1, $t3
li $t5, '.'
sb $t5, ($a1)
addi $a1, $a1, 1
addi $s3, $s3, -8
addi $s2, $s2, 1
blt $s2, 4, loop
addi $s1, $s1, -1
sb $0, ($a1)
lw $s3, 16($sp)
ra: lw $s2, 12($sp)
lw $s1, 8($sp)
lw $s0, 4($sp)
lw $ra, ($sp)
addi $sp, $sp, 20
jr $ra
Could please help out here. I tried a lot but not able make it run correctly.
edit:
C++ function for byteToDec
int byteToDec(int arg0, char *arg1)
{
int temp, flag = 0, count = 0;
if (arg0==0) {
*arg1 = '0';
return 1;
}
else {
temp = arg0/100;
if (temp != 0) {
*arg1++ = (char) temp + 0x30;
count++;
flag = 1;
}
temp = (arg0 % 100) / 10;
if ((flag!=0) || (temp != 0)) {
*arg1++ = (char) temp + 0x30;
count++;
}
temp = arg0 % 10;
*arg1 = (char) temp + 0x30;
count++;
return count;
}
}
byteToDec in MIPS:
byteToDec: #t0= temp
#t1= flag
#v0= count
#t3= (*arg1)
bne $a0, $0, else
li $t3, '0'
sb $t3, ($a1)
li $v0, 1
jr $ra
else: div $t0, $a0, 100
beq $t0, 0, cont
bp2: addi $t3, $t0, 0x30
sb $t3, ($a1)
addi $a1, $a1, 1
addi $v0, $v0, 1
li $t1, 1
cont: rem $t3, $a0, 100
div $t0, $t3, 10
bne $t1, 0, nxtIf
beq $t0, 0, endElse
nxtIf: addi $t3, $t0, 0x30
sb $t3, ($a1)
addi $a1, $a1, 1
addi $v0, $v0, 1
endElse:rem $t0, $a0, 10
bp1: addi $t3, $t0, 0x30
sb $t3, ($a1)
addi $v0, $v0, 1
ra1: jr $ra
You are using t3 as your loop counter and then trashing t3 in your byteToDec function. MIPS convention is that t registers are "temp" and not to be used across function calls like that. You should put your loop variable in a s register (a "save" register), and if your called function needs to reuse that same s register, it needs to save it to the stack or something and restore the value before returning to the callee.
I Have a program and it the first loop works ell but the second to 6th loop goes to inf. I'm a beginner and could use some help
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
ofstream myfile ("Atomseries.txt");
int main()
{
float rconstant=109677.58;
int nstart, nend;
int length;
for (nstart = 1; nstart <= 6; nstart++){
for (nend= 2; nend <=nstart + 10; nend++)
length = 1/(rconstant*(1/(nstart*nstart)- 1/(nend*nend)));
myfile << length * 10000000 << endl;
}
if (nstart = 1)
myfile << "Lyman Series"<< endl;
else
;
if (nstart = 2)
myfile << "B Series" << endl;
else
;
if (nstart= 3)
myfile << " series"<< endl;
return 0;
}

C++ code into MIPS, the code in MIPS runs without error messages, but does not display results

The assignment asked us to complete a code in MIPS. The initial MIPS code needed a function we had to translate from a C++ code. I translated the function from C++ to MIPS as required.
The code in MIPS does not display any errors when I run it. However, the code does not display anything.
I tried to debug it, but I still don't see where the problem is. I am using Notepad++ as an editor and QtSpim to run the MIPS code. I posted the code(in C++ first, then in MIPS) bellow. Your help will be appreciated.
//*************C++ Code***********************
#include <stdio.h>
void update(float *, float *);
int main()
{
float x[5] = {.1, .2, .4, .6, .85};
float vx[5] = {-.2, -.15, .05, -.2, .2};
int i, steps;
for (i=0; i<5; i++)
{
printf("%d %f %f\n", i, x[i], vx[i]);
}
for (steps=0; steps<3; steps++)
{
update(x, vx);
printf("Step %d\n", steps);
for (i=0; i<5; i++)
{
printf("%d %f %f\n", i, x[i], vx[i]);
}
}
}
void update(float pos[], float vel[])
{
int i;
for (i=0; i<5; i++)
{
pos[i] += vel[i];
if (pos[i] < 0.)
{
pos[i] = 0.;
vel[i] = - vel[i];
}
else if (pos[i] > 1.)
{
pos[i] = 1.;
vel[i] = - vel[i];
}
}
}
//************************End of C++ Code*************************
//**************************Code Translated in MIPS*************
.data
x: .float 0.1
.float 0.2
.float 0.4
.float 0.6
.float 0.85
vx: .float -0.2
.float -0.15
.float 0.05
.float -0.2
.float 0.2
zero: .float 0.
one: .float 1.
space: .asciiz " "
endl: .asciiz "\n"
outStep:.asciiz "Step "
# i $s0
# steps $s1
# &x[0] $s2
# &vx[0] $s3
.globl for
.text
main: la $s2, x
la $s3, vx
move $t2, $s2
move $t3, $s3
li $s0, 0 # for (i=0; i<5; i++) {
ploop: move $a0, $s0 # printf("%d %f %f\n", i, x[i], vx[i]);
li $v0, 1
syscall
la $a0, space
li $v0, 4
syscall
l.s $f12, ($t2)
li $v0, 2
syscall
la $a0, space
li $v0, 4
syscall
l.s $f12, ($t3)
li $v0, 2
syscall
la $a0, endl
li $v0, 4
syscall
addi $t2, $t2, 4
addi $t3, $t3, 4
addi $s0, $s0, 1 # }
blt $s0, 5, ploop
li $s1, 0 # for (steps=0; steps<3; steps++) {
loop: move $a0, $s2 # update(x, vx);
move $a1, $s3
jal update
la $a0, outStep # printf("Step %d\n", steps);
li $v0, 4
syscall
move $a0, $s1
li $v0, 1
syscall
la $a0, endl
li $v0, 4
syscall
move $t2, $s2
move $t3, $s3
li $s0, 0 # for (i=0; i<5; i++) {
ploop1: move $a0, $s0 # printf("%d %f %f\n", i, x[i], vx[i]);
li $v0, 1
syscall
la $a0, space
li $v0, 4
syscall
l.s $f12, ($t2)
li $v0, 2
syscall
la $a0, space
li $v0, 4
syscall
l.s $f12, ($t3)
li $v0, 2
syscall
la $a0, endl
li $v0, 4
syscall
addi $t2, $t2, 4
addi $t3, $t3, 4
addi $s0, $s0, 1
blt $s0, 5, ploop1
addi $s1, $s1, 1
blt $s1, 3, loop
li $v0, 10
syscall
# void update(float pos[], float vel[]) {
# arg0 $a0
# arg1 $a1
# i $s0
update: l.s $f30, zero
l.s $f31, one
# The function i translated in MIPS is right bellow
addi $s0,$s0,0 # i=0
add.s $f0, $f0, $f1 # pos[i] += vel[i];
c.lt.s $f0, $f30 # if (pos[i] < 0.) {
bc1f else
add.s $f0, $f0, $f30 # pos[i]=0;
sub.s $f1, $f30 , $f1
sub.s $f31, $f30, $f31
add.s $f1, $f1, $f31 # vel[i] = - vel[i];
blt $s0, 5, update
else : c.lt.s $f31, $f0 #else if (pos[i] > 1.) {
bc1f skip
add.s $f0, $f0, $f31
sub.s $f31, $f30, $f31
sub.s $f1, $f1, $f31
blt $s0, 5, update
skip : add $s0, $s0, 0
addi $s0, $s0, 1
slti $t1, $s0, 5
blt $s0, 5, update
end: jr $31 #}
I tried it in PCSpim, but changed .globl for to .globl main. Now there is output, but your update function doesn't work yet as expected. You use $f0 and $f1, but these are never loaded or stored, so there is no update.