Create a function in MIPS using MARS to determine whether a user input string is a palindrome or not. Assume that the function is not part of the same program that is calling it. This means it would not have access to your .data segment in the function, so you need to send and receive information from the function itself. The program should be as simple as possible while still using necessary procedures. Follow the instructions below carefully.
Instructions:
● Prompt the user and let them know what the program is supposed to do, what they should be entering, and what the output is.
● Take in the user input and output the result as a portion of the “main” of your program.
● Program must make use of a function to solve whether the input string is a palindrome or not.
● Function must take in the location of the string as an argument. Don’t just load it from your .data segment.
● Function must return some value to let your main know the outcome of its operation.
● Make sure the size of string that can be entered can be at least 20 characters long.
● You can not ask the user the size of the string they entered.
● Keep in mind our register conventions and how certain registers should be used or preserved.
Helpful Hints:
● Finding the end of the string will be important, once again remember that each character is a single byte.
● When you enter your string you press enter to show that you are done. This means that the character inserted by the enter key is the final character of the string, but not something we want to check as part of our palindrome.
● It may be helpful to start by just printing through the characters of your string in a loop as part of your function to get a feel for navigating through it.
● Use characters of all the same case (all lowercase or all uppercase) to make things easier on yourself.
Sample Output:
● Note that the bold words are user input and that this is two runs of the program.
This program will take in a string and then test if it is a palindrome or not.
Enter your string to test: racecar
Your string is a palindrome!
-- program is finished running --
This program will take in a string and then test if it is a palindrome or not.
Enter your string to test: hello
Your string is not a palindrome.
-- program is finished running --
Greetings!!
Code:
#DATA SEGMENT
.data
prompt: .asciiz "Enter a string to determine whether it is palindrome or not"
string: .byte 0:250
size: .word 250
out1: .asciiz "String is Palindrome"
out2: .asciiz "String is not Palindrome"
#CODE SEGMENT
.text
main:
#DISPLAY PROMPT MESSAGE
la $a0,prompt
li $v0,4
syscall
#READ THE STRING
la $a0,string
lw $a1,size
li $v0,8
syscall
#CALLING THE FUNCTION PALINDROME
jal palindrome
#DISPLAY WHETHER THE STRING IS PALINDROME OR NOT
move $t6,$v0 #v0 have 1 if the string is palindrome else 0
beq $t6,1,pal #if 1 then go and print string is pal message
la $a0,out2 #else print string is not palindrome
li $v0,4
syscall
j end
pal:
la $a0,out1 #print string is palindrome message
li $v0,4
syscall
#STANDARD TERMINATION
end:
li $v0,10
syscall
#MAIN ENDS HERE
palindrome:
li $t1,1 #initialize to 1 for counting the number of char
move $t3,$a0 #save string address passed as parameter
#COUNTING THE NUMBER OF CHARACTERS
loop:
lb $t0,0($t3) #read one char
beq $t0,0x0A,endp #if end of string then goto endp
addi $t1,$t1,1 #else count the number of characters in the string
addi $t3,$t3,1 #increment the array address
j loop #repeat
endp:
li $t2,0 #initialize t2 to 0 for counting the comparisons
move $t3,$a0 #save the array address
add $t4,$a0,$t1 #calculate the last character address in the string
addi $t4,$t4,-2 #adjust the last address
div $t1,$t1,2 #divide the character count by 2 and used for controlling the number of comparisons of characters
repeat:
beq $t2,$t1,endpr #do comparisons till the half the length
lb $t0,0($t3) #read one char from start of the string
lb $t5,0($t4) #read one char from end of the string
bne $t0,$t5,not_pal #if both the characters are not matching
addi $v0,$0,1 #else if both the characters are matching v0=1
addi $t3,$t3,1 #increment the start address
addi $t4,$t4,-1 #decrement the end address
addi $t2,$t2,1 #loop control
j repeat #repeat
not_pal:
addi $v0,$0,0 #if both the characters are not matching,v0=0
endpr:
jr $ra #return to main
Output screenshot:
Hope this helps
Get Answers For Free
Most questions answered within 1 hours.