Laboratory assignment ยท 27 August

Lab 4: Hack Machine Language

Write and test two Hack assembly programs. When executed on the CPU emulator, your programs should generate the results required by the supplied project test scripts.

Back to course home

Programs

What to submit

Complete both source files with the exact required names and locations.

Program 1

Mult.asm: multiplication

File
projects/04/mult/Mult.asm
Inputs
The current values stored in R0 and R1.
Output
Compute R0 * R1 and store the result in R2.
Assume
R0 >= 0, R1 >= 0, and R0 * R1 < 32768. Your program need not test these conditions.
Test
Run Mult.tst. The output is checked against Mult.cmp on representative values.
Program 2

Fill.asm: keyboard and screen I/O

File
projects/04/fill/Fill.asm
Loop
Run continuously in an infinite loop that listens to the keyboard.
Key pressed
Blacken the screen by writing black in every pixel.
No key
Clear the screen by writing white in every pixel.
Order
You may process pixels in any spatial order, as long as holding or releasing a key long enough eventually gives a fully blackened or cleared screen.
Test
Run Fill.tst. There is no compare file, so inspect the simulated screen visually.

Workflow

Recommended procedure

Write, assemble, test, debug, and repeat until the emulator behavior is correct.

Steps

  1. Go to the assembler tab of the emulator.
  2. Click the "Load File" button. Open the file projects/04/mult/Mult.asm or projects/04/fill/Fill.asm.
  3. Write your assembly code in the editor.
  4. Use the assembler to translate your program. If there are syntax errors, fix the source file and assemble again.
  5. After successful assembly, the assembler will produce Mult.hack or Fill.hack. You can either download the file or use the emulator's "Load in CPU Emulator" button to open it directly.
  6. Use the CPU emulator to test Mult.hack or Fill.hack, either interactively or with the supplied Mult.tst or Fill.tst script.
  7. To load the test script, click the "Load a test script" button and select the appropriate test file.
  8. If there are run-time errors or wrong results, edit Mult.asm or Fill.asm, assemble again, and rerun the emulator test.
  9. Repeat the same process for projects/04/fill/Fill.asm using the projects/04/fill directory.