Lab 6

| categories: Labs

Due 14 October before lab.

First in C

Exercise 1. ASCII string to binary number (atoi.c)

Write a function atoi that converts a string of up to 8 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. The string is a standard C string, an array of characters terminated by a NULL. 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 will not represent a negative number or too large a number. For example, atoi called with the argument “12345” will return the integer 12345.

Exercise 2. Fibonacci (fib.c)

Write a recursive function fib that computes the Nth Fibonacci number, Fib(N) where Fib(0)=0, Fib(1)=1, and Fib(n)=Fib(n-1)+Fib(n-2). This function takes in a single unsigned 32-bit integer and will return an unsigned 32-bit 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 (unsigned).

    A non-recursive implementation of the function will receive zero credit.

Exercise 3. Put it together. (main.c)

Write a main program that calls atoi with the string "12" and then calls fib with the result. Finally print the value that fib returns.

Now in Assembly

Now do the steps above in assembly language for MIPS. You must manage the stack appropriately by following all the MIPS conventions regarding saved and temporary registers, passing arguments, stack pointer, frame pointer, return values, etc. You should use the template files provided below.

Exercise 4. ASCII string to binary number (atoi.asm)

Translate your atoi.c into atoi.asm in MIPS assembly language that produces the same result.

Exercise 5. Fibonacci (fib.asm)

Translate your fib.c into fib.asm in MIPS assembly language that produces the same result.

Exercise 6. Put it together. (main.asm)

Translate your main.c into main.asm in main.asm and combine the result with your atoi.asm and fib.asm to produce a complete program.

Submission

Include your name and PID in each file. Submit your C and assembly language code along with the output for each part.

Grading

Exercise 3 will be graded first. If it is correct you will get full credit for parts 1 to 3 (30 points). If it is incorrect, Chad will grade parts 1 and 2 (10 points each).

Exercise 6 will be graded next. If it is correct you will get full credit for parts 4 to 6 (70 points). If it is incorrect, Chad will grade parts 3 and 4 (25 points each).