CS 352 Lecture 15 – Hack Assembly
Dear students,
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
, andA
.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 namedM
, which yields the contents of memory at addressA
. One could say thatM
short for*A
. - It supports assignment to any of
D
,A
, orM
. - 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
throughR15
, 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. IfIDENTIFIER
is a known identifier, it essentially executesA = &IDENTIFIER
, placing the address ofIDENTIFIER
in theA
register. IfIDENTIFIER
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 likeA = 123
.
Today we’ll explore this architecture by writing some Hack assembly programs:
- Add
R0
andR1
and store the result inR2
. - Store the ordinal number of the depressed letter key in local variable ORD. By ordinal number, I mean that A = 0, B = 1, C = 2, D = 3, …
- Local variable
ISEVEN
is 1 ifR0
is even and 0 otherwise. - Store the twos complement of
R0
inR1
. - Add
R0
,R1
, andR2
and store the result inR2
. R4[R5] = R6
.- Declare an array of five
short
s with these contents:[R0 * 1, R0 * 2, R0 * 3, R0 * 4, R0 * 5]
. R15 = R14 * 32
.- Plot a sixteen-pixel line at beginning of row
R14
in theSCREEN
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.
See you next class!
Sincerely,
P.S. Here’s the code we wrote together:
SumR.asm
@R0 D=0 D=D+M @R1 D=D+M @R2 M=D
Ord.asm
@KBD D=M @65 D=D-A @ORD M=D
Even.asm
D=1 @R0 M=M+1 D=D&M @ISEVEN M=D