I'm at a loss when I implement arithmetic operations.
I think I should do "POP 1 or 2 times, execute process and then PUSH".
But what segments or symbols in the text do I reserve "POP"ed value(s)?
Arbitrary selected symbol name is OK?
My guess is as follows. If I have these pseudo vm code,
Can I declare whereX, whereY and whereResult?
Are they nothing to do with segments like LCL, ARG, etc?
No, you can't do this, because these will pollute the region reserved for static variables.
The VM Translator was THREE registers, R13, R14, and R15, available for its use. But most commands, including add, do not need to us them at all.
Don't try to write ASM code that implements the VM operations directly. By that, I mean you don't have to pop two arguments off the stack to "someplace", add them together in that "someplace" and then push the result back onto the stack.
Draw a memory map of what things look like before the command executes and what it needs to look like after the command executes. All you have to do is write ASM code that turns the first into the second.
Yes, that should work. I'm going to remove your code so that we don't leave solutions lying around the forum.
Your solution uses 10 instructions. It can be done in 5. As you can imagine, the add command is going to be calls LOTS of times, so even trimming a single instruction from it is worth quite a bit of effort -- trimming five is very significant.
But, for now, what is important is that you have something that will work and that's the first critical step. Get everything working and worry about performance later.