Write a complete MIPS program that implements the same algorithm shown below (in C). To verify the output of the MIPS program, copy this C code and execute it.
So I need to convert this C code to MIP code
This is the C code,
int main()
{
char string[256];
int i=0;
char *result = NULL; // NULL pointer is binary zero
// Obtain string from user, e.g. "Constantinople"
scanf("%255s", string);
// Search string for letter 'e'.
// Result is pointer to first e (if it exists)
// or NULL pointer if it does not exist
while(string[i] != '\0') {
if(string[i] == 'e') {
result = &string[i];
break; // exit from while loop early
}
i++;
}
if(result != NULL) {
printf("First match at address %d\n", result);
printf("The matching character is %c\n", *result);
}
else
printf("No match found\n");
}
And this is my Mips code, I am not sure how to get the input, compare and print the result
.globl main
.text
main:
#$s0 : base address string
#$s1 : i
#$s2 : *result
la $s0, string
addi $s1, $zero, 0
addi $s2, $zero, 0 #*result = null
#get input from user
li $v0 ,8 # take input
la $a0, string # load byte space into address
li $a1, 256 #allot the byte space for string
move $t0, $a0
syscall
addi $t9, $s0, $s1 # &string[i]
lb $t8,0($t9) #t8 = string [i]
while: beq $t8 , 0, outsidewhile # if string[i] == '\0'
beq $t8, 101, body # if string i == 'e'
add $s1, $s1,1 #i++
j while
body:
add $t2, $t9, $zero #result = &string[i];
j outsidewhile
outsidewhile :
beq $s2, 0, printaddress # if(result != NULL)
# printf("No match found\n");
li $v0,4
la $a0, msg2
li $v0,4
la $a0, newline
printaddress:
#printf("First match at address %d\n", result);
#printf("The matching character is %c\n", *result);
li $v0,4
la $a0, msg
li $v0, 10
syscall
.data
string: .space 256
msg: .asciiz "First match at address "
msg1: .asciiz "The matching character is "
msg2: .asciiz "No match found"
endstring: .asciiz "\0"
newline: .asciiz "\n"