MIPS - compare input string to one stored in memory - mips

I have a functioning string compare method written in MIPS (bit by bit comparison of two strings from an input by the user), but I'm trying to update it to compare the second input with one that I have stored in memory. (If those two are equal, I want to use the first string somewhere else).
However, I'm running into some issues. Here's my code:
.data
str1: .space 20
str2: .space 20
msg1:.asciiz "Please enter string (max 20 characters): "
msg2: .asciiz "\n Please enter method (max 20 chars): "
msg3:.asciiz "\nSAME"
msg4:.asciiz "\nNOT SAME"
.text
.globl main
main:
li $v0,4 #loads msg1
la $a0,msg1
syscall
li $v0,8
la $a0,str1
addi $a1,$zero,20
syscall #got string to manipulate
li $v0,4 #loads msg2
la $a0,msg2
syscall
li $v0,8
la $a0,str2
addi $a1,$zero,20
syscall #got string method
la $a0,str1 #pass address of str1
la $a1,str2 #pass address of str2
jal methodComp #call methodComp
beq $v0,$zero,ok #check result
li $v0,4
la $a0,msg4
syscall
j exit
ok:
li $v0,4
la $a0,msg3
syscall
exit:
li $v0,10
syscall
methodComp:
add $t0,$zero,$zero
add $t1,$zero,$a0
add $t2,$zero,$a1
loop:
lb $t3($t1) #load a byte from each string
lb $t4($t2)
beqz $t3,checkt2 #str1 end
beqz $t4,missmatch
slt $t5,$t3,$t4 #compare two bytes
bnez $t5,missmatch
addi $t1,$t1,1 #t1 points to the next byte of str1
addi $t2,$t2,1
j loop
missmatch:
addi $v0,$zero,1
j endfunction
checkt2:
bnez $t4,missmatch
add $v0,$zero,$zero
endfunction:
jr $ra
Essentially, what I was hoping to do was declare at the beginnning
strToCompare: .asciiz "foo"
and change where I pass the adress of str1 from
la $a0,str1 #pass address of str1
to
la $a0,strToCompare #pass address of strToCompare
Does anyone know why this would not work, or a better way to implement this?
Thanks a bunch, sorry if my formatting is off, this is my first time posting.

You are missing the line feed character (\n) that your input method (the system call you are using to input data from the user) is adding at the end.
You can either preprocess your input string to remove that line feed or add it to your stored string.
The latter is easy, just change
strToCompare: .asciiz "foo"
to
strToCompare: .asciiz "foo\n"

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

Why is the upper and lower bound not resetting properly after giving L or H, so the computer guesses are off. MIPS code

.data
prompt : .asciiz "Enter the secret number : "
prompt2 : .asciiz "\nComputer guess is : "
higherLowerOrCorrect : .asciiz "\nNumber is higher (h) lower (l) or correct/exit (x) : "
.text
li $v0,4
la $a0,prompt #it will print prompt for year
syscall
li $v0,5
syscall #wait for user input
move $t2,$v0
li $t7,100 #higher bound
li $t6,0 #lower bound
li $t5,0 #stores guess
loop :
move $a1, $t7 #Here you set $a1 to the max bound.
li $v0, 42 #generates the random number.
syscall
add $a0,$a0,$t6
move $t5,$a0
li $v0,4
la $a0,prompt2 #it will print prompt for year
syscall
move $a0,$t5
li $v0, 1 #1 print integer
syscall
li $v0, 4
la $a0, higherLowerOrCorrect
syscall
li $v0, 12 #GET CHARACTER
syscall
beq $v0,'l',setHigherBound #IF Y DO THE LOOP
beq $v0,'h',setLowerBound #IF Y DO THE LOOP
beq $v0,'x',exit #IF Y DO THE LOOP
setHigherBound:
move $t7,$t5
j loop
setLowerBound:
add $t7,$t7,$t6
sub $t7, $t7,$t5
move $t6,$t5
j loop
exit:
output:
Your setHigherBound branch doesn't take into account that the current lower bound might be non-zero. Instead of move $t7,$t5 it should be sub $t7,$t5,$t6.

Implementing two function in Mips Assembley that finds the length of a word

I need help creating two functions. The first function needs to find the length of a word. For input the function takes the address of the first character of the word and return the length of the word (the words are in an array).
The second function needs to delete the words in the array that had more characters than the average (counting from all the characters in the array) so lets say the average is 4 characters in a word, all words that have more than 4 characters need to be deleted.
UPDATE: prntscr.com/ezjylq
I give you two example code I hope this help.
This code finding the length of arbitrary ASCII string
Given String:
"Hello\n"
Output:
6
.data
message: .asciiz "Hello\n"
.text
main:
li $t1,0
la $t0,message #load message to t0
loop:
lb $a0,0($t0) #load one byte of t0 to a0
beqz $a0,done #branch if a0 = 0
addi $t0,$t0,1 #increament t0
addi $t1,$t1,1 #increament the counter t1
j loop
done:
li $v0,1 #print an integer
add $a0, $0,$t1 #add the counter to a0
syscall
li $v0,10 #exit program
syscall
This is a function printing "Hello World".
.data
message: .asciiz "Hello World.\n"
.text
main:
jal displayMessage
li $v0,10 #exit function
syscall
displayMessage:
li $v0,4 #printing string
la $a0,message #save the message to argument $a0
syscall
jr $ra

accessing first character of an string and comparing it with an char 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