Assignment 6

This is the finale of a three-assignment series.

The goal of these assignments 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 4, you have written a function atoi that converts a string of characters into a 32-bit integer.

int atoi(char str[]);

In assignment 5, 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-42 to A-45. 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. Your program must run in SPIM without error.

last edited 2005-10-04 13:13:13 by GaryBishop