Help please there is a bug which is causing my code to output -1 even when it shoud not. It is supposed to be a MIPS assembly language program that asks the user to input 2 strings (each should be no longer than 50 characters including the null terminator). The program should determine whether the second string is a substring of the first. If it is, then the program should print out the first index in which the second string appears in the first. As an example, if the first string is “Hello World” and the second string is “lo”, then the program should print out 3, i.e. the starting index of “lo” in “Hello World.” If the second string is not contained in the first string, then the program should print out -1. .datastr1: .space 51str2: .space 51msg1: .asciiz " Please enter a string: "msg2: .asciiz " Please enter a 2nd string: ".text.globl mainmain:# print message 1li $v0, 4la $a0, msg1syscall# read string 1li $v0, 8la $a0, str1li $a1, 51syscall# print message 2li $v0, 4la $a0, msg2syscall# read string 2li $v0, 8la $a0, str2li $a1, 51syscall# find substringla $t0, str1la $t1, str2find_substring:lb $t2, 0($t0)beqz $t2, not_foundlb $t3, 0($t1)beq $t2, $t3, check_substringaddiu $t0, $t0, 1j find_substringcheck_substring:addiu $t1, $t1, 1lb $t3, 0($t1)beqz $t3, foundlb $t2, 1($t0)beq $t2, $t3, check_substringla $t1, str2addiu $t0, $t0, 1j find_substringfound:la $a0, str1sub $a0, $t0, $a0li $v0, 1syscallj exitnot_found:li $v0, 1li $a0, -1syscallexit:li $v0, 10syscall
Help please there is a bug which is causing my code to output -1 even when it shoud not.
It is supposed to be a
MIPS assembly language
The program should determine whether the second string is a substring of the first.
If it is, then the program should print out the first index in which the second string appears in the first.
As an example, if the first string is “Hello World” and the second string is “lo”, then the program should print out 3, i.e. the starting index of “lo” in “Hello World.”
If the second string is not contained in the first string, then the program should print out -1.
.data
str1: .space 51
str2: .space 51
msg1: .asciiz " Please enter a string: "
msg2: .asciiz " Please enter a 2nd string: "
.text
.globl main
main:
# print message 1
li $v0, 4
la $a0, msg1
syscall
# read string 1
li $v0, 8
la $a0, str1
li $a1, 51
syscall
# print message 2
li $v0, 4
la $a0, msg2
syscall
# read string 2
li $v0, 8
la $a0, str2
li $a1, 51
syscall
# find substring
la $t0, str1
la $t1, str2
find_substring:
lb $t2, 0($t0)
beqz $t2, not_found
lb $t3, 0($t1)
beq $t2, $t3, check_substring
addiu $t0, $t0, 1
j find_substring
check_substring:
addiu $t1, $t1, 1
lb $t3, 0($t1)
beqz $t3, found
lb $t2, 1($t0)
beq $t2, $t3, check_substring
la $t1, str2
addiu $t0, $t0, 1
j find_substring
found:
la $a0, str1
sub $a0, $t0, $a0
li $v0, 1
syscall
j exit
not_found:
li $v0, 1
li $a0, -1
syscall
exit:
li $v0, 10
syscall
Step by step
Solved in 2 steps