Assigned 22 February, 2001
Due 8 March, 2001.
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 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 in SPIM, 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.
Submit your answers via EMAIL to chunfa@email.unc.edu by the beginning of class on the due date. Put HW7 in the subject line of your mail. Please make sure your email is in plain text format.
February 22, 2001 16:10