about hack commands

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

about hack commands

lonelyjohner
This post was updated on .
Just to make sure I understand the basics:
I'd appreciate it if anyone could check the codes below for me:
the main function is to make arr[j]=0 and arr[j]=17. Thanks

arr[j]=0://arr=20561,j=3012
@3012
D=A
@20561
D=D+A
@D
M=0


arr[j]=17//
@3012
D=A
@20561
D=D+A
@D
D=M
@17
D=D+A

if x[i]<=0 goto NEXT
@x
D=A  //&x
@i
D=D+A  //&x+i
@D
D=M    //x[i]
@NEXT
D;JLE

another question is whether do I need to use the symbols like arr or j to code this or simply just replace them with the numbers you provide in the pdf?
Reply | Threaded
Open this post in threaded view
|

Re: about hack commands

cadet1620
Administrator
lonelyjohner wrote
Just to make sure I understand the basics:
I'd appreciate it if anyone could check the codes below for me:
the main function is to make arr[j]=0 and arr[j]=17. Thanks

...

//arr=20561,j=3012
arr[j]=17//
@3012
D=A
@20561
D=D+A
@D
D=M
@17
D=D+A

...

another question is whether do I need to use the symbols like arr or j to code this or simply just replace them with the numbers you provide in the pdf?
I suggest that you load your code into the CPU Simulator, step through it, an see what happens; it doesn't do what you want.

Reread how @ commands work. In particular, if the argument to an @ command is not a number or a code label, the assembler allocates a RAM location for a new variable. The command "@D" does not do anything involving the D Register.

To read or write RAM locations, you need to use "M" as a source or destination. The address of the RAM location to be accessed must be in the A Register.


It is better to use symbolic names because there is less chance of error.  For the programs that you write for for project 4, you can use the built-in variable names R0 - R15, KBD, and SCREEN instead of hard coded numbers. You can use a named variable like "j" for indexing into the screen memory.


--Mark
Reply | Threaded
Open this post in threaded view
|

Re: about hack commands

lonelyjohner
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?

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  
Reply | Threaded
Open this post in threaded view
|

Re: about hack commands

cadet1620
Administrator
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