Hack Assembly Language - Nand2Tetris Unit 04
The course is going great and in this Unit we’re introduced to the lowest form of programming, the machine language.
The instructors have made their own language, the Hack Assembly Language for this course. It’s similar to the regular Assembly but not entirely the same.
I attacked it this week and had some struggles along the way.
What I Built This Week
- Mult.asm - A multiplication program using loops.
- Fill.asm - Program that takes keyboard input and fills/clears the screen based on keypresses.
What Did I Learn
I learned the Hack Assembly Language. Well, some of it. The basics. I am by no means an expert yet. It gave me a very good introduction to the world of very low level programming. I really enjoyed it. Enjoyed being a beginner again and really thinking differntly when coding. Loops are different, variables, syntax and so on.. The mentality shift I really enjoyed, but also struggled with.
I learnt of the 3 D, A, M registers.
- D - The playground register. Use it store temporary data.
- A - The address pointer. Points to the address of the active register. Mostly used like @variable.
- M - Accesses the memory at address A. If A = RAM[20]. M = 100 (or whatever binary value is stored at RAM[20]).
I learnt of
- Variables, Labels & Loops ((LOOP), @variableName)
- Confitional Jumps (JMP, JEQ, etc..)
- Also how they always compare to zero. That has to be taken into account when writing the program. You can’t do 10 > 5
- Memory Mapped I/O Devices. @KBD @SCREEN variables and where they are stored in the RAM.
- @KBD is stored at RAM[24576]
- @SCREEN is stored at RAM[16384] and takes the next 8192 words(16bit arrays). Actually an 8k RAM.
My struggles this Unit
After going through all the material, when it was finally time to write code myself, that’s when it got tough. It all makes sense when you’re reading, writing and an instructor is showing, but when the time comes, it all blanks. That’s part of the struggle and means i’m learning. I’m just trying 1 line at at time.
I had a hard time with the syntax and D, A, M registers. What they meant and how they’re used.
Especially the A was a struggle. When I figured that I could keep incrementing the A register with +1 and access many addresses quickly it kind of clicked.
A part of the loop that paints the screen.
@paintStartAddr // RAM[16384] first time around
A=M // Point the A Register to value of M
M=D // Set the value of D to M
@paintStartAddr
M=M+1 // paintStartAddr = 16385 My previous struggle of the programmers mindset also bugged me here. No early retunrs and the flow of a program is very different.
Labels and variables clicked rather quickly. For loops and jump. That’s smart.
What Surprised Me
How stripped down Hack Assemly is, yet so powerful. You can build very complex programs with just a handful of operations. The screen and keyboard is just memory addresses. Screen is basically just a bunch of pixels with 0 or 1. I know it gets more complicated with RGB and so on but all the way down.
Also how fun it was writing this code.
Connections
- The Program Counter from Unit 03 is what’s used for machine instructions.
- The RAM from Unit 03 is what the M register is accessing.
- The ALU is doing the Arithmetic like D=D+1
Now What?
On to Unit 05, the CPU Architecture. We’re going back to Hardware after a brief stop in software. Let’s go!
See you around!