CS341 Spring '08
Lab Assignments
Lab 8 (April 25):
Today we discussed basic threading. Below is an example of a race condition
causing unexpected behavior. Your assignment is to modify my thread.c file
to use a
To build this file using gcc, go to the command prompt and type
gcc -l pthread thread.c
A more in-depth coverage of the pthread library and its use can be found here
Lab 7 (March 28):
Today we introduced floating point operations. They should be pretty straightforward, as you've all done arithmetic before. The important thing to remember is that floating point ops use a different set of registers than integer operations.
Here is a list of floating point instructions.
Excercise: write a function, mycos, that implements the cosine function
using the Taylor series
expansion. The Taylor series for the cosine function is as follows:

For simplicity, you may make the number of terms, n, a constant.
As per usual, submit using the turnin script: cs341.lab7
Lab 6 (March 6):
Excercise: Write a function, toUpper(), that changes all lowercase letters in a string to uppercase while leaving everything else unchanged. Remember that the alphabet in ASCII is (in decimal) 65-90 for Uppercase characters and 97-122 for lowercase letters. $a0 should have a pointer to the beginning of the string.
The turnin for this assignment is cs341.lab6
Lab 5 (February 29):
Excercises for this week:
1) Implement a subroutine, fib, which calculates the nth fibonacci number, where the Fibonacci sequence is defined as the sequence (1 1 2 3 5 8 13...), that is, fib(n) = fib(n-1) + fib(n-2). You may use any method you like for calculating the value.
2) Write a program that repeatedly prompts the user to input an integer and prints the sum of all the numbers input so far. Be sure to include a way to exit out of the loop, e.g. if the user inputs 0.
3) Write a function to calculate a hash function on a string. You can create the hash any way you want: a simple way is to sum the values of all the characters in the string (remember that even characters are numbers when they're being stored!)
Turn in your code using the turnin script. The assignment is cs341.lab5.
Lab 4 (February 22):
Today we talked about branches, and how to use them to mimic common control structures such as if and while statements. As an example, I've implemented the answer to HW1 Problem 3 in MIPS assembly here.
Write a MIPS program, fact.asm, and implement the factorial function. You should write two functions, fact_r and fact_i which implement the factorial function recursively and iteratively respectively. You should also have a main function to test your factorial functions. Make sure your function uses the $a0 register for its parameter and saves the value in $v0 upon returning. Also be sure to save all saved temporaries ($s*) used in your fact function.
In case you've forgotten, the factorial function n! is the product of all
integers from 1 to n; mathematically:
Lab 3 (Feburary 15):
Today we are discussing writing our own functions in MIPS. Functions are accomplished with the use of the jal (jump and link) instruction, which jumps to the given label and saves the return address in the $ra register.
Write a simple function that takes two arguments (use registers $a0 and $a1) and returns a+b in $v0 and a*b in $v1. Be sure to save all the $s registers you modify. You should also write a main function to test your function and print out the solution.
Submit your file using the turnin script as detailed in the previous lab. Use the turnin cs341.lab3.
Lab 2 (February 8):
In today's lab we've discussed using system calls to do input and output. You should be familiar enough with MIPS to write your own programs from scratch now. Remember to properly save all necessary variables and return properly from main. (For this assignment it should be sufficient to store only the $ra register.
Write a MIPS program, io.asm, that prompts the user to input two numbers, a and b, and displays their sum. Be sure to display strings such as "Input a:" and "The sum is equal to x." I don't care about the exact wording, just that you display something to let the user know what's going on.
When you're finished, submit your file using the turnin script, like this:
turnin cs341.lab2 io.asm
Lab 1 (February 1):
Modify the following assembly file to perform a variety of arithmetic operations. Replace "# Your code here" with your own code. The subsequent code will print the value of the register $s2.
Links
- SPIM MIPS emulator: http://pages.cs.wisc.edu/~larus/spim.html