MIPS: write a function ‘CheckName’ to compare the name with the input string. ‘C
ID: 3589682 • Letter: M
Question
MIPS: write a function ‘CheckName’ to compare the name with the input string. ‘CheckName’ takes the addresses of two strings and returns 1 if those two strings match or returns 0 otherwise
using:
.data
names: .asciiz “julia”, “steven”, “daniel”, “kate”, “adrian”
cities: .asciiz “boston”, “new york”, “chicago”, “pittsburgh”, “denver”
As you are checking for a name match, keep track of the current index of array ‘names’. For example, “julia” is at index 0, “daniel” is at index 2 and so on. If you get a name match, you can use the index to find out the corresponding city from that index of the ‘cities’ array using another function called ‘LookUp’. The function ‘LookUp’ takes the address of a string array and an index, and returns the address of the string at that index.
You also need to keep track of the address of the current string in the ‘names’ array. To increment this address by the correct amount after each loop iteration, you can use another function called ‘StrSize’. The function ‘StrSize’ takes the address of a string and returns the number of characters before the next null (0x00) character (the size of the string).
If you reach the end of the ‘names’ array, then there is no match of available names with the input name. In that case your program will print “Not found!” and exit.
here are the outputs:
I also want to know if the names are considered as words or halfs because I am not sure what to do.
Please enter a name kate this is the input City is: pittsbucghExplanation / Answer
Given below is the code for the question. Since there are mulitple functions involved, the temporary variables alone will not be enough. So we use the s register, and by convention we need to save the registers on stack before using and after use restore them. The LookUp function makes a call to another function StrSize as well, so we will have to save return address as well on the stack and restore it later.
Hope the answer helps. Please don't forget to rate the answer if it helped. Thank you.
.data
names: .asciiz "julia", "steven", "daniel", "kate", "adrian"
cities: .asciiz "boston", "new york", "chicago", "pittsburgh", "denver"
prompt: .asciiz "Please enter a name: "
citymsg: .asciiz "City is: "
notfoundmsg: .asciiz "Not found!"
name: .space 30 #a string with max 30 chars
count: .word 5
.text
#prompt user
li $v0, 4
la $a0, prompt
syscall
#get string input
li $v0, 8
la $a0, name
li $a1, 30
syscall
la $t0, names #address of current string in array
la $t1, name #address of search name
li $t2, 0 # current index
lw $t3, count #no. of elemnets in array
process_loop:
bge $t2, $t3, end_process_loop #stop processing when index is more than count
#set up parameters for CheckName and call it, the current string in array = a0, searchname = a1
move $a0, $t0
move $a1, $t1
jal CheckName
beq $v0, 1, found #check the return value of CheckName, if found, we are done
#not found yet, then find the length of the curent string in array and incrment pointer by its size + 1
move $a0, $t0
jal StrSize #call StrSize to find the lenght
add $t0, $t0, $v0 #increment pointer by size
add $t0, $t0, 1 #additional increment to skip ''
add $t2, $t2, 1 #incremetn index
b process_loop
end_process_loop:
#if we reached end of loop without goint to found, then its not found
li $v0, 4
la $a0, notfoundmsg
syscall
b exit
found:
#now we have found the search name and have the index in t2, look up the cities for the same index
la $a0, cities
move $a1, $t2
jal LookUp
#return value from LookUp has the string address in the cities array, save the return value to t4
move $t4, $v0
#dispaly the results
li $v0, 4
la $a0, citymsg
syscall
li $v0, 4
move $a0, $t4 #address of city is saved in $t4
syscall
exit:
#exit
li $v0, 10
syscall
#--------------------
#CheckName: takes 2 parameters i.e address of 2 strings and returns 1 if the strings match and 0 if not matched
CheckName:
#save the registers on stack
sub $sp , $sp, 4
sw $s1, ($sp)
sub $sp , $sp, 4
sw $s2, ($sp)
sub $sp , $sp, 4
sw $s3, ($sp)
sub $sp , $sp, 4
sw $s4, ($sp)
sub $sp , $sp, 4
sw $s5, ($sp)
#copy values of arg
move $s1, $a0 #string1 address
move $s2, $a1#string2 address
li $s5, 1 #assume the 2 strings matched
loop:
lb $s3, ($s1)
lb $s4, ($s2)
#string2 given from keyboard will have a newline in it and then null terminator
beq $s4, '', end_loop
beq $s4, ' ', end_loop
beq $s3, $s4, next_char #if current chars matche , goto next
li $s5, 0 #no match, save 0 in s5 and end loop
b end_loop
next_char:
add $s1, $s1, 1
add $s2, $s2, 1
b loop
end_loop:
move $v0, $s5 #move the results to $v0
#restore the registers in reverse order
lw $s5, ($sp)
add $sp, $sp, 4
lw $s4, ($sp)
add $sp, $sp, 4
lw $s3, ($sp)
add $sp, $sp, 4
lw $s2, ($sp)
add $sp, $sp, 4
lw $s1, ($sp)
add $sp, $sp, 4
jr $ra
#----------------
#StrSize: takes a parameter which is the address of the string and returns its length
StrSize:
#save the registers on stack
sub $sp , $sp, 4
sw $s1, ($sp)
sub $sp , $sp, 4
sw $s2, ($sp)
sub $sp , $sp, 4
sw $s3, ($sp)
#copy values of arg
move $s1, $a0 #address of string
li $s2, 0 #length
loop1:
lb $s3, ($s1) #get current char
beq $s3, '', end_loop1 #end of string ?
add $s2, $s2, 1 #increment length
add $s1, $s1 , 1 #increment pointer
b loop1
end_loop1:
move $v0, $s2
#restore the registers in reverse order
lw $s3, ($sp)
add $sp, $sp, 4
lw $s2, ($sp)
add $sp, $sp, 4
lw $s1, ($sp)
add $sp, $sp, 4
jr $ra
#----------------
#LookUp: takes the address of a string array and an index and returns the string at the index
LookUp:
#save the registers on stack
sub $sp , $sp, 4
sw $s1, ($sp)
sub $sp , $sp, 4
sw $s2, ($sp)
#we need to save the return address as well as this function is going to call another function within
sub $sp, $sp, 4
sw $ra, ($sp)
#copy values of args
move $s1, $a0 #current address
move $s2, $a1 #index
#we will use the s2 to end the loop , each time we will decrement index , and incremetn address,
#whenever index reached 0, we are at the correct address
loop2:
beqz $s2, end_loop2 #index reached 0, we reached the string needed
move $a0, $s1 #toherwise get the length of current string using StrSize
jal StrSize
#increment pointer by length of string + 1 for null teminator
add $s1, $s1, $v0
add $s1, $s1, 1
sub $s2, $s2, 1 #decrement index
b loop2
end_loop2:
move $v0, $s1 #save teh address of string needed into v0
#restore the registers in reverse order
lw $ra, ($sp)
add $sp, $sp, 4
lw $s2, ($sp)
add $sp, $sp, 4
lw $s1, ($sp)
add $sp, $sp, 4
jr $ra
output
Please enter a name:
kate
City is: pittsburgh
-- program is finished running --
Please enter a name:
julia
City is: boston
-- program is finished running --
Please enter a name:
daniel
City is: chicago
-- program is finished running --
Please enter a name:
adrian
City is: denver
-- program is finished running --
Please enter a name:
jim
Not found!
-- program is finished running --
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.