Steven Meow De Claude
Steven Meow De Claude

Reputation: 380

HACK Machines and its assembler

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?

Upvotes: 0

Views: 882

Answers (1)

user781847
user781847

Reputation:

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. R2 is just another way to write 2.

Assembled @R2 and @2 instructions

The manual states

To simplify assembly programming, the symbols R0 to R15 are predefined to refer to RAM addresses 0 to 15, respectively.

I initially thought you could use them like this R3=D-1 or even R3=R2+R3. In the HACK machine accessing memory is very verbose as you can only address it indirectly by the use of the A register, so I thought that Virtual Registers could be used to automatically generate more complex code, say by assembling R3=R1+R2 into

@1 D=M @2 D=D+M @3 M=D

But 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 A only by using labels, so that if you see a @R10 you known/expect that A is loaded with an address and when you see an (equivalent) @10 you know/expect that A contains a values for arithmetic computation (say adding 10 to D).
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.

Upvotes: 2

Related Questions