Objective: Write MIPS program to check if an input string is a palindrome. A pal
ID: 3916976 • Letter: O
Question
Objective: Write MIPS program to check if an input string is a palindrome. A palindrome reads the same forwards and backwards, like: radar, racecar, noon, anna. Assume one continuous word, no space or punctuation.
Implement the following program in MIPS:
In the main program loop:
get a string from the user (use syscalls 4 and 8, not syscall 54)
call a function to check if the input string length is zero, in which case, exit the program
call a function to check if the string is a palindrome and print a message
Explanation / Answer
MIPS PROGRAM:-
.data
stringEnterText: .asciiz " Enter a string :"
IS_STRING: .asciiz " is a PALINDROME"
NOT_STRING: .asciiz " is not PALINDROME"
input: .space 40
.text
.globl main
main:
li $v0,4
la $a0,stringEnterText
syscall
li $v0, 8
la $a0, userInput
li $a1, 40
syscall
la $a0,input
jal string_count
move $a0,$v0 #load length
la $a1, input
jal isPalindrome # Do the Palindrome check
add $a0, $v0, $zero
jal printRes # Print
addi $v0, $zero, 10
syscall # Exit
string_count:
move $t0,$a0
li $t1,0
li $t5,0xa
charac_count:
lb $t2,0($t0)
beq $t2,$t5,exit_loop_count
addi $t0,$t0,1
addi $t1,$t1,1
j charac_count
exit_loop_count:
move $v0,$t1
jr $ra
isPalindrome:
# Check base case
slti $t0, $a0, 2
bne $t0, $zero, returnTrue
# Make sure first and last are equal
lb $t0, 0($a1)
addi $t1, $a0, -1
add $t1, $t1, $a1
lb $t1, 0($t1)
bne $t0, $t1, returnFalse
# Shift pointer, length, recurse
addi $a0, $a0, -2
addi $a1, $a1, 1
j isPalindrome
returnFalse:
addi $v0, $zero, 4
la $a0, input
syscall
addi $v0, $zero, 4
la $a0, NOT_STRING
syscall
jr $ra
returnTrue:
addi $v0, $zero, 4
la $a0, input
syscall
addi $v0, $zero, 4
la $a0, IS_STRING
syscall
jr $ra
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.