Assigned: 23 September 2004

Due: 7 October 2004

This is the finale of a three-assignment series.

The goal of assignments 5–7 is to add two numbers to produce a third number. The complication is that the two numbers to be added are each represented by a string of up to 8 ASCII characters for the decimal digits and the result must be represented as a string of up to 8 decimal digits as well. This will allow you to get some experience with the representation of numbers.

In assignment 5, you have written a function “atoi” that converts a string of characters into a 32-bit integer.

int atoi(char str[]);

In assignment 6, you have written a function “num2str” that converts a 32 bit integer into a null-terminated ASCII string of characters.

void num2str(int num, char str[]);

Now write a main program which uses atoi to convert each input string into a number, adds them using MIPS add instruction and then converts the result back to a string using num2str. Display the result using the syscall 4 described in the textbook pages A-48 to A-49. Please declare the two input strings as num1 and num2 exactly in the .data section to help the grading. (Hint: Use .asciiz instead of .ascii will null-terminate the string automatically.)

char num1[] = "1234";
char num2[] = "87654321";
char numres[10];
main() {
int n1 = atoi(num1);
int n2 = atoi(num2);
int nr = n1 + n2;
num2str(nr, numres);
syscall(4, numres);
}

Your program must include the atoi function, the num2str function, and the main program. *You may use different versions of atoi or num2str function from those you have submitted*. Your program must run in SPIM without error.

23 September 2004