Question

Create a function in MIPS using MARS to determine whether a user input string is a...

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 --

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
MIPS ASSEMBLY Have the user input a string and then be able to make changes to...
MIPS ASSEMBLY Have the user input a string and then be able to make changes to the characters that are in the string until they are ready to stop. We will need to read in several pieces of data from the user, including a string and multiple characters. You can set a maximum size for the user string, however, the specific size of the string can change and you can’t ask them how long the string is (see the sample...
This is for C++ A string is a palindrome if reverse of the string is same...
This is for C++ A string is a palindrome if reverse of the string is same as the original string. For example, “abba” is palindrome, but “abbc” is not palindrome. Basically a string with a plane of symmetry in the middle of it is considered a palindrome. For example, the following strings are palindromic: "abbbbba", "abba", "abbcbba", "a", etc. For this problem, you have an input string consisting of both lowercase or uppercase characters. You are allowed to pick and...
Write a MIPS assembly program that asks the user first for a string, then for a...
Write a MIPS assembly program that asks the user first for a string, then for a character. The program should search the string for the character and print out the number of times the character appears in the string. Restrictions can be used, just be sure to list them. You must allocate space in the .data segment of your program for the user string.
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Using MIPS, create a stack that takes user input until the user inputs 0, then output...
Using MIPS, create a stack that takes user input until the user inputs 0, then output the characters that were entered onto the stack. Make sure to use $sp. Example: Please enter a number. Press 0 to quit: 4 5 2 5 7 0 Your numbers were: 4 5 2 5 7
create a program in c++ to determine whether any 5-letter word is a palindrome. Palindromes are...
create a program in c++ to determine whether any 5-letter word is a palindrome. Palindromes are words or sentences that read the same backward or forward. For example, “kayak” is a palindrome while “meter” is not. Ask the user to input 5 characters. You will need five separate variables to store these five characters. After obtaining the characters, compare the characters to determine if the word is a palindrome and output an appropriate message. Ex: Please enter 5 letters: h...
Write a program that accepts an input string from the user and converts it into an...
Write a program that accepts an input string from the user and converts it into an array of words using an array of pointers. Each pointer in the array should point to the location of the first letter of each word. Implement this conversion in a function str_to_word which returns an integer reflecting the number of words in the original string. To help isolate each word in the sentence, convert the spaces to NULL characters. You can assume the input...
3. Create a program which allows the user to swap individual words in an input string....
3. Create a program which allows the user to swap individual words in an input string. Given a string of input, your program should count and store the number of words in a 2D array. The user can then select the index of the words they want to swap. Your program should swap those two words and then print the entire string to ouput. • Use string prompt "> ". • Use indices prompt "Enter two indices: ". • Remove...
This is C. Please write it C. 1) Prompt the user to enter a string of...
This is C. Please write it C. 1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be...
Prompt the user to enter a string of their choosing. Store the text in a string....
Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (2 pt) Ex: Enter a sample text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue! You entered: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and,...