Use the value of D as a memory address

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

Use the value of D as a memory address

Thorvald
Perhaps I have missed something, but how can I use the value of D to write in the address @D.
I have the naive statements:

        @R9
        D=M
        @16384
        D=A+D
        @D // I want to access here to register "D" to write -1
        M=-1

Thanks in advance.
Reply | Threaded
Open this post in threaded view
|

Re: Use the value of D as a memory address

ybakos
D is not addressabe. The D and A registers physically exist within the CPU itself. Take a look at the tables in chapter 4. To store a -1 in D, you'd simply write D=-1.
Reply | Threaded
Open this post in threaded view
|

Re: Use the value of D as a memory address

cadet1620
Administrator
This post was updated on .
In reply to this post by Thorvald
[Edit: add explanation of the problem.]
Thorvald wrote
Perhaps I have missed something, but how can I use the value of D to write in the address @D.
You can't use D to reference memory; the address must be in A.

When you write @D, you are loading the address of label "(D)" or a RAM variable named "D" into the A Register.

You can use the A=D command to copy the address from the D Register into the A Register. Usually, you don't need to do this because you can store the result of a computation directly into the A Register.

For example, in your code instead of "D=A+D" use "A=A+D".

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

Re: Use the value of D as a memory address

Thorvald
This post was updated on .
I think it does not work. My code is

        @R9
        D=M
        @16384
        A=A+D
        @A    // <-----
        M=-1

whether I write in the arrow @D or @A it always led me to the register 16.
Reply | Threaded
Open this post in threaded view
|

Re: Use the value of D as a memory address

cadet1620
Administrator
"@A" has the same problem; it is loading the address of a label or variable named "A" into the A Register.

The "@something" instruction means "A-reg = address of something." Since "A=A+D" put the address in the A-reg, you don't need anything more except "M=-1".

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

Re: Use the value of D as a memory address

Thorvald
I see. I think this is indirect addressing, right? I have found in algorithm analysis books that they use this "technique" a lot. Is it treated at some point in the book?
Reply | Threaded
Open this post in threaded view
|

Re: Use the value of D as a memory address

cadet1620
Administrator
This post was updated on .
In reply to this post by Thorvald
Also, I've written an introduction to Hack programming that you might find helpful.

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

Re: Use the value of D as a memory address

cadet1620
Administrator
In reply to this post by Thorvald
Indirect addressing is mentioned in respect to generic assembly languages in chapter 4 section 4.1.3.  In the Hack CPU all memory references are indirect through the A Register.

In the context of algorithms, indirect addressing refers to variables that contain addresses of other variables.

For example, to implement an array that holds strings, usually the array holds addresses of the actual strings (called pointers). This lets the strings use only as much memory as they individually require, rather than every string requiring the same amount of memory.

Simple string array uses 48 words in memory:
1000: first
1016: something longer
1032: last
1048:
Indirect string array uses 28 words in memory:
1000: 1001 1008 1024
1003: first
1008: something longer
1024: last
1028:
Using pointers also makes many data manipulations faster. For instance, if I want to sort this string list alphabetically all I need to do is change the pointers; I don't need to move the string data.
1000: 1001 1024 1008
1003: first
1008: something longer
1024: last
1028:

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

Re: Use the value of D as a memory address

Thorvald
Ok, thank you so much for the help and the reference.