# Use the value of D as a memory address

9 messages
Open this post in threaded view
|

## Use the value of D as a memory address

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

## Re: Use the value of D as a memory address

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

## Re: Use the value of D as a memory address

Open this post in threaded view
|

## Re: Use the value of D as a memory address

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

## Re: Use the value of D as a memory address

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

## Re: Use the value of D as a memory address

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

## Re: Use the value of D as a memory address

 Administrator In reply to this post by Thorvald Also, I've written an introduction to Hack programming that you might find helpful. Introduction to Hack Assembly Language--Mark
 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