(Usage hints for this presentation)
IT Systems, Summer Term 2025
Dr. Jens Lechtenbörger (License Information)
Hack computer

“Hack Computer” by user52174 under CC BY-SA 4.0; from StackExchange
Black-and-white, 512 x 256 pixels, 1 bit per pixel
Hack uses implicit location “M” for memory access
Convention: M refers to memory word whose address is in A
Example: “D = Memory[42] + 1” (D is useful as data register)
@value
value is a non-negative number (or a symbol)
@42value into A register
@42 writes 42 into A register2 steps as pairs of A-instruction and C-instruction (try out in CPU Emulator)
Assign constant to D register
@42 // Write 42 into A register.
D=A // Copy contents of A register into D register.
Access RAM[42]
@42 // Write 42 into A register.
D=M // M refers to RAM[A], which is RAM[42].
// Thus, copy contents of RAM[42] to D.
Access ROM[42]
@42 // Write 42 into A register.
JMP // JMP loads the PC with the value of the A register.
// Thus, execution continues at ROM[42].
On previous slide, D=A and D=M are C-Instructions
x in {A, D, M}, y in {A, D, M, 1}dest in {null, M, D, MD, A, AM, AD, AMD}
dest = x+y
D=D+A, MD=M+1dest = x-ydest = xdest = 0dest = 1dest = -1Two issues:
If you get stuck, see here for solutions.
dest=comp;jump // comp is mandatory; dest and jump are optional
comp is one of
0,1,-1,D,A,!D,!A,-D,-A,D+1,A+1,D-1,A-1,D+A,D-A,A-D,D&A,D|A,
M, !M, -M, M+1, M-1,D+M,D-M,M-D,D&M,D|M
dest is one of
null, M, D, MD, A, AM, AD, AMD
jump is one of
null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP
if and goto statements (e.g., jump to else part)By convention, use only 2 types of jumps
0;JMP
dest is null, comp is 0
Conditional jumps refer to D register: D;JGT, D;JEQ, …
Thus, dest is null, comp is D
D;JGT jumps if value of D register is Greater Than 0
Similarly for D;JEQ (if EQual to 0); D;JGE (if Greater or
Equal to 0), D;JLT (if Less Than 0), D;JNE (if Not Equal
to 0), D;JLE (if Less than or Equal to 0)
// Write the larger of the two variables x and y to RAM[0].
// Idea: Compute y-x. If result is >=0, then y is larger.
@x // Load location of x to A. Replaced by @16 in CPU Emulator.
D=M // Load value of x to D.
@y // Load location of y to A. Replaced by @17.
D=M-D // Compute y-D (=y-x) to D.
@Y_IS_LARGER // Load Jump target to A. Replaced by @10.
D;JGE // If D is GE (greater or equal to 0), Jump.
// Otherwise, continue with next instruction.
// If we are here, x is larger.
// Load x to D and jump elsewhere for writing.
@x
D=M
@STORE_RESULT // Load Jump target to A. Replaced by @12.
0;JMP // Jump unconditionally.
(Y_IS_LARGER) // Declare Jump target.
// Next instruction (@y) is in ROM[10].
// @Y_IS_LARGER is the address of that instruction.
// If we are here, y is larger. Copy via D to RAM[0].
@y
D=M
(STORE_RESULT) // Declare Jump target.
// Next two instructions write D to RAM[0].
@0
M=D
(END) // Declare Jump target for end of program (ROM[14]).
// By convention, infinite loops end programs in Hack.
@END // Load Jump target to A.
0;JMP // Jump unconditionally to previous A-instruction.
Symbol classes
Predefined
Labels (in UPPER case by convention)
JMP commands(Y_IS_LARGER) and (END) above@END
Variables (in lower case by convention)
x and y above@value@42 → 0000000000101010

Mult.asm as part of Project 4 (ignore Fill.asm)Source files are available on GitLab (check out embedded submodules) under free licenses. Icons of custom controls are by @fontawesome, released under CC BY 4.0.
Except where otherwise noted, the work “Machine and Assembly Language”, © 2024-2025 Jens Lechtenbörger, is published under the Creative Commons license CC BY-SA 4.0.