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.
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.
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.