lonelyjohner wrote
I don't understand how to write CMP and TST files for the test case. The CMP and TST file provided by the course all directly include the RAM name. Here should I just replace them with register name A,D or memory M?
You do not change the TST or CMP files. The TST file is like an exam given to you by your instructor; it asks your program some questions. The CMP file is your instructor's answer key.
Your program doesn't need to use named variables. In fact, it must not use named variables to get the test arguments from RAM[0] and [1], nor to set the answer into RAM[2].
Don't confuse
A,
D and
M with variables; they have special meaning in the Hack assembly language.
A and
D refer to the A-register and D-register in the CPU;
M means RAM[A-register].
The @ command only sets the A-register to a number; if never reads from the RAM.
for example, if I want to test the function here: Set D to A-1
Is this all I need to write?
D=A-1 (with no @ sentence)
Plus, I thought I was able to get the address of arr[3] from the @D. If it's not allowed, what should I do? Thanks
Yes, D=A-1 sets the D-register to A-register - 1.
Don't worry about complicated things like accessing elements of arrays stored in RAM. You won't need to do that until chapter 7.
For Mult.asm and Fill.asm you will access the RAM and screen memory directly. For example, the first thing your Mult.asm will want to do is set RAM[2] to 0:
@2
M=0
Your Fill.asm program will use either @16384 or @SCREEN to refer the starting address of the screen. Similarly it will use either the numeric address of the keyboard or the built in constant KEYBOARD.
You may want to use some named variables in Fill.asm. For example:
@index
M=0
(ClearLoop)
@index // address of 'index' into A
D=M // value of index into D
@SCREEN
A=D+A // address of SCREEN[index] into A
M=-1 // set screen pixels
--Mark