I am learning about Hack Assembly Language and Jump Instructions. Could anyone please give me elaborate explanations of how you could calculate the jump instructions for a given virtual machine code. I would like to understand how to find the Jump Instructions if I had the Virtual Machine Command of, for example, push constant 0.
Please give me detailed explanations so I can learn!
Example: VIRTUAL MACHINE COMMAND is 'push constant 0'.
How to get from THAT COMMAND to the following written JUMP INSTRUCTIONS:
@SP
A = M
M = 0
@SP
M=M+1
The jump instruction above is just an example, not the answer.
Jumps are encoded in C-type instructions.
C-type instructions have the following encoding:
To differentiate A-type and C-type,
A/Cis0for A-type andA/Cis1for C-type.If the
jumpfield of a C-type instruction has the binary value000, then no jump is encoded.Otherwise, any other binary value in the
jumpfield encodes some kind of branch. If the condition of the branch is true, it will transfer control to the value in theAregister (effectively doingpc := A), so typically, a branching instruction is preceded by an@instruction to load theAregister with the address of a label, but other approaches are possible. If the condition of the branch is false, it will simply advance to the next sequential instruction (pc := pc + 1instead ofpc := A)See the following presentation: https://www.coursera.org/lecture/nand2tetris2/unit-0-4-hack-language-specification-MXyAQ, at time 7:57 for a table of the C-type instruction encodings.