Skip to content

Commit e01dd76

Browse files
committed
updated 第四章
1 parent e98c035 commit e01dd76

13 files changed

+215
-0
lines changed

.DS_Store

2 KB
Binary file not shown.

Documents/.DS_Store

0 Bytes
Binary file not shown.

Documents/Code/.DS_Store

0 Bytes
Binary file not shown.

Documents/Code/Fill.asm

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// This file is part of www.nand2tetris.org
2+
// and the book "The Elements of Computing Systems"
3+
// by Nisan and Schocken, MIT Press.
4+
// File name: projects/04/Fill.asm
5+
6+
// Runs an infinite loop that listens to the keyboard input.
7+
// When a key is pressed (any key), the program blackens the screen,
8+
// i.e. writes "black" in every pixel;
9+
// the screen should remain fully black as long as the key is pressed.
10+
// When no key is pressed, the program clears the screen, i.e. writes
11+
// "white" in every pixel;
12+
// the screen should remain fully clear as long as no key is pressed.
13+
14+
// Put your code here.
15+
16+
17+
18+
//At first we set n
19+
(Infinite)
20+
21+
22+
@KBD
23+
D = M
24+
@BLACK
25+
D; JGT // if D > 0 then turns black
26+
27+
//TURNS WHITE
28+
@i
29+
M = 0
30+
@SCREEN
31+
D = A
32+
@KBD
33+
D = A - D
34+
@n
35+
M = D
36+
37+
38+
@SCREEN //gets the add of the Screen
39+
D = A
40+
@index
41+
M = D
42+
43+
44+
(LOOP1) //Fills the screen: 8192
45+
@i
46+
M = M + 1
47+
D = M
48+
@n
49+
D = D - M
50+
@END
51+
D; JGT
52+
@index
53+
A = M
54+
M = 0
55+
@1
56+
D = A
57+
@index
58+
M = D + M
59+
@LOOP1
60+
0; JMP
61+
62+
63+
64+
// TURNS BLACK
65+
(BLACK)
66+
@i
67+
M = 0
68+
@SCREEN
69+
D = A
70+
@KBD
71+
D = A - D
72+
@n
73+
M = D
74+
75+
76+
@SCREEN //gets the add of the Screen
77+
D = A
78+
@index
79+
M = D
80+
81+
82+
(LOOP2) //Fills the screen: 8096
83+
@i
84+
M = M + 1
85+
D = M
86+
@n
87+
D = D - M
88+
@END
89+
D; JGT
90+
@index
91+
A = M
92+
M = -1
93+
@1
94+
D = A
95+
@index
96+
M = D + M
97+
@LOOP2
98+
0; JMP
99+
100+
101+
//runs an infinite loop
102+
(END)
103+
@Infinite
104+
0; JMP

Documents/Code/Mult.asm

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// This file is part of www.nand2tetris.org
2+
// and the book "The Elements of Computing Systems"
3+
// by Nisan and Schocken, MIT Press.
4+
// File name: projects/04/Mult.asm
5+
6+
// Multiplies R0 and R1 and stores the result in R2.
7+
// (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[2], respectively.)
8+
//
9+
// This program only needs to handle arguments that satisfy
10+
// R0 >= 0, R1 >= 0, and R0*R1 < 32768.
11+
12+
// Put your code here.
13+
14+
15+
16+
@R2
17+
M = 0
18+
(LOOP)
19+
@R0
20+
D = M
21+
@END
22+
23+
24+
D; JEQ
25+
@R0
26+
M = M - 1
27+
@R1
28+
D = M
29+
(END)
30+
@R2
31+
M = M + D
32+
@LOOP
33+
0; JMP
34+
(END)
35+
//(END)
36+
@END
37+
0; JMP

Images/.DS_Store

6 KB
Binary file not shown.

Images/hack_builtin_symbols.png

513 KB
Loading

Images/hack_character_set.png

911 KB
Loading

Images/hack_computer_architecture.png

304 KB
Loading

Images/hack_contents.png

340 KB
Loading

Images/hack_screenMemory_map.png

817 KB
Loading
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 第四章:Hack Machine language
2+
3+
> 本章内容:了解一台Computer所需要的所有基本部件:CPU,ROM,RAM,I/O deivices。然后完成部件,并且最后搭建成hack computer。
4+
5+
6+
7+
<img src="/Users/wocaibujiaoquanmei/iCloud 云盘(归档)/GitHub/nand2tetris/Images/hack_contents.png" alt="image-20211011201932143" style="zoom:50%;" />
8+
9+
**内存地址空间的分布:(在hack computer的架构中,指令内存和数据内存是分割开的)**
10+
11+
- 指令地址空间
12+
- 数据地址空间
13+
14+
15+
16+
**指令类型(只有两种):**
17+
18+
- A- 指令: 地址指令
19+
- 用法:`@value`
20+
21+
- C- 指令: 计算指令
22+
- 用法: `dest = comp ; jump`
23+
24+
**@value语法:**
25+
26+
- 导致$A$寄存器的值更新为$value$
27+
- 导致被隐式选择的寄存器为$M[A]$(side effect):也就是说$M$默认为$M[A]$
28+
- 该指令还可用于创建变量和标签
29+
30+
31+
32+
**`C指令`各位域详解:**
33+
34+
![image-20211011190905670](/Users/wocaibujiaoquanmei/Library/Application Support/typora-user-images/image-20211011190905670.png)
35+
36+
**一些预定义的标签:**
37+
38+
![image-20211011201712082](/Users/wocaibujiaoquanmei/iCloud 云盘(归档)/GitHub/nand2tetris/Images/hack_builtin_symbols.png)
39+
40+
41+
42+
其中比较重要的有:**分支****循环**
43+
44+
该操作都通过核心的`jump`指令来完成。
45+
46+
47+
48+
**I/O内存映射**
49+
50+
<img src="/Users/wocaibujiaoquanmei/iCloud 云盘(归档)/GitHub/nand2tetris/Images/hack_screenMemory_map.png" alt="image-20211012114001565" style="zoom: 33%;" />
51+
52+
我们的`RAM​`是16位的,使用`2^{13}*16`个位,映射为屏幕的`256 * 512​`个点阵
53+
54+
55+
56+
**键盘**
57+
58+
预定义宏`@KBD​`代表键盘的占用内存的起始位置,实际上该内存的大小仅为1。
59+
60+
<img src="/Users/wocaibujiaoquanmei/iCloud 云盘(归档)/GitHub/nand2tetris/Images/hack_character_set.png" alt="image-20211012221144778" style="zoom: 50%;" />
61+
62+
**变量的抽象**
63+
64+
本质上是在内存中申请了一片区域,然后通过CPU和GPU等进行各种操作。在hack语言中,通过`A`寄存器指定变量的内存偏移量,然后可以直接通过`M``M[A]​`来操作A指向的内存区域,使得其值发生改变。
65+
66+
67+
68+
69+
70+
#### 源码
71+
72+
`Mulit.asm`: [这里]()
73+
74+
`Fill.asm`[这里]()

Notes/.DS_Store

6 KB
Binary file not shown.

0 commit comments

Comments
 (0)