array

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

array

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

Re: array

ybakos
Umang, can you be more specific?

Are you trying to build some hardware with Hack HDL, or are you working on a Jack program?
Reply | Threaded
Open this post in threaded view
|

Re: array

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

Re: array

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

Re: array

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

Re: array

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

Re: array

cadet1620
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

Reply | Threaded
Open this post in threaded view
|

Re: array

inyeop
Thanks Mark.  I got it now.  I wish I had figured that out my own. :(  Looking at the working code, it looks so obvious. haha.

Mike
Reply | Threaded
Open this post in threaded view
|

Re: array

cadet1620
Administrator
inyeop wrote
Looking at the working code, it looks so obvious.
It always does! 8^)

If you are planning to do the software half of the course, remember this trick; you'll need it in the VM translator.

--Mark