| CPSC 220 | Introduction to Computer Architecture | Fall 2008 |
In this lab, which is due next week, you will debug Larc machine programs. You will need to use your notes from class and the two handouts on Larc: Larc ISA and Larc machine programming.
Create a lab07 directory within the ~/cs220 directory that you created in lab 1 for use in this lab. Change to the newly-created lab07 directory. Copy the provided files from the directory /classes/f08/cs220/labs/lab07 to your new lab07 directory. The following commands should work (note: the "-r" in the cp command allows for copying directories as well as files):
bash$ mkdir ~/cs220/lab07 bash$ cd ~/cs220/lab07/ bash$ cp -r /classes/f08/cs220/labs/lab07/* ~/cs220/lab07/
Note: the rest of this lab assumes you are in your lab07 directory.
The files odd-or-even-broken.out, is-prime-broken.out, sim, and db should have been copied to your working directory. odd-or-even-broken.out and is-prime-broken.out are two Larc machine programs containing errors that you must debug in this lab. sim is a working simulator that will allow you to run Larc machine code programs. To run a Larc program called hello-world.out (which just prints "Hello, world!\n" to the screen) in the simulator, you would do the following:
bash$ ./sim hell-world.out Hello, world! bash$db is a graphical debugger that will allow you to debug Larc machine programs. It will open a graphical window where you will be able to step through your program and view the state (i.e., registers and memory) as it executes. To run a Larc machine program called hello-world.out in the debugger you would do the following:
bash$ ./db hell-world.out & bash$
See your class notes and the Larc machine programming handout for details on how to use the debugger.
There are two exercises for this week's lab due next week.
In this exercise, you will debug a buggy, pre-existing Larc machine code program called odd-or-even-broken.out for determining whether a number is odd or even (similar to the one shown in class). The program contains exactly 5 bugs. Each bug corresponds to an incorrect line in the program that must be fixed. You should not have to remove or add any additional lines to fix the program.
The program is supposed to prompt the user for an integer, read in the integer, and print whether the integer is odd or even. Here are some sample runs of the working version of the program (not the copy that you have):
When the entered number is odd:
bash$ ./sim odd-or-even.out Enter a number: 11 11 is odd. bash$
When the entered number is even:
bash$ ./sim odd-or-even.out Enter a number: 6 6 is even. bash$
Here is the algorithm used to write this program:
Print "Enter a number: "
Read in number (number is put in $1)
Set $2 to $1
Print number
Set $4 to $2 & 1 (determines whether even or odd)
if_statement:
Branch if $4 == 0 to then_clause
else_clause:
Print " is odd.\n"
Branch unconditionally to end_if
then_clause:
Print " is even.\n"
end_if:
Exit program
Although the algorithm is OK, unfortunately, 5 errors resulted when translating this algorithm into machine code. In some cases, the algorithm was not followed explicitly. In other cases, errors occurred when translating the textual representation into a binary represenation. As stated above, all of the errors can be fixed by modifying a single line. You should not have to add or remove any lines (which would result in a significant amount of additional work to update all of the addresses in load immediates and offsets in branches).
Your task is to find those 5 errors and fix them. You should submit a file called odd-or-even-bugs.txt, which describes each bug that you found and how to fix each bug. In adddition, you should submit a file called odd-or-even.out, which contains the working program without any errors.
In this exercise, you will debug another buggy, pre-existing Larc machine code program (one you haven't seen before) called is-prime-broken.out for determining whether a number is prime. The program contains exactly 5 bugs. Each bug corresponds to an incorrect line in the program that must be fixed. You should not have to remove or add any additional lines to the program.
The program is supposed to prompt the user for an integer >= 2, read in the integer, and print whether the integer is prime or not. Here are some sample runs of the working version of the program (not the copy that you have):
When the entered number is not prime:
bash$ ./sim is-prime.out Enter a number: 10 10 is not prime. bash$
When the entered number is prime:
bash$ ./sim is-prime.out Enter a number: 5 5 is prime. bash$
Note: the working version of the program only works correctly when the entered number is greater than or equal to 2. So one bug is not that the program fails for integers less than 2. The program also only works correctly if the user enters an integer. That is also not a bug in the program.
Our technique for determining whether a number, n, is prime is to check whether any number from 2 to n-1 evenly divides n. If so, we know n is not prime, otherwise, we know that n is prime. Here is the detailed algorithm for this program:
Print "Enter a number: "
Read in number (number is put in $1)
Set $4 to $1 ($4 holds entered number)
Set $5 to 2 ($5 is a loop iterator which goes from 2 to $4-1)
Set $6 to 0 ($6 is flag indicating primality -- 0 means prime, 1 means not prime)
loop_start:
Branch if $4 <= $5 to loop_end
Set $7 to $4 / $5 (this instruction and the following two compute $4 % $5)
Set $8 to $7 * $5
Set $7 to $4 - $8 ($7 = $4 % $5 -- if this is 0 then number is not prime)
if_statement:
Branch if $7 == 0 to then_clause
else_clause:
Branch unconditionally to end_if
then_clause:
Set $6 to 1 (set flag to 1 meaning number is not prime)
end_if:
Set $5 to $5+1
Branch unconditionally to loop_start
loop_end:
Set $2 to $1 (put entered number in $2)
Print entered number
Branch if $6 is 0 to is_prime:
not_prime:
Print " is not prime.\n"
Branch unconditionally to end
is_prime:
Print " is prime.\n"
end:
Exit program
Although the algorithm is OK, unfortunately, 5 errors resulted when translating this algorithm into machine code. In some cases, the algorithm was not followed explicitly. In other cases, errors occurred when translating the textual representation into a binary represenation. As stated above, all of the errors can be fixed by modifying a single line. You should not have to add or remove any lines (which would result in a significant amount of additional work to update all of the addresses in load immediates and offsets in branches).
Your task is to find the 5 errors contained in the broken program is-prime-broken.out and to fix those errors. You should submit a file called is-prime-bugs.txt, which describes each bug that you found and how to fix each bug. In adddition, you should submit a file called is-prime.out, which contains the working program without any errors.
Verify that your lab07 folder contains all of the files you created or modified for this lab, then copy your entire lab07 folder to the handin directory ~mcorliss/handin/cs220/username (where username is replaced with your username). For example, if your working directory is ~/cs220/lab07/ then you could do the following:
cp -r ~/cs220/lab07 ~mcorliss/handin/cs220/username
where username is replaced with your particular username (e.g., mcorliss).