Write a complete MIPS program that implements the algorithm shown below (in C). To verify the output of the MIPS program, copy this C code and execute it.
1)
int main()
{
int Z=2;
int i;
i=0;
while(1){
if(i>20)
break;
Z++;
i+=2;
}
do {
Z++;
} while (Z<100);
while(i > 0) {
Z--;
i--;
}
return 0;
}
I and Z must be integer words in memory.
2)
int main()
{
int A[5]; // Empty memory region for 5 elements
int B[5] = {1,2,3,4,5};
int C=12;
int i;
for(i=0; i<5; i++)
{
A[i] = B[i] + C;
}
i--;
while(i >= 0)
{
A[i]=A[i]*2;
i--;
}
}
A and B must be arrays of integer words in memory, but i can be a register.
3)
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");
}
The array of characters is an array of bytes, not words! The result pointer must be stored in memory when the program finishes.