PUT A VALUE IN RAM

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

PUT A VALUE IN RAM

Minoshi
I am basically trying to add 5 numbers . But i can't figure out how to put value in RAM . For example i want to put value 100 in REGISTER 0 of RAM . So i am doing the following------>
@0
M=100

but this is showing error .But M=M+1 works and M=M+100 does not .
Please guide me on this . I want to add numbers . It is difficult to understand from the book . Getting confused about capabilities of RAM and REGISTERS A and D .
Reply | Threaded
Open this post in threaded view
|

Re: PUT A VALUE IN RAM

Chen Li
Try this:
@100
D=A
@0
M=D
Reply | Threaded
Open this post in threaded view
|

Re: PUT A VALUE IN RAM

ivant
In reply to this post by Minoshi
Minoshi wrote
I am basically trying to add 5 numbers . But i can't figure out how to put value in RAM . For example i want to put value 100 in REGISTER 0 of RAM . So i am doing the following------>
@0
M=100

but this is showing error .But M=M+1 works and M=M+100 does not .
Please guide me on this . I want to add numbers . It is difficult to understand from the book . Getting confused about capabilities of RAM and REGISTERS A and D .
HACK's CPU has 2 registers and you have to use both of them to do even simple operations. Suppose that you want to put 0 in Mem[0]. You can do that like this:
@0
M=0
This will also work for for 1 or for -1:
@0
M=-1
But it won't work for any other number. Why? Because the ALU can produce only these 3 numbers by itself. The only way to directly, that is without computation or memory access, store other numbers in registers is through the @ instruction. But it stores the number in register A, which we also need to hold the address. So we have to use the D register as well.
@100 // A holds 100
D=A  // D also holds 100
@0   // Now A holds the address
M=D  // We store the content of D in the memory address contained in A, that is M[0]=100
Which is exactly what Chen Li wrote, but I thought I'd add some explanations as well.
Reply | Threaded
Open this post in threaded view
|

Re: PUT A VALUE IN RAM

cadet1620
Administrator
In reply to this post by Minoshi
See Introduction to Hack Assembly Language for more information, including a sample program that writes a list of numbers to RAM using an index variable in a loop.
Reply | Threaded
Open this post in threaded view
|

Re: PUT A VALUE IN RAM

Chen Li
Wow, this introduction is also pretty useful for both assembler and virtual machine. Thanks a ton!
Reply | Threaded
Open this post in threaded view
|

Re: PUT A VALUE IN RAM

Minoshi
In reply to this post by Chen Li
Thank you Chen. It works.
Reply | Threaded
Open this post in threaded view
|

Re: PUT A VALUE IN RAM

Minoshi
In reply to this post by ivant
THANK YOU ivant . Thanks for explaining . helped me solve my problem.

And the introduction is really helpful cadet.Thank you .