title: Lab 6: Assembly language [TOC] ### Task 0: Review assembly language concepts 1. What's the point of programming in assembly language? 2. What's the difference between registers and memory? 3. What instructions can be used on the Hmmm? There are 24 different instructions in the Hmmm assembly language. The complete list is available [here][instructions]. __Instructions for Representing Functions__ The following instructions are used when we need to represent functions in assembly. Use the `calln` instruction to store the return address and jump to the start of the function, and use `jumpr` to return using the previously-stored return address.
| assembly | description | alias | |--------------|---------------------------------------------|-------| | `calln rX N` | Copy next address to `rX` and jump to `N` | `call`| | `jumpr rX` | Jump to address stored in `rX` | `jump`|
*Question*: what is the difference between `jumpr` and the unconditional jump instruction `jumpn`? __Instructions for Interacting with Memory__ The following instructions are used to store and load data from memory, in a region that we will refer to as the "stack." This is useful when you have many local variables (more local variables than the number of registers), or when you make many function calls. Therefore, they are especially needed for recursion.
| assembly | description | alias | |---------------|----------------------------------------------------|-------| | `storer rX rY`| Store contents of `rX` into mem. addr. held in `rY`| `store`| | `loadr rX rY` | Load `rX` with data from mem. addr. held in `rY` | `load`|
### Task 1: Set-up Hmmm and run some sample programs For this week's homework, you will need to use several Python files for running the Hmmm and some template files for completing the exercises. Let's follow the instructions in the [Preparing for the Remaining Problems] [preparing] section of Problem Set 6 to set up the environment and run an example program. If you write a Hmmm program that contains errors, the assembler will fail when trying to assemble the file. Introduce the following errors in the `example1.txt` program: * 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`. ### Task 2: Fix a buggy Hmmm program In the `ps6` folder, we have included a file called `lab6task2.txt`. Open the file in a [text editor](../problem_sets/plain_text.html). 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 However, the program currently has some bugs that you will need to fix. 1. Start by assembling and running the existing program that we have given you, following the [instructions from Problem Set 6][preparing]. 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! ### 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. Find the appropriate lab section on the main page and click *Upload files*. 3. 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. 4. Click the *Upload* button at the bottom of the page. 5. 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. 6. 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.*** [instructions]: http://www.cs.hmc.edu/~cs5grad/cs5/hmmm/documentation/documentation.html [preparing]: ../problem_sets/ps6.html#preparing-for-the-remaining-problems [ps6]: ../problem_sets/ps6.html