Posts 从零开始构建计算机chapter8_虚拟机II
Post
Cancel

从零开始构建计算机chapter8_虚拟机II

1. 程序流程控制命令

  1. label
  2. goto label 无条件跳转
  3. if-goto lable 弹栈 如果值非零 就跳转

汇编符号

p2

label 与 goto label

1
2
3
4
5
6
// vm command: label LOOP_START
(Test$LOOP_START)

// vm command: goto LOOP_START
@Test$LOOP_START
0;JMP

if goto label

1
2
3
4
5
6
7
8
// vm command: if-goto LOOP_START
// get the top element of stack
@SP
M=M-1
A=M
D=M
@Test$LOOP_START
D;JNE

2. 函数调用

  1. function f n n为该函数有几个局部变量
  2. call f m m为该函数m个参数压入栈中
  3. return p2

function f n

声明函数 并将局部变量压栈 function SimpleFunction 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// vm command: function SimpleFunction 2
(SimpleFunction)
// initialize local segment
@2
D=A
(SimpleFunction$LOOP)
D=D-1
@SimpleFunction$END
D;JLT
// push the value into stack
@SP
A=M
M=0
@SP
M=M+1
@SimpleFunction$LOOP
0;JMP
(SimpleFunction$END)

call f m 与 return

在m个参数被压入栈中后调用f

原vm

1
2
3
4
5
6
7
8
9
function main 1
push constant 123
call Sys.add 1

function add 0
push argument 0
push constant 12
add
return

翻译为汇编

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
 声明函数 局部变量压栈
**/
// vm command: function main 1
(main)
// initialize local segment
@1
D=A
(main$LOOP)
D=D-1
@main$END
D;JLT
// push the value into stack
@SP
A=M
M=0
@SP
M=M+1
@main$LOOP
0;JMP
(main$END)

/**
压入常量
**/
// vm command:push constant 123
@123
D=A
// push the value into stack
@SP
A=M
M=D
@SP
M=M+1


// vm command: call Sys.add 1
// save work
/**
地址压栈 返回地址
**/
@Sys.add1$retAddr1
D=A
// push the value into stack
@SP
A=M
M=D
@SP
M=M+1

/**
局部变量压栈
**/
@LCL
D=M
// push the value into stack
@SP
A=M
M=D
@SP
M=M+1

/**
参数压栈
**/
@ARG
D=M
// push the value into stack
@SP
A=M
M=D
@SP
M=M+1

/**
this 压栈
**/
@THIS
D=M
// push the value into stack
@SP
A=M
M=D
@SP
M=M+1

/**
that 压栈
**/
@THAT
D=M
// push the value into stack
@SP
A=M
M=D
@SP
M=M+1

/**
新的arg地址
**/
// argument process
@SP
D=M
@5
D=D-A
@1
D=D-A
@ARG
M=D

/**
新的local地址
**/
// LCL=SP
@SP
D=M
@LCL
M=D
// go to called function
@Sys.add
0;JMP

/**
为返回地址声明标签
**/
(Sys.add1$retAddr1)


// vm command: function add 0
(add)
// initialize local segment
@0
D=A
(add$LOOP)
D=D-1
@add$END
D;JLT
// push the value into stack
@SP
A=M
M=0
@SP
M=M+1
@add$LOOP
0;JMP
(add$END)

// vm command:push argument 0
@ARG
D=M
@0
A=D+A
D=M
// push the value into stack
@SP
A=M
M=D
@SP
M=M+1

// vm command:push constant 12
@12
D=A
// push the value into stack
@SP
A=M
M=D
@SP
M=M+1

// vm command:add
// get the top element of stack
@SP
M=M-1
A=M
D=M
// store the result temporarily
@R14
M=D
// get the top element of stack
@SP
M=M-1
A=M
D=M
// store the result temporarily
@R13
M=D
@R13
D=M
@R14
D=D+M
// push the value into stack
@SP
A=M
M=D
@SP
M=M+1

// vm command: return
/**
lcl存储在r13中
**/
@LCL
D=M
@R13
M=D        // temporarily store the endFrame
@R13
D=M

/**
address存储在r14中
**/
@5
A=D-A      // get the return address
D=M
@R14
M=D        // temporarily store the return address
@ARG
D=M
@0
D=D+A
// store the result temporarily
@R15
M=D
// get the top element of stack
@SP
M=M-1
A=M
D=M
// store the top value
@R15
A=M
M=D
// set the SP
@ARG
D=M
@SP
M=D+1
// restore scene
@R13
D=M
@R15
M=D

@R15
M=M-1
A=M
D=M
@THAT
M=D

@R15
M=M-1
A=M
D=M
@THIS
M=D

@R15
M=M-1
A=M
D=M
@ARG
M=D

@R15
M=M-1
A=M
D=M
@LCL
M=D

// goto return address
@R14
A=M
0;JMP

This post is licensed under CC BY 4.0 by the author.

Contents

Trending Tags