Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the following C code into MIPS: #include int compare_strings(char [ ], cha

ID: 651318 • Letter: W

Question

Write the following C code into MIPS:

#include
int compare_strings(char [ ], char [ ]);
int main()
{
int flag;
char a[1000], b[1000];
printf("Input first string ");
scanf("%s", &a);
printf("Input second string ");
scanf("%s", &b);
flag = compare_strings(a, b);
if (flag == 0)
printf("Entered strings are equal. ");
else
printf("Entered strings are not equal. ");
return 0;
}
int compare_strings(char a[ ], char b[ ])
{
int c = 0;
while (a[c] == b[c]) {
if (a[c] == '' || b[c] == '')
break;
c++;
}
if (a[c] == '' && b[c] == '')
return 0;
else
return -1;
}

Explanation / Answer

.data
msg1:.asciiz "Please insert text (max 20 characters): "
msg2:.asciiz " NOT SAME STRING"
msg3:.asciiz " SAME STRING"
str1: .space 25
str2: .space 25
.text
.globl main
main:
addi $v0,4
la $a0,mseg1
syscall
li $v0,8
la $a0,str1
addi $a1,$zero,20
syscall #got the string 1
li $v0,4
la $a0,mseg1
syscall
li $v0,8
la $a0,str2
addi $a1,$zero,20
syscall #got the string 2

la $a0,str1 #passes the address of str1
la $a1,str2 #passes the address of str2
jal strcmp #calling strcmp

beq $v0,$zero,ok #check out the result
li $v0,4
la $a0,mseg2
syscall
j exit
ok:
li $v0,4
la $a0,mseg3
syscall
exit:
li $v0,10
syscall

strcmp:
add $t0,$zero,$zero
add $t1,$zero,$a0
add $t2,$zero,$a1
loop:
lb $t3($t1) #load byte from every string
lb $t4($t2)
beqz $t3,checkt2 #str1 end
beqz $t4,missmatch
slt $t5,$t3,$t4 #compare the two bytes
bnez $t5,missmatch
addi $t1,$t1,1 #t1 points to next byte of str1
addi $t2,$t2,1
j loop

missmatch:
addi $v0,$zero,1
j endfunction
checkt2:
bnez $t4,missmatch
add $v0,$zero,$zero

endfunction:
jr $ra