accessing first character of an string and comparing it with an char MIPS - mips

I want to compare a first character of a string with '#' char. If these are equal I want to print "they're equal" in mips. To do this, I've written a piece of code as below.However it does not give me an output even if they're equal. Is there anyone to help me ?
Thanks in advance.
.data
input: .space 201
string2: .asciiz "they're equal.\n"
finish: .byte '#'
.text
main:
la $a0,input
li $a1,201 #read 200 char
li $v0,8 #read string
syscall
jal evaluate
evaluate:
lw $t1, 0($a0)
lw $t2,finish
beq $t1,$t2,testi
testi:
la $a0,string2
li $v0,4
syscall
li $v0, 10
syscall

Yes, you have placed the branch in such a way that the next instruction is the same regardless of whether or not the branch is taken.
Consider changing it to something like this:
evaluate:
lw $t1, 0($a0)
lw $t2,finish
bne $t1,$t2,testi
la $a0,string2
li $v0,4
syscall
test1:
li $v0, 10
syscall

Related

Memory address out of bounds error in MIPS but works in a different function

I am making a program in MIPS that will compare two strings but I can't seem to access the proper locations. I have a function that calculates the lenght of the strings which works fine, but when I try to use a different function (in this case its just a test to see if it works), it says that i'm acessing the wrong memory. I am using QTSpim to test it. The function that works is the duzinaStringa, the one that does not is the krajStringa. Currently it's just trying to print out the first character of the string so I can make sure it works
.text
krajStringa:
li $v0,0
li $t0,0 #brojac1
move $t2, $a0 #adresa prvog
move $t3, $a1 #adresa drugog
lb $a0,0($t2)
li $v0,4
syscall
jr $ra
duzinaStringa:
move $t1,$a0
li $t0,0
petlja:
lb $t2,0($t1)
beqz $t2,krajDuzine
addi $t1,$t1,1
addi $t0,$t0,1
j petlja
krajDuzine:
addi $t0,$t0,-1
move $v0,$t0
jr $ra
main:
la $a0,str1
li $v0, 4
syscall
la $a0, arr
li $a1,200
li $v0, 8
syscall
la $a0,str1
li $v0, 4
syscall
la $a0, arr2
li $a1,200
li $v0, 8
syscall
la $a0,arr
jal duzinaStringa
move $a2,$v0
la $a0,arr2
jal duzinaStringa
move $a3,$v0
la $a0,arr
la $a1,arr2
jal krajStringa
li $v0,10
syscall
.data
arr: .space 200
arr2: .space 200
str1: .asciiz "Unesi string: \n"
As #ErikEidt pointed out, I used the wrong system call when printing the first character of a string.

How to store the input correctly in MIPS?

Although I have try to input different value ,but the Qtspim still print out the value"0" and the input is also not saved in the register ,I try to find the answer in internet,but it seem like no one have face this problem before.I do it work for 4hrs, I FEEL very frustrated with this.
here is my code ,I try to define whether the input is prime number or not.
# Power program
# ----------------------------------
# Data Segment
.data
request: .asciiz "Enter an integer:"
Yes: .asciiz " is a prime number.\n"
No: .asciiz " is not a prime number\n."
End: .asciiz"End!\n"
Key: .word -1
One: .word 1
# ----------------------------------
# Text/Code Segment
.text
.globl main
main:
lw $t1,One
add $s2,$zero,$zero
lw $s3,One
lw $s0,Key
add $s5,$zero,$zero
j power # call the function power
li $v0, 10 # call code for exit
syscall # system call
.end main
.globl power
power:
j power_loop
power_loop:
la $a0, request # load string request in memory to $a0
li $v0, 4 # print out the string
syscall #prevent Memory address out of bounds
li $v0, 5
syscall
move $s7,$v0
beq $s7, $s0, exit_L #when input =-1 exit
j Prime #otherwise go to define the input whether prime num
Prime:
beq $s7,$t1,result
div $s7,$t1
mthi $t4
beq $t4,$zero,count
addi $t1,$t1,1
j Prime
count: #counting the number of factors
addi $s2,1
addi $t1,$t1,1
result: addi $t1,$zero,1
bne $s2,$s3,resultNo
beq $s2,$s3,resultYes
resultYes: li $v0,1
move $a0,$s7
syscall
add $s2,$zero,$zero
la $a0,Yes
li $v0,4
syscall
j power_loop
resultNo: li $v0,1
move $a0,$s7
syscall
add $s2,$zero,$zero
la $a0,No
li $v0,4
syscall
j power_loop
exit_L:
la $a0,End
li $v0,4
syscall
jr $ra
.end power

MIPS Converting Decimal To Binary Run Errors

Can someone look through my codes and see if anything wrong that has been causing me errors?
**Error I received:**spim:(parser) Label is defined for the second time on line 8 of file C:/Users/Desktop/test5.asm main:
^
Is the error trying to say that i have 2 "main" word in my code? I tried removing one of it..but failed.
.data
msg1: .asciiz "Please insert value (A > 0) : "
msg2: .asciiz "Please insert the number system B you want to
convert to (2<=B<=10): "
#Above sting must be in one line
msg3: .asciiz "\nResult : "
.text
.globl main
main:
addi $s0,$zero,2
addi $s1,$zero,10
getA:
li $v0,4
la $a0,msg1
syscall
li $v0,5
syscall
blt $v0,$zero,getA
move $t0,$v0
getB:
li $v0,4
la $a0,msg2
syscall
li $v0,5
syscall
blt $v0,$s0,getB
bgt $v0,$s1,getB
add $t1,$zero,$v0
li $v0,4
la $a0,msg3
syscall
add $a0,$zero,$t0
add $a1,$zero,$t1
jal convert
li $v0,10
syscall
convert:
#a0=A
#a1=B
addi $sp,$sp,-16
sw $s3,12($sp) #counter,used to know
#how many times we will pop from stack
sw $s0,8($sp) #A
sw $s1,4($sp) #B
sw $ra,0($sp)
add $s0,$zero,$a0
add $s1,$zero,$a1
beqz $s0,end
div $t4,$s0,$s1 #t4=A/B
rem $t3,$s0,$s1 #t3=A%B
add $sp,$sp,-4
sw $t3,0($sp) #save t3
add $a0,$zero,$t4 #pass A/B
add $a1,$zero,$s1 #pass B
addi $s3,$s3,1
jal convert #call convert
end:
lw $ra,0($sp)
lw $s1,4($sp)
lw $s0,8($sp)
lw $s3,12($sp)
beqz $s3,done
lw $a0,16($sp)
li $v0,1
syscall
done:
addi $sp,$sp,20
jr $ra #return
Solved.TQ
Turned out that the SPIM compiler was messed up

What does the following QtSPIM/MIPS code do

What does the following QtSPIM/MIPS code do. Describe by referring to the functions of various blocks of the code (Block1, Block2, …).
Answer the questions in front of certain instructions.
Block1:
.text
main:
li $s4, 0
la $a0, input
li $v0, 4
syscall
li $v0, 5
syscall
move $t0, $v0 #Why do we need this?
Block2:
la $a0, output
li $v0, 4
syscall
move $a0, $t0
li $v0, 1
syscall
la $a0, newline # How will the output change if newline is replaced by newspace defined as “ “?
li $v0, 4
syscall
Block3:
Do:
move $a0, $s4
li $v0, 1
syscall
la $a0, newline
li $v0, 4
syscall
slt $s1, $s4, $t0
addi $s4, $s4, 1
bgt $s1, $zero, Do # Why can’t we use “beq $s1, 1, Do “?
Block4:
Exit:
li $v0, 10
syscall
.data
input: .asciiz "Input a number: "
output: .asciiz "Let me count till "
newline: .asciiz "\n "
So I need help with identifying what the code does... I have the concept I just want o make sure Im properly figuring this out... So I have this so far:
move $t0, $v0 #Why do we need this? --- to store number in t0
la $a0, newline # How will the output change if newline is replaced by newspace defined as “ “?
--- load address of string to be printed into $a0 and adds the space if replaced by newspace
the last one I cant quite figure out...

Take user input and print a floating point array in MIPS

I need to take input from the user a floating point array and then print it. I tried the following code:-
.text
.globl main
main:
la $s0,size
lw $s1,0($s0) # size in $s1
ori $s2,$zero,0 # i in $s2
la $s3,arr # arr in $s3
li $v0,4
la $a0,msg1
syscall
L1:
beq $s2,$s1,DONE
li $v0,6
syscall
swc1 $f0,0($s3)
j UPDATE
UPDATE:
addi $s3,$s3,4
addi $s1,$s1,1
j L1
DONE:
li $v0,4
la $a0,msg2
syscall
la $t0,arr
ori $t1,$zero,0
L2:
beq $t1,$s1,EXIT
lwc1 $f20,0($t0)
li $v0,2
mov.s $f12,$f20
syscall
addi $t0,$t0,4
addi $t1,$t1,1
j L2
EXIT:
li $v0,10
syscall
.data
size: .word 9
arr: .float 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
msg1: .asciiz "Enter the elements:-"
msg2: .asciiz "The elements are:-"
When i give the input, there is a runtime exception 'invalid float input' at syscall 6. Please help!!!
I guess you are using , as the decimal point instead of ..
E.g.: instead of entering 3.14159 you are entering 3,14159 which is not expected at least by MARS.
Aside from that, I think the line addi $s1,$s1,1 in your code should be addi $s2,$s2,1, as you seem to be using $s2 to hold the current value of your index counter.