Posts 从零开始构建计算机chapter7_虚拟机I
Post
Cancel

从零开始构建计算机chapter7_虚拟机I

高级语言翻译成vm语言,再由vm语言翻译成机器语言。这两章介绍vm语言到机器语言的翻译过程

1. 算数命令

p2

范例:

1
2
3
push constant 17
push constant 8
add

翻译

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
// vm command:push constant 17
@17
D=A
// push the value into stack
@SP
A=M
M=D
@SP
M=M+1

// vm command:push constant 8
@8
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

简单解释一下流程,将常量17,常量8压入栈中。调用add命令,弹出栈顶元素分别存储在R13与R14临时寄存器中,相加,结果压入栈中

2. 内存命令

命令简介

  • push local 1 将local[1] 的值压入栈中

  • pop local 1 将栈顶元素弹出,并存入local[1]中

  • vm函数使用的内存段 p2

内存分段

p2

寄存器位置

p2

static 问题 存放在f.j

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// vm command:push constant 111
@111
D=A
// push the value into stack
@SP
A=M
M=D
@SP
M=M+1

// vm command:pop static 1
// get the top element of stack
@SP
M=M-1
A=M
D=M
@Test.1
M=D
This post is licensed under CC BY 4.0 by the author.

Contents

Trending Tags