Project 04
Project 04
CS 220
Background
Each hardware platform is designed to execute a certain machine language, expressed using agreed-
upon binary codes. Writing programs directly in binary code is a possible, yet an unnecessary,
tedium. Instead, we can write such programs in a low-level symbolic language, called assembly,
and have them translated into binary code by a program called an assembler. In this project you
will write some low-level assembly programs, and will be forever thankful for high-level languages
like C and Java. (Actually, assembly programming can be a lot of fun, if you are in the right
mood; it’s an excellent brain teaser, and it allows you to control the underlying machine directly
and completely.)
Objective
To get a taste of low-level programming in machine language, and to get acquainted with the
Hack computer platform. In the process of working on this project, you will become familiar with
the assembly process — translating from symbolic language to machine-language — and you will
appreciate visually how native binary code executes on the target hardware platform. These lessons
will be learned in the context of writing and testing the two low-level programs described below.
Contract
Write and test the two programs described above. When executed on the CPU Emulator, your
programs should generate the results mandated by the specified tests.
Resources
The Hack assembly language is described in detail in Chapter 4. You will need two tools: the
supplied Assembler — a program that translates programs written in the Hack assembly language
into binary Hack code, and the supplied CPU Emulator — a program that runs binary Hack code
on a simulated Hack platform. Two other related and useful resources are the supplied Assembler
Tutorial and the CPU Emulator Tutorial. We recommend going through these tutorials before
starting to work on this project. The project files are available in a ZIP archive file available on
the course web site.
1
Programs
Mult Pseudo-Code
See pp. 249–250 of the textbook for a binary multiplication example. In the example on pg. 250, x
is the multiplicand and y is the multiplier.
2
Fill Pseudo-Code
sptr = SCREEN; // Memory pointer to next screen memory location
// to be filled with black or white pixels.
clear = -1; // This is for the 10 "smooth user experience" points.
// See implementation tip in Proposed Implementation
// Section below. "-1" is true in Hack; "0" is false.
while (1)
{
if (*KBD != 0) // Read the keyboard location’s value. if
{ // !0, fill with black pixels.
if (clear != 0) // For "smooth user experience."
{
clear = false;
sptr = SCREEN;
}
Proposed Implementation
1. Use a plain text editor to write the first assembly program. You can do it by loading and
editing the supplied mult/Mult.asm file.
2. Use the supplied Assembler (in either batch or interactive mode) to translate your program. If
you get syntax errors, go to step 1. If there are no syntax errors, the assembler will produce
a file called mult/Mult.hack, containing binary instructions written in the Hack machine
language.
3. Use the supplied CPU Emulator to load, and then test, the translated Mult.hack code. This
3
can be done either interactively, or batch-style using the supplied Mult.tst script. If you get
run-time errors, go to step 1.
4. Repeat steps 1–3 for the second program (Fill.asm), working in the fill directory. Imple-
mentation tip: If you’re struggling with the Fill.asm program’s “smooth user experience”
logic, implement it first without this logic and get that working. Then, work on adding this
logic. At the worst, you’ll lose 10 points for not having this logic working.
Debugging tip: The Hack language is case-sensitive. A common error occurs when one writes,
say, @foo and @Foo in different parts of the program, thinking that both labels are treated as
the same symbol. In fact, the assembler treats them as two different symbols. This is a nasty,
difficult-to-detect bug.
Tools
The supplied Hack Assembler can be used in either command mode (from the command shell) or
interactively. The latter mode of operation allows observing the translation process in a visual and
step-wise fashion, as shown below:
The machine language programs produced by the assembler can be tested using the supplied
CPU Emulator. This CPU Emulator includes a ROM (also called Instruction Memory) representa-
tion, into which the binary code is loaded, and a RAM representation, which holds data. For ease
of use, the emulator enables the user to view the loaded ROM-resident code in either binary mode,
or in symbolic / assembly mode. In fact, the CPU Emulator even allows loading a program written
in assembly directly into the ROM, in which case the program is translated into binary code on the
fly. This utility seems to render the supplied assembler unnecessary, but this is not the case. First,
4
the supplied assembler shows the translation process visually, for instructive purposes. Second, the
assembler generates a persistent binary file. This file can be executed either on the CPU Emulator,
as we illustrate below, or directly on the hardware platform, as we’ll do in the next project.
1. A README file containing the names of all group members. This file may also contain other
information, as described above.
3. Nothing else.
5
Project 4: Machine Language
Grading method: As usual with programming assignments, we look for elegance, clarity, and
sensible documentation.
Mult / 25 / 10
Total 70 30