Now that we have all the fundamental hardware components for our computer, let’s rise another layer of abstraction. We will discuss the machine code and assembly code, which are really just encodings of the inputs that will be fed to our hardware components.
The Nand2Tetris computer has the following architecture:
It supports three registers: D, PC, and A. D is a general-purpose register that can be used for anything. PC is the program counter, which points to the instruction to execute. A is an address register. It has a companion register named M, which yields the contents of memory at address A. One could say that M short for *A.
It supports assignment to any of D, A, or M.
It supports these right-hand side expressions:
0
1
-1
D
A M
!D
!A !M
-D
-A -M
D+1
A+1 M+1
D-1
A-1 M-1
D+A D+M
D-A D-M
A-D M-D
D&A D&M
D|A D|M
It also supports 16 storage registers, R0 through R15, but these can’t be used directly in computation. Their contents will have to be moved into one of the main registers first.
It supports a special @IDENTIFIER instruction. If IDENTIFIER is a known identifier, it essentially executes A = &IDENTIFIER, placing the address of IDENTIFIER in the A register. If IDENTIFIER is unknown, it allocates space in memory for a variable with this name. Future @s with this identifier will load this variable’s address.
The ASCII value of the currently depressed key can be accessed via KEYBOARD.
The bitmap used to draw the screen can be accessed via SCREEN. The screen only displays black and white, and the machine’s word size is 16-bits. This means that every 16 pixels are grouped together into a single word. Accessing a single pixel with assembly language only is annoying. The bitmap has a resolution of 512 by 256 pixels, or 512 / 16 by 256 or 32 by 256 words.
Constants can be loaded into memory with @. For example, @123 behaves like A = 123.
Today we’ll explore this architecture by writing some Hack assembly programs:
...Plot a sixteen-pixel line at beginning of row R14 in the SCREEN bitmap.
Here’s your TODO list to complete before we meet next:
Read chapter 3.
On a quarter sheet, write the HDL for the Bit component.
Plan to not have class on Friday. I will be attending an all-day workshop led by Code.org, a non-profit whose aim is to get young people learning about computer science.