Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a mips assembly language program that allows the user to enter two strings

ID: 3756369 • Letter: W

Question

Write a mips assembly language program that allows the user to enter two strings. Have the program determine whether the second string is a substring of the first. If it is infact a substring of the first string, print out the index location of where it begins in the first string. For example:

string #1: lamp shade

string #2: sh

Resulting output: 5

As 5 is the index location as to where the substring starts in the first string. If the second string is not a substring of the first, make the resulting output -1. The -1 will inform the user that it's not a substring of the first.

Explanation / Answer


Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you


.data
prompt1: .asciiz "string #1: "
prompt2: .asciiz "string #2: "
result: .asciiz "Resulting output: "
str1: .space 30
str2: .space 30
.text
#prompt and read string
li $v0, 4
la $a0, prompt1
syscall
#read string
li $v0, 8
la $a0, str1
li $a1, 30
syscall
#prompt and read string
li $v0, 4
la $a0, prompt2
syscall
#read string
li $v0, 8
la $a0, str2
li $a1, 30
syscall
la $t0, str1 #start with 1st locatoin in str1
li $t1, 0 #start at index 0
li $t2, -1 #final output index
loop1:
move $t3, $t0
la $t5, str2 #get current char in str2
loop2:
lb $t4, ($t3) #get current char
beq $t4, '', end_loop1
beq $t4, ' ', end_loop1
lb $t6, ($t5) #get current char
beq $t6, '', end_loop2
beq $t6, ' ', end_loop2
bne $t6, $t4, loop1_next #no match
add $t5, $t5, 1 #move to next char in str2
add $t3, $t3, 1 #move to next char in str1
b loop2
end_loop2:
#matched all , so found it at index $t1
move $t2, $t1
b end_loop1
loop1_next:
add $t0, $t0, 1 #next location in str1
add $t1, $t1, 1 #next index of str1
b loop1
end_loop1:
li $v0, 4
la $a0, result
syscall
li $v0, 1
move $a0, $t2
syscall
#exit
li $v0, 10
syscall
output
-----
string #1: lamp shade
string #2: sh
Resulting output: 5
-- program is finished running --
string #1: lamp shade
string #2: lamp
Resulting output: 0
-- program is finished running --
string #1: lamp shade
string #2: abc
Resulting output: -1
-- program is finished running --

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote