Workshop In Computer Construction - From Nand to Tetris
Home
Syllabus
Book
Projects
Tools
Guidelines
Administratives
Twist for project 6 - Compact your assembler

The twist for project 6 is to add support for the [ ] operator in your Assembler.

This operator has the same effect as @xxx (i.e., [xxx] stores xxx in the A register) but instead of being used in a seperate line, it can be joined with any assembly command that uses the value of the A register. This operator can only appear once in a line and should appear right after the first token that uses the value of the A register.

For example, instead of the following 2 lines (of our original assembly):
@xxx
D=M
One may write the following line, which should have the same effect:
D=M[xxx]

Please notice that:
  • You should support this operator for all commands that use the value of the A register (and please notice that xxx can also be a label / variable name / reserved name / etc).
  • You should submit one Assembler that includes the twist. The twist does not replace any part of the assembly - it's just an addition. (All programs which are valid in the original assembly are still valid and do the exact same thing!).
  • You should describe in your README how you chose to implement this operator.
  • You should check your own code - we will not supply tests for this operator.
  • This operator will save a lot of assembly code (before, each assembly instruction line was exactly one HACK instruction and vice-versa). Can you think of other ways to do so?

Some other examples of uses of the [ ] operator:
  • D=M[xxx]+1 (similar to the above).
  • M[xxx]=M+1 (in this case the two "M"s refer to the same memory address - xxx).
  • D=A[xxx] (this will result in both A and D having the value xxx).
  • D;JGT[xxx] (this will cause a jump to xxx if D>0).