Setting A value from RAM value

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

Setting A value from RAM value

polarbearskill
Hello,

I'm working on the fill.asm project.

I've gotten a loop which will store decimal values of 16384 to 24575 in RAM[16].

However, my problem is I can't figure out how to get the data from RAM[16] into the A register so that I can then either "black" or "white" the screen.

I tried putting the RAM[16] (when it equals 16384) into register D, then trying to put register D into A by doing "@D", however this just assigns the next open RAM location to a new variable called "D".

Basically, how do I pull a decimal number out of the ram into the A register so that I can then adjust that memory location?
Reply | Threaded
Open this post in threaded view
|

Re: Setting A value from RAM value

cadet1620
Administrator
This post was updated on .
The @ command puts either a constant, the RAM address of a variable or the ROM address of a label into the A register.

The 'M' source or destination in C-commands, reads or writes RAM[A].

To read the value of RAM[16] into A you need
    @16     // A=16
    A=M     // A = RAM[16]
I wrote an Introduction to Hack assembly programming that you might find useful.

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

Re: Setting A value from RAM value

polarbearskill
Thanks very much for the reply Mark.

A follow up question if I may.

What if instead of 16 being a "constant", I wanted to dynamically reference some RAM segment.

Perhaps I start with wanting the data in RAM[16], but want to read everything from RAM[16] to RAM[20] within a Loop.

I could create a loop variable "i" (lets say i gets stored in RAM[0]), and have an decimal value go from 16 to 20 within "i".

After the first loop, how could I get the "17" which is now in RAM[0] to allow me to pull data from RAM[17], instead of having to hard code it each time (i.e. RAM[16], RAM[17], RAM[18], etc..)?

On Thu, Aug 10, 2017 at 11:03 AM, cadet1620 [via Nand2Tetris Questions and Answers Forum] <[hidden email]> wrote:
The @ command puts either a constant, the RAM address of a variable or the ROM address of a label into the A register.

The 'M' source or destination in C-commands, reads or writes RAM[A].

To read the value of RAM[16] into D you need
    @16     // A=16
    D=M     // D = RAM[16]
I wrote an Introduction to Hack assembly programming that you might find useful.

--Mark


If you reply to this email, your message will be added to the discussion below:
http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/Setting-A-value-from-RAM-value-tp4031312p4031313.html
To unsubscribe from Setting A value from RAM value, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: Setting A value from RAM value

cadet1620
Administrator
You can use named variables in Hack ASM. The assembler assigns them to RAM addresses starting with 16. That way you don't need to keep track of where in RAM all your variables are.

To use addresses stored in RAM variables, you need to load the address of the variable into the A register and then load the value of the variable into the A register.

In this example, 'i' is the first variable used so it gets address 16
    @i      // A = address of i   (16)
    M=0     // i = 0
(LOOP)
    @i      // A = address of i   (16)
    A=M     // A = i   (RAM[16])
    D=M     // D = RAM[i]
    ...
    @i
    M=M+1   // i = i+1

--Mark

[You want to read posts and respond to then in the forum.  You'll see the final posts rather than the initial posts with typos 8-( and your responses will be cleaner without the extra email text.]
Reply | Threaded
Open this post in threaded view
|

Re: Setting A value from RAM value

polarbearskill
Hi Mark,

Thanks very much, this was exactly the problem I was having!

Your insight really helped me over this hump.

Chad

On Thu, Aug 10, 2017 at 12:02 PM, cadet1620 [via Nand2Tetris Questions and Answers Forum] <[hidden email]> wrote:
You can use named variables in Hack ASM. The assembler assigns them to RAM addresses starting with 16. That way you don't need to keep track of where in RAM all your variables are.

To use addresses stored in RAM variables, you need to load the address of the variable into the A register and then load the value of the variable into the A register.

In this example, 'i' is the first variable used so it gets address 16
    @i      // A = address of i   (16)
    M=0     // i = 0
(LOOP)
    @i      // A = address of i   (16)
    A=M     // A = i   (RAM[16])
    D=M     // D = RAM[i]
    ...
    @i
    M=M+1   // i = i+1

--Mark

[You want to read posts and respond to then in the forum.  You'll see the final posts rather than the initial posts with typos 8-( and your responses will be cleaner without the extra email text.]


If you reply to this email, your message will be added to the discussion below:
http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/Setting-A-value-from-RAM-value-tp4031312p4031315.html
To unsubscribe from Setting A value from RAM value, click here.
NAML