← SEARCHABLE ARCHIVE
Nand2Tetris · 2026-05-18

Nand2Tetris — Chapter 4

Hack Machine Language

INTERACTIVE READING MODE →

// Mult.asm
//Input: RAM[0]=R0, RAM[1]=R1
//Output: RAM[2]=R0 * R1
//Action: Repeat R1 times and keep adding R0.

    @R2
    M=0

    @i
    M=0

(LOOP)
    @i
    D=M
    @R1
    D=D-M
    @END
    D;JGE

    @R0
    D=M
    @R2
    M=M+D

    @i
    M=M+1

    @LOOP
    0;JMP

(END)
    @END
    0;JMP
// Fill.asm
//Input: Check if keyboard was pressed (KBD)
//Output: Fills the entire screen with black when a key is pressed, otherwise white.
//Operation: Write value while repeating 8192 words from SCREEN

(LOOP)
    @KBD
    D=M
    @BLACK
    D;JNE

    @color
    M=0
    @DRAW
    0;JMP

(BLACK)
    @color
    M=-1

(DRAW)
    @SCREEN
    D=A
    @addr
    M=D

    @8192
    D=A
    @count
    M=D

(DRAW_LOOP)
    @color
    D=M
    @addr
    A=M
    M=D

    @addr
    M=M+1

    @count
    M=M-1
    D=M
    @DRAW_LOOP
    D;JGT

    @LOOP
    0;JMP