CS 111
Spring 2018

Old version

This is the CS 111 site as it appeared on May 10, 2018.

Lab 6: Assembly language

FLR 267

If your lab meets in FLR 267, you should begin by following the instructions found here.

Overview

This week’s homework asks you to write several assembly-language programs for a simple virtual machine called Hmmm.

There are 24 different instructions in the Hmmm assembly language. The complete list is available here.

Task 1: Set-up Hmmm and run some sample programs

In order to complete the assignment, you will need to use several Python files for running the Hmmm and some template files for completing the exercises.

  1. Let’s follow the instructions in the Preparing for the Remaining Problems section of Part II of Problem Set 5 to set up the environment and run an example program.

  2. If you write a Hmmm program that contains syntax errors – errors that violate the rules of the language – the assembler will fail when trying to assemble the file. To see what this looks like, go ahead and introduce the following errors in the example1.txt program from ps5partII.zip (the program that we just assembled and ran together):

    • In memory location 02, change mul to mult and observe the OPERATION ERROR that results when trying to run assemble.

    • In memory location 01, add an extra register so that the line is read r2 r3 and observe the ARGUMENT ERROR that results when trying to run assemble.

    If you see a message like this in your own work, you’ll need to fix the error and reassemble the program before you try to run it again.

Task 2: Fix a buggy Hmmm program

Download the following file: lab6task2.txt

Move it into your ps5partII folder, and open the file in a text editor.

This program is supposed to take two integers as inputs, and to count upward from the first input to the second input. For example:

Enter number: 2
Enter number: 5
2
3
4
5

The current version of the program assembles and runs (because it doesn’t have any syntax errors), but it does have some logic errors that keep it from doing what it’s supposed to do. Your task is to fix those logic errors.

  1. Start by assembling and running the existing program that we have given you, following the instructions from Problem Set 5.

    Enter two integers, with the first integer smaller than the second (e.g., 2 and 5). Instead of seeing the desired output, you should see this instead:

    Enter number: 2
    Enter number: 5
    

    with no values printed after the numbers are entered.

  2. The program includes a loop that is supposed to count upward, but currently the loop is stopping too early – before it can write any numbers.

    1. Which lines make up the loop? (Hint: Look for the use of jump statements – one that causes the program to go back to an earlier line in the program, and one that causes the program to break out of the loop and go to a line that comes after the loop.)

    2. Currently, the program breaks out of the loop before it can write anything. You should be able to fix this by changing a single line of code. Do so and re-run your program. Keep trying until you start seeing numbers being printed. For example, you may see this:

      Enter number: 2
      Enter number: 5
      2
      3
      4
      
    3. Note that we are still not getting the final number to be printed. Make whatever changes are needed to fix this, but do not change lines 02-04 (the three nop statements).

      Notes:

      • There are multiple ways to fix things, but you will probably need to either rearrange the existing lines of code or add one or more new lines of code.

      • Remember to leave lines 02-04 alone for now.

      • If you move or add lines, make sure to renumber things as needed.

      • Feel free to ask the course staff for help if you get stuck!

Important notes:

  • Make sure to follow the style guidelines that we’ve specified. In particular, you must have a comment on every line except for nop and halt statements.

  • Make sure that your comments do not include any special characters (e.g., $, quotes, or &).

Task 3: Modify the previous program to compute a sum

Modify the program from the previous task so that in addition to writing the numbers from the first input to the second input, it also computes and writes their sum. For example:

Enter number: 2
Enter number: 5
2
3
4
5
14

Note that the program’s final output is 14, which is the sum of the integers from 2 to 5.

Here again, you should not change lines 02-04 for this task.

Task 4: Modify the previous program to use a function

If you have time, create another version of your program that prints the numbers and computes their sum using a function call.

Begin by making a copy of your lab6task2.txt file, using File->Save As or an equivalent option to save the file using the name lab6task4.txt.

Next, replace the nop lines (lines 02-04) with lines that do the following:

  1. Make a function call to the portion of your program that performs the computations. Use r14 as the return-address register.

  2. Print the return value of the function (the sum of the numbers).

    Don’t forget that there is no such thing as a local variable in assembly, and thus you can find the return value in whatever register is used to store the sum.

  3. Halt the program.

Finally, modify the remainder of the program (lines 05 and following) so that it serves as a separate function that prints the integers from r1 to r2 and computes (but does not print) the sum of those integers.

You will need to:

  1. Remove the line that printed the sum in the previous version of program (since the sum is now being printed after we return from the function call).

  2. Add a line that uses jumpr to return from the function.

Once you have completed the changes, run the program again to ensure that it works.

Task 5: Submit your work

You should use Apollo to submit your lab6task2.txt file and (if you had time) your lab6task4.txt file.

Don’t worry if you didn’t finish all of the tasks. You should just submit whatever work you were able to complete during lab.

Here are the steps:

  1. Login to Apollo, using the link in the left-hand navigation bar. You will need to use your Kerberos user name and password.
  2. Check to see that your BU username is at the top of the Apollo page. If it isn’t, click the Log out button and login again.
  3. Find the appropriate lab section on the main page and click Upload files.
  4. For each file that you want to submit, find the matching upload section for the file. Make sure that you use the right section for each file. You may upload any number of files at a time.
  5. Click the Upload button at the bottom of the page.
  6. Review the upload results. If Apollo reports any issues, return to the upload page by clicking the link at the top of the results page, and try the upload again, per Apollo’s advice.
  7. Once all of your files have been successfully uploaded, return to the upload page by clicking the link at the top of the results page. The upload page will show you when you uploaded each file, and it will give you a way to view or download the uploaded file. Click on the link for each file so that you can ensure that you submitted the correct file.