Lab 8: String Processing and Recursion


Exercise 1: Parsing an ASCII string to binary number

Write a function a_to_i that converts a string of ASCII digits into a 32-bit integer. The function will receive as an argument the starting address of the string and must return a 32-bit integer containing the integer value of the string. Assume that the string is an ASCIIZ string, i.e., ends with the null character (ASCII code 0). You don't need to check for errors in the string, i.e., you may assume the string contains only characters '0' through '9' (i.e., their corresponding ASCII codes), and will not represent a negative number or a non-decimal value or too large a number. For example, a_to_i called with the argument "12345" will return the integer 12345.

First write a C program (ex1.c), where main() repeatedly reads a string from the input using fgets(), then calls a_to_i() with this string as an argument. The function a_to_i() returns an integer, which main prints using printf("%d\n",...). The program should terminate when the integer read is 0.

Next, write the equivalent MIPS assembly program (ex1.asm), that does the same. That is, your main procedure must repeatedly read a string from the input using syscall 8, then call procedure a_to_i with the string just read as an argument. The function a_to_i returns an integer, which main prints using syscall 1. The program should terminate when the integer read is 0.

Write and test your C code (ex1.c) first, then write and test your assembly code (ex1.asm) using your own inputs, as well as sample input files provided.

Exercise 2: Computing the factorial of a number

In this exercise, you are to write a recursive implementation of a function factorial() that computes the factorial of N, i.e., N!, using the following definition:

This function takes in a single integer and will return an integer result. You can assume that the function will be called only with an argument small enough so that the result does not overflow, i.e., fits within 32 bits. A non-recursive implementation of the function will receive zero credit.

First implement this function in C. Keep in mind the following:

Add this function to your code from Exercise 1, and call the file ex2.c. Your program should do the following:

Name the file with your C code ex2.c. Test it using your own inputs, and also the sample inputs provided.

Once your C code is working correctly, write the equivalent MIPS assembly program (ex2.asm), that does the same. That is, your main procedure must repeatedly read a string from the input, then call procedure a_to_i to convert it to an integer, then call factorial to compute the factorial, and finally use syscall 1 to print the result. The program should terminate when the integer read is 0, but after computing and printing its factorial value (=1).

Name the file with your assembly code ex2.asm. Test it using your own inputs, and also the sample inputs provided.


12 October 2012, Montek Singh, montek@cs.unc.edu