7.3.内存段
\(7.3.\)Memory Segments
1.Memory segments intro
There are different variable kinds in the high-level language, such as:
- Argument variables
- Local variables
- Static variables
Since different kind of variables play different role, we need some mechanism to record the different roles of different variables. We can achieve this by the notion of memory segments:
So the program below can be translated into two ways: without memory segments:
And with memory segments:
In this way, we will be able to preserve the role semantics of these variables.
But it seems that we lost the variable names in the process. It's not a big deal, however, for the VM obstruction doesn't recognize symbolic variable names, all the variables are replaced by references to memory segments.
Through memory segments, we now interact with several different segments:
And the syntax of the command is:
push / pop segment i
- segment is the segment you want to operate on.
- i is the index of the command in that segment.
- \(e.g.\)
:
push constant 17
- \(e.g.\) : translation of
let static 2 = argument 1
:
1 | push argument 1 |
2.The usage of segment
There's a lot of semantics going on in the high-level language:
- static variables, local variables, argument variables, ...
- the object consisting a bundle of variables
We can handle all the semantics using the eight virtual segments.