Write a MIPS function (called “palindrome”) that returns 1 if the input string i
ID: 3668235 • Letter: W
Question
Write a MIPS function (called “palindrome”) that returns 1 if the input string is a palindrome and returns 0 otherwise. The address of the string will be passed in $a0 and the value should be returned in $v0. Here a string is a palindrome if it reads the same in forward or backward direction (including white spaces, punctuation marks, and so on, but is case insensitive (i.e., ‘A’ and ‘a’ are considered to be the same)). Note the string is ended with ‘’ (C/C++ convention).
Thank you for assistance :)
Explanation / Answer
sub $t5, $t0, $t2 #subtract counter from the string length
add $t6, $t5, $t1 #add index from the end of the string to start address
lb $t7, 0($t6) #get corresponding character from the end of the string
beq $t4, $t7, palindrom_continue #check to determine are the characters exact match
li $v0, 0 #if not return 0, immediately
j palindrom_exit
palindrom_continue:
addi $a0, $a0, 1 #shift pointer to string one space right
addi $t2, $t2, 1 #increment counter by one
j palindrom_loop
palindrom_exit:
# ------------
# returns:
# TRUE (1) or FALSE (0)
# ------------
lw $ra 0($sp) #load return address
addi $sp, $sp, 8 #free stack
jr $ra #return
main:
#entry point
la $a0, str #load string
jal palindrom #call palindrom
move $a0, $v0 #set a0 to palindrom return value
li $v0, 1 #set 1 to v0 - as this is system call for print int
syscall #make the call
li $v0, 10 #set 10 to v0 - as this is system call for exit program
syscall #make the call
.data
str: .asciiz "thiispalindrome_emordnilapsiiht"
NOTE -:
The approach is to get character from beginning and the corresponding character from the end of the string. If they are exact match, continue execution by the time when counter reaches half of the string length. If any pair of characters are found different, stop execution immediately and return FALSE.
One more thing, I haven't implemented part where upper case letters are not different from the lower case letters. But it isn't hard. You can add it yourself. The idea is to test each loaded character if it's ascii is between A (65) and Z (90). If so you'll add 32 to ascii value and you'll get lower case letter
AND HERE IS CODE
#include <stdio.h>
int is_palindrome(char*);
void copy_string(char*, char*);
void reverse_string(char*);
int string_length(char*);
int compare_string(char*, char*);
int main()
{
char string[100];
int result;
printf("Input a string ");
gets(string);
result = is_palindrome(string);
if ( result == 1 )
printf(""%s" is a palindrome string. ", string);
else
printf(""%s" is not a palindrome string. ", string);
return 0;
}
int is_palindrome(char *string)
{
int check, length;
char *reverse;
length = string_length(string);
reverse = (char*)malloc(length+1);
copy_string(reverse, string);
reverse_string(reverse);
check = compare_string(string, reverse);
free(reverse);
if ( check == 0 )
return 1;
else
return 0;
}
int string_length(char *string)
{
int length = 0;
while(*string)
{
length++;
string++;
}
return length;
}
void copy_string(char *target, char *source)
{
while(*source)
{
*target = *source;
source++;
target++;
}
*target = '';
}
void reverse_string(char *string)
{
int length, c;
char *begin, *end, temp;
length = string_length(string);
begin = string;
end = string;
for ( c = 0 ; c < ( length - 1 ) ; c++ )
end++;
for ( c = 0 ; c < length/2 ; c++ )
{
temp = *end;
*end = *begin;
*begin = temp;
begin++;
end--;
}
}
int compare_string(char *first, char *second)
{
while(*first==*second)
{
if ( *first == '' || *second == '' )
break;
first++;
second++;
}
if( *first == '' && *second == '' )
return 0;
else
return -1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.