Pointers

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

Pointers

Mark Blackwell
Perhaps I'm just being dense, but I don't understand how to set the A register to a variable memory location.

So for instance I can do
@SCREEN
M=!M
which draw a row of 16 black pixels starting at the top left

And I can even say
@16385
M=!M
and get the next row of black pixels

But I'm pretty sure that's not the right way to draw 8192 rows of black pixels.

How do I substitute the @SCREEN or @16385 for a variable?

@SCREEN
D=M
@currentLocation
M=D

seems like the right start, because @currentLocation will now store 16384, but simply writing to @currentLocation doesn't solve the problem... I want to write to the memory address that @currentLocation points to.

Hints? Clues?
Reply | Threaded
Open this post in threaded view
|

Re: Pointers

ybakos
Plan your program before worrying about the Hack assembly.

Mark Blackwell wrote
I don't understand how to set the A register to a variable memory location.
Can you be more specific? The instruction

@foo

Specifically stores the address of foo in the A register. This also creates a variable for you in Hack assembly. At this level of abstraction, you don't need to worry about what that specific memory address is (since you've named it). However, variables in Hack assembly are stored in addresses 16 and up.

For the project you're on, isn't your goal to merely write a certain value to addresses @SCREEN through @24575?
Reply | Threaded
Open this post in threaded view
|

Re: Pointers

cadet1620
Administrator
In reply to this post by Mark Blackwell
Mark Blackwell wrote
@SCREEN
D=M
@currentLocation
M=D
Examine what this code does in detail
@SCREEN            // A = SCREEN
D=M                // D = RAM[A]
@currentLocation   // A = currentLocation
M=D                // RAM[currentLocation] = D
What you are missing is that the second instruction needs to get the constant value SCREEN into D, not RAM[A].

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

Re: Pointers

Mark Blackwell
I figured it out (the old trick of explaining it helped). I just need to increment the A register by 1 and then write to M... But in the spirit of the forum won't say more.