COMP 411: Lab 8: String Processing, Procedure Calls and Recursion in Assembly

Due Wednesday, March 21, 2018, 11:59 PM

Exercise 1: Parsing an ASCII Hexadecimal string to binary number

Write a function h_to_i that converts a string of ASCII hexits (hexadecimal digits) into a 32-bit integer. The function will receive as an argument the starting address of the string (excluding the customary "0x") 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' and 'a' though 'f' (i.e., their corresponding ASCII codes), and will not represent a negative number or a non-decimal value or too large a number. You can further assume only lowercase letters will be present. For example, h_to_i called with the argument "dada" will return the integer 56026.

Starter files are provided: ex1.c and ex1.asm.

First write a C program (ex1.c), where main() repeatedly reads a string from the input using fgets(), then calls h_to_i() with this string as an argument. The function h_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 h_to_i with the string just read as an argument. The function h_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. For this assignment, all your assembly code must be in the same file (ex1.asm), with the code for main at the top, and the code for h_to_i pasted below it. We will learn how to work with multiple source files in the next lab assignment.

Exercise 2: Computing NchooseK of a number iteratively

In this exercise, you are to write a non-recursive (i.e., simple iterative) implementation of a function NchooseK() that computes NchooseK of two input numbers n and k (i.e., the value of the binomial coefficient nCk). You computed this in C recursively in Lab 3.

Here, we will first compute this using an iterative method, using the Multiplicative Formula: `((n), (k)) = prod_(i=1)^k (n+1-i)/i`

This function takes in two integers, each on a separate line, 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. Please do not implement using recursion; that will be the topic of the next exercise!

Starter files are provided: ex2.c and ex2.asm.

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

Copy in your code from Exercise 1, so that you can use h_to_i. 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 using syscall 8, then call procedure h_to_i to convert each input to an integer, then call NchooseK to compute NchooseK, and finally use syscall 1 to print the result. The program should terminate when the first integer read is "0".

Once again, all your assembly code must be within one file; name it ex2.asm, with the code for main on top. Test it using your own inputs, and also the sample inputs provided.


Exercise 3: Computing NchooseK recursively

C version

In this exercise, you are to write a recursive implementation of NchooseK(), similar to lab 3. You should use the following definition:

        NchooseK(n, 0) = 1
        NchooseK(n, n) = 1
        NchooseK(n, k) = NchooseK(n-1, k-1) + NchooseK(n-1, k)

This function takes in two integers and will return an integer result. All other assumptions Exercise 2 still hold, except that the implementation of the NchooseK function should be recursive. A non-recursive implementation of the function will receive zero credit.

First implement this function in C (or copy your implementation from lab3). Keep the rest of the program from Exercise 2 as is. That is:

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

Assembly version

Once your C code is working correctly, write the equivalent MIPS assembly program (ex3.asm), that does the same. Since you are now implementing a procedure that is recursive, you will need to appropriately manage the stack. That is the main challenge of this exercise.

Templates: Assembly coding templates have been provided for the main procedure (main_template4b.asm) and for other procedures (proc_template4b.asm). To simplify the assembly coding, these templates assume the following: (i) no procedure call needs more than 4 arguments; (ii) any local variables are placed in registers and not created on the stack; (iii) no "temporary" registers need to be protected from a subsequent call to another procedure. These templates correspond to Example 4b on Slide 37 of Lecture 9 (Procedure and Stacks).

Assembly coding templates corresponding to Example 5a on Slide 38 of the lecture are also available (main_template5a.asm and proc_template5a.asm)). The only difference here is that these templates also allow you the ability to save "temporaries" on the stack when they need to be protected from subsequent procedure calls.

Our recommendation is to construct your assembly code in such a manner that the simpler "4b" templates can be used. In particular, if, say, proc1 calls proc2, and proc1 has some important values in temporary registers that it must protect from proc2, then proc1 should copy these important values to "saved" registers (e.g., $s0, $s1, etc.), because it is guaranteed that these values will be the same upon return from proc2. However, if you find the style of the "5a" templates more to your liking, you may cerainly use those.

Factorial Example: As an example of how to use these templates to write a recursive procedure, assembly code is provided for calculating factorial in a recursive fashion, using the "4b" templates. Please carefully study the code for the main procedure (fact_main4b.asm), as well as the recursive factorial procedure (fact_proc4b.asm). The two procedures have been concatenated together into a single assembly file (fact4b.asm); open this file in Mars and run it step-by-step to follow the execution of the recursive procedure. Use these files as guidance to develop your code for this exercise.

You may develop your code for each procedure in a separate file using the templates provided, but for this lab, all your final assembly code must be within one file; simply copy-and-paste your procedures below main.

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


Test Inputs, Due Date and Submission Procedure

Sample inputs and corresponding outputs are provided under /home/porter/comp411/samples/lab8. You should try running your program on the sample input files provided, and make sure the program's output is identical to that in the sample output files.

Your assignment must be submitted electronically by 11:59pm on Wednesday, March 21.

First copy the files ex1.c, ex1.asm, ex2.c, ex2.asm, ex3.c and ex3.asm to the CS Dept server (classroom.cs.unc.edu) under the appropriate folder in your home directory (e.g., comp411lab/lab8). Then, run the self-checking and submit scripts:

% cd ~/comp411lab/lab8
% /home/porter/comp411/bin/selfchecklab8
% /home/porter/comp411/bin/submitlab8

In case of any problems, please contact the instructor or the TAs.


3 November 2017, Montek Singh, montek@cs.unc.edu
26 February 2018, Don Porter, porter@cs.unc.edu
2 March 2018, Montek Singh, montek@cs.unc.edu

Last updated: 2018-03-03 19:26:46 -0500 [validate xhtml]