Mapping VM to hack assembly.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Mapping VM to hack assembly.

Henoktes722
Mapping VM to hack assembly.

push constant 7
push constant 8
add

What will be the compact hack assembly of this program, just to check with the output of my VM translator.
Reply | Threaded
Open this post in threaded view
|

Re: Mapping VM to hack assembly.

WBahn
Administrator
Henoktes722 wrote
Mapping VM to hack assembly.

push constant 7
push constant 8
add

What will be the compact hack assembly of this program, just to check with the output of my VM translator.
There a multiple ways to do it.

If our translator is really sophisticated, it could recognize these three VM commands as conveying a single expression that can be replaced by

push constant 15

and then translate that.

BTW, modern compilers do this kind of thing as a matter of course -- they have gotten so good at optimization that it has long since crossed into outright sorcery.

But getting back to your situation, there are still multiple ways to translate each of the VM commands into assembly code. What you need to look at is what did the machine state look like before the command is run and what should it look like after. Then compare what it should look like to what it actually looks like.

Reply | Threaded
Open this post in threaded view
|

Re: Mapping VM to hack assembly.

Henoktes722
Yeah, but to do my own simple VM translator, I need one example of .vm mapped to .hack file. So that I can check my VM translator output.
Reply | Threaded
Open this post in threaded view
|

Re: Mapping VM to hack assembly.

WBahn
Administrator
Henoktes722 wrote
Yeah, but to do my own simple VM translator, I need one example of .vm mapped to .hack file. So that I can check my VM translator output.
Why?

Let's consider the two commands you are using in your example, namely push and add.

What changes should the push command make to the machine state?
1) Place the value being pushed at the memory location pointed to by the stack pointer.
2) Increment the stack pointer.

For the case of pushing from the constant memory segment, what value is being pushed?
What sequence of assembly code will make this happen?

What changes should the add command make to the machine state?
1) Replace the value located two places below where the stack pointer points with the sum of the value already at that location and the value immediately above it.
2) Decrement the stack pointer.

Take you best shot at those two and then we can discuss it further, including perhaps tightening up the code some.