Skip to content

Commit a08c497

Browse files
committed
Chapter 4 - Machine Language
Slight lexicological improvements to Asm programs Mult and Fill.
1 parent 06f6d20 commit a08c497

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

04/fill/Fill.asm

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,46 +25,46 @@
2525

2626
(LOOP)
2727
// Setup i counter variable for SETSCREEN loop.
28-
@8191
28+
@8191 // 8192 total words address the screen; we already address 0x4000.
2929
D=A
3030
@i
3131
M=D
3232

33-
// Set default color to all black (key down)
33+
// Set default color to all black (key down).
3434
@color
35-
M=-1
35+
M=-1 // -1 represented as 1111111111111111 in Hack.
3636

37-
// Pressed key? Jump to SETSCREEN
38-
@24576
37+
// Pressed key? Jump to SETSCREEN.
38+
@KBD
3939
D=M
4040
@SETSCREEN
4141
D;JNE
4242

43-
// No pressed key? Set color to white, then jump to SETSCREEN
43+
// No pressed key? Set color to white, then jump to SETSCREEN.
4444
@color
45-
M=0
45+
M=0 // RAM[@color] = white
4646
@SETSCREEN
4747
0;JMP
4848

4949
(SETSCREEN)
50-
// If i < 0, send back to parent loop
50+
// If i < 0, send back to parent loop.
5151
@i
5252
D=M
5353
@LOOP
5454
D;JLT
5555

5656
// Calculate 16384+i, the current screenword.
5757
@SCREEN
58-
D=D+A
58+
D=D+A // i=i+@SCREEN
5959
@screenword
6060
M=D
6161

62-
// Set color to white or black
62+
// Set color at current screenword to white or black.
6363
@color
64-
D=M
64+
D=M // Pull the color from RAM[@color], store in D register.
6565
@screenword
66-
A=M
67-
M=D
66+
A=M // Pull the screenword from data memory, becomes address.
67+
M=D // RAM[RAM[@screenword]] = RAM[@color].
6868

6969
// Decrement i.
7070
@i

04/mult/mult.asm

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55

66
// Multiplies R0 and R1 and stores the result in R2.
77
// (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[2], respectively.)
8+
// + Preserves R0 and R1.
89

910
// Setup i counter variable
10-
@1
11+
@R1
1112
D=M
1213
@i
13-
M=D
14+
M=D // RAM[@i] = R1
1415

1516
// Make sure R2 is zero-ed out
16-
@2
17-
M=0
17+
@R2
18+
M=0 // RAM[@R2] = 0
1819

1920
(LOOP)
2021
// If i == 0, jump to END.
@@ -24,14 +25,14 @@
2425
D;JEQ
2526

2627
// Do SUM stuff here. R2 = R2 + R0
27-
@0
28+
@R0
2829
D=M
29-
@2
30-
M=M+D
30+
@R2
31+
M=M+D // RAM[@R2] = RAM[@R2] + RAM[@R0]
3132

3233
// Decrement i.
3334
@i
34-
M=M-1
35+
M=M-1 // RAM[@i] -= 1
3536

3637
// Loop again.
3738
@LOOP

0 commit comments

Comments
 (0)