Write a MIPS function int isPal (char *str) that takes a pointer to a C-string,
ID: 3814836 • Letter: W
Question
Write a MIPS function int isPal (char *str) that takes a pointer to a C-string, str, as a parameter. It should return 1 if the C-string is a palindrome, else it should return 0. A palindrome is a string that reads the same forwards and backwards. For example. is Pal ("radar") should return 1. is Pal ("hannah") should return 1. is Pal ("ccsf") should return 0. is Pal ("rada") should return 0. is Pal() may use functions from util.s, e.g. strlen(). You can assume that parameter str points to a valid, NULL-terminated C-string which only contains lowercase letters You must write your function in C-like code first, then translate it to MIPS assembler. Part of your grade will be how well your assembly code follows your C-like code Write the C-like code and assembly code for isPal () on the next page. Fill in the table provided with your variables and their register or stack assignments.Explanation / Answer
//pls comment
int isPal(char *str)
{
int begin, middle, end, length = 0;
while (str[length] != '')
length++;
end = length - 1;
middle = length/2;
for (begin = 0; begin < middle; begin++)
{
if (str[begin] != str[end])
{
return 0;
break;
}
end--;
}
if (begin == middle)
return 1;
return 0;
}
isPal(char*):
daddiu $sp,$sp,-48
sd $fp,40($sp)
move $fp,$sp
sd $4,8($fp)
sw $0,24($fp)
.L3:
lw $2,24($fp)
ld $3,8($fp)
daddu $2,$3,$2
lb $2,0($2)
beq $2,$0,.L2
nop
lw $2,24($fp)
addiu $2,$2,1
sw $2,24($fp)
b .L3
nop
.L2:
lw $2,24($fp)
addiu $2,$2,-1
sw $2,20($fp)
lw $2,24($fp)
srl $3,$2,31
addu $2,$3,$2
sra $2,$2,1
sw $2,28($fp)
sw $0,16($fp)
.L7:
lw $3,16($fp)
lw $2,28($fp)
slt $2,$3,$2
beq $2,$0,.L4
nop
lw $2,16($fp)
ld $3,8($fp)
daddu $2,$3,$2
lb $2,0($2)
lw $3,20($fp)
ld $4,8($fp)
daddu $3,$4,$3
lb $3,0($3)
beq $2,$3,.L5
nop
move $2,$0
b .L6
nop
.L5:
lw $2,20($fp)
addiu $2,$2,-1
sw $2,20($fp)
lw $2,16($fp)
addiu $2,$2,1
sw $2,16($fp)
b .L7
nop
.L4:
lw $3,16($fp)
lw $2,28($fp)
bne $3,$2,.L8
nop
li $2,1 # 0x1
b .L6
nop
.L8:
move $2,$0
.L6:
move $sp,$fp
ld $fp,40($sp)
daddiu $sp,$sp,48
j $31
nop
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.