If greater than or equal in MIPS - if-statement

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)

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.

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

Drawing on MIPS. Runtime error

have a problem with the following code. I wrote a code for drawig a quater circle on a MARS assembler. But there is an error occurring. The program itself draws the quater circle but in the end there is an error, because of it I cannot use a loop for this procedure, the code is below
.data
DISPLAY: .space 65536
DISPLAYWIDTH: .word 128
DISPLAYHEIGHT: .word 128
.text
j main
set_pixel_color:
mulu $t0 $a1 $s0
add $t0 $t0 $a0
mul $t0 $t0 4
la $t2 DISPLAY
add $t2 $t2 $t0
sw $a2 ($t2)
jr $ra
quater_circle:
add $a1 $0 0
add $a0 $0 0
lw $a2 RED
loop2:
jal set_pixel_color
add $a0 $a0 1
mulu $t3 $a0 $a0
mulu $t4 $a1 $a1
add $t3 $t3 $t4
mulu $t4 $a3 $a3
ble $t3 $t4 loop2
add $a0 $0 $0
add $a1 $a1 1
ble $a1 $a3 loop2
jr $ra
.data
RED: .word 0xff0000
GREEN: .word 0x00ff00
BLUE: .word 0x0000ff
.text
main:
lw $s0, DISPLAYWIDTH # s0 holds MAX x
lw $s1, DISPLAYHEIGHT # s1 holds MAX y
li $a3 1
lw $a2 BLUE
add $a0 $0 $0
add $a1 $0 $0
loop:
jal set_pixel_color
add $a0 $a0 1
ble $a0 127 loop
add $a0 $0 $0
add $a1 $a1 1
ble $a1 127 loop
draw:
jal quater_circle
add $a3 $a3 1
ble $a3 128 draw
It ends up with the drawn red quater circle with the blue backround, however it terminates in the end of the procedure, and I cannot use a draw procedure. Can you help me please?
You are losing $ra in your quater_circle subroutine.
That routine is called from main and it calls another subroutine (set_pixel_color), thus losing original return address.
You should preserve $ra in your quater_circle subroutine by storing its contents into the stack (and restoring it before returning to the caller) or alternatively moving $ra to some other register (e.g, $s2) and use it to return.
For the second alternative, you would add
move $s2, $ra
at the beginning of quater_circle subroutine and change
jr $ra
with
jr $s2
at the end of the subroutine.

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.