couldn't really get much information about the virtual register. All i know is the R0 to R15 which is address 0 to 15 are pre-defined.
Is virtual register = virtual machine? so what is a virtual register in the context of Hack machine and its assembler?
I've have never heard of HACK machine before, so I looked for it on Google and found the official page, read a couple of PDFs and make a simple test. All this just to say: I have 5 min preparation on this subject :)
Virtual Register are just symbols, names for numbers.
R2is just another way to write2.The manual states
I initially thought you could use them like this
R3=D-1or evenR3=R2+R3. In the HACK machine accessing memory is very verbose as you can only address it indirectly by the use of theAregister, so I thought that Virtual Registers could be used to automatically generate more complex code, say by assemblingR3=R1+R2into@1 D=M @2 D=D+M @3 M=DBut this is not the case, you can use them only with the
@instruction and I don't see how this would simplify assembly.They can make it more readable if you stick to the convention that memory locations are loaded into
Aonly by using labels, so that if you see a@R10you known/expect thatAis loaded with an address and when you see an (equivalent)@10you know/expect thatAcontains a values for arithmetic computation (say adding 10 toD).This is just pure semantic however, a matter of how you use the assembler, like naming string variable with str prefix, and have not meaning to the assembler itself.