A couple of questions about memory segments

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

A couple of questions about memory segments

ElliotW
Is 'static i' equivalent in terms of memory allocation to @16+i?  
The chapter notes seem to imply the static memory segment has a unique implementation to the other memory segments, but so far I've programmed it almost exactly like push temp i (except push temp uses 5+i to access RAM[5..12]).  
 
I'm also unsure about what data the push/pop Pointer segment should get. The notes say it should supply the "base address of THIS or THAT", but are the base addresses RAM[3,4] or the value stored in these registers?  
 
TIA.
Reply | Threaded
Open this post in threaded view
|

Re: A couple of questions about memory segments

WBahn
Administrator
This post was updated on .
The static memory segment is very unique compared to the others because the static variables for each VM file must coexist in the same memory block on the Hack hardware, namely addresses 16 - 255. Remember that the static variables for each class must remain in memory throughout the entire execution of the entire program.

So if static 4 always referred to memory address 20 (16 + 4), then EVERY class would use that same memory location for whatever variable it maps to static 4. Not a good idea.

Instead, you need to map each memory location in the static memory segment to a variable name that is unique amongst all of the VM files. Look in Section 7.3.1 and you'll see that they recommend mapping static n in file myFile.vm to the assembly language variable name myFile.n.

As for the second question, what "notes" are you referring to?

The pointer segment is just like the temp segment. Don't make it harder than it is.
Reply | Threaded
Open this post in threaded view
|

Re: A couple of questions about memory segments

ElliotW
Right, I think I understand the static memory segment.  
 
The notes I'm using is on the nand2tetris site, under projects. The relevant slide is slide 81.  
Say RAM[3] had a value of 3000 in it. If I had to push pointer 0, would I add 3 or 3000 to the stack?
Reply | Threaded
Open this post in threaded view
|

Re: A couple of questions about memory segments

WBahn
Administrator
Say RAM[5] currently has 3000 in it and you execute

push temp 0

what value would you push onto the stack?

Reply | Threaded
Open this post in threaded view
|

Re: A couple of questions about memory segments

ElliotW
I think I've got it right, thanks for the clarification