# array

9 messages
Open this post in threaded view
|

## array

 how to implement arrey like arr[5]=3
Open this post in threaded view
|

## Re: array

 Umang, can you be more specific? Are you trying to build some hardware with Hack HDL, or are you working on a Jack program?
Open this post in threaded view
|

## Re: array

 just a jack program. I know  @i  M=100 makes i=100 but i dont know how to make an array like a[5] and initialise it
Open this post in threaded view
|

## Re: array

 Administrator Umang Gupta wrote just a jack program. I know  @i  M=100 makes i=100 but i dont know how to make an array like a[5] and initialise it You can set a[5] to 0 like this: ``` @5 D=A @a A=A+D // A = memory address of a[5] M=0 ```To set a[5] to an arbitrary value is a bit tricky because you need the value in D and the array element address in A.  You should not need to do this until chapter 7.  Hint: to do this you need to use one of the "registers" like R15 to hold either the address or the data for the array element. --Mark
Open this post in threaded view
|

## Re: array

 i want to thank you all for helping. it worked
Open this post in threaded view
|

## Re: array

 Can you help me with this.  I've been going around in circles for a while and I need some help. I'm trying to write hdl code for arr[j]=17.  In order to test the code, I chose "j" to be 3.  so. @3 D=A @j      //RAM[16] = 3 M=D // arr[j] = 0 @j D=M @arr A=D M=D I'm having trouble where to put @17 D=A??  If I put after @j, then j's value disappears.  I can't certainly put it after @arr.... nor can I put it at the beginning!! TIA
Open this post in threaded view
|

## Re: array

 Administrator inyeop wrote I'm trying to write hdl code for arr[j]=17. Working backwards you need 17 in D and arr+j in A so that the last instruction     M=D will set arr[j] = 17. The only way to get 17 in D is by using A either as @17 or as a memory address for some RAM that contains 17.     @17     D=A Now you need to get arr+j into A without changing D. The only way to do this is to have arr+j stored in RAM somewhere. You will need to use a temporary variable. Do something like     compute R15 = arr+j     load 17 into D     load R15 value into A     M=D --Mark