Translating c++ to MIPS assembly - c++

I'm trying to convert the following line of C++ code to MIPS assembly:
cout << a2[i]
This is the code I've tried but I'm getting incorrect results.
*The variable i is saved in $t5.
li $v0, 1
la $a0, a2
sll $t5, $t5, 2
add $a0, $a0, $t5
srl $t5, $t5, 2
lw $a0, 0($a0)
syscall

I'm a little rusty when it comes to MIPS. However, I do remember some things.
You generally shouldn't use the $a registers unless you're passing arguments to a function (as you do with syscall).
So, in these two cases, I would use temporary registers instead:
la $a0, a2
add $a0, $a0, $t5
becomes
la $t0, a2
add $t1, $t0, $t5
and then:
lw $a0, 0($t1)
syscall
And you can probably omit this as well:
srl $t5, $t5, 2
Other than that, I would ask what sort of incorrect output are you receiving?

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

QUICKSORT IN MIPS

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.

why jal main error (jal 0x00000000 [main])

When I run this code,There is an error in the main and it does not operate normally.
I changed the main function or thought there was a problem with the main and global main, so I repeated the correction several times.
.data
message: .asciiz " The final result is "
.text
.global main
main:
addi $a0, $zero, 3
addi $a1, $zero, 10
addi $a2, $zero, 15
addi $a3, $zero, 7
jal leaf_example
li $v0, 4
la $a0, message
syscall
li $v0, 1
syscall
leaf_example:
addi $sp, $sp,-12
sw $t1, 8
sw $t0, 4
sw $s0, 0
add $t0,$a0,$a1
add $t1,$a2,$a3
sub $s0,$t0,$t1
add $v0,$s0,$zero
lw $s0,0
lw $t0,4
lw $t1,8
addi $sp,$sp,12
jr $ra
spim: (parser) syntax error on line 4 of file
C:/Users/user/Desktop/qtspim16.asm .global main
^

How to write int to file in MIPS?

I'm doing a homework where I have to read a non-fixed numbers of integers separated by \n from a text file and sort them in a linked list (and in an array, to compare performance). After that, I have to write the sorted list into another text file, and that's where my problem is. I don't really know how syscall 15 works (write to file). I don't know what kind of input it would print to the file. I mean, I'm guessing they have to be strings, not integers... so I made a little test program and it didn't work. Here it is:
.data
archivo: .asciiz "salida.txt"
.text
# reservar memoria para 3 chars + \0
li $v0, 9
li $a0, 4
syscall
move $s0, $v0
# agregar al array el numero 1 ascii
addi $t0, $zero, 49
sw $t0, 0($s0)
addi $s0, $s0, 4
# agregar al array el numero 0 ascii
addi $t0, $zero, 48
sw $t0, 0($s0)
addi $s0, $s0, 4
# agregar al array el numero 0 ascii
addi $t0, $zero, 48
sw $t0, 0($s0)
addi $s0, $s0, 4
# agregar al array \0 al final
addi $t0, $zero, 0
sw $t0, 0($s0)
addi $s0, $s0, -12
# abrir archivo en modo lectura
li $v0, 13
la $a0, archivo
li $a1, 1
li $a2, 0
move $s1, $v0
syscall
# escribir buffer $s0 en el archivo
li $v0, 15
move $a0, $s1
move $a1, $s0
addi $a2, $zero, 4
syscall
# cerrar archivo
li $v0, 16
move $a0, $s1
syscall
# finalizar ejecucion
li $v0, 17
syscall
I tried to allocate enough memory for 3 chars + \0 char, in order to write the number 100 to the file "salida.txt". So, I stored the ascii values of 1, 0, 0 into an array (which is the allocated memory), and then decrement the pointer, to point to the beginning of that memory block. After that, I open the file in write-mode and write 4 characters of the buffer $s0.
Unfortunately, this only creates the file but writes nothing in it. Any help would be appreciated. Thanks.
I also tried writing a variable declared in .data, like this:
.data
hola: .asciiz "hola"
.text
la $s3, hola
...
# do syscall 15 with move $a1, $s3
but this didn't work either.
Turns out you need to store the numbers as bytes in the array you want to print. So, if you want to write a number, you store its ascii number as byte using "sb". I used a little example I found online and modified it to my testing needs. I stored a number backwards in an array and wrote that to a file.
.data
fout: .asciiz "testout.txt" # filename for output
.text
# allocate memory for 3 chars + \n, no need to worry about \0
li $v0, 9
li $a0, 4 # allocate 4 bytes for 4 chars
syscall
move $s0, $v0
addi $s0, $s0, 3 # point to the end of the buffer
li $t3, 10 # end line with \n
sb $t3, 0($s0)
addi $s0, $s0, -1
# start witing the number 100 backwars. ascii_to_dec(48) = 0, ascii_to_dec(49) = 1
li $t3, 48
sb $t3, 0($s0)
addi $s0, $s0, -1 # move the pointer backwards, meaning you go from the end to the beginning
li $t3, 48
sb $t3, 0($s0)
addi $s0, $s0, -1
li $t3, 49
sb $t3, 0($s0)
# Open (for writing) a file that does not exist
li $v0, 13 # system call for open file
la $a0, fout # output file name
li $a1, 1 # Open for writing (flags are 0: read, 1: write)
li $a2, 0 # mode is ignored
syscall # open a file (file descriptor returned in $v0)
move $s6, $v0 # save the file descriptor
# Write to file just opened
li $v0, 15 # system call for write to file
move $a0, $s6 # file descriptor
move $a1, $s0 # address of buffer from which to write
li $a2, 4 # hardcoded buffer length
syscall # write to file
# Close the file
li $v0, 16 # system call for close file
move $a0, $s6 # file descriptor to close
syscall # close file

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.