Where to map lcl, arg, this, that

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

Where to map lcl, arg, this, that

Mark
Hello,

Just a couple general questions regarding the mapping of the segments LCL, ARG, etc.

I'm looking at the example BasicTestVME.tst, and notice that, for example, ARG is mapped to RAM location 400. Is this mapping choice arbitrary? In the specification, it says that RAM addresses 256-2047 are reserved for the stack, so are you not writing ARG onto the stack (albeit much higher up than the stack would ever reach)? Is this a good practice?

Another source of confusion, THIS is mapped to 3000, and THAT to 3010. Wouldn't this arrangement require that no more than 10 words be allocated to THIS before parts of THAT is overwritten?

I guess it's up to the programmer to decide where these segments start on the RAM, and how much space to allocate each?

Thanks in advance for clearing this up!

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

Re: Where to map lcl, arg, this, that

cadet1620
Administrator
Mark wrote
I'm looking at the example BasicTestVME.tst, and notice that, for example, ARG is mapped to RAM location 400. Is this mapping choice arbitrary? In the specification, it says that RAM addresses 256-2047 are reserved for the stack, so are you not writing ARG onto the stack (albeit much higher up than the stack would ever reach)? Is this a good practice?
The values of ARG, LCL, THIS and THAT are arbitrary, although they have conventional uses by the compiler you will write in projects 10 and 11. As far as the VM translator is concerned, they are simply memory addresses that specify the beginning of the VM segments argument, local, this and that.
Another source of confusion, THIS is mapped to 3000, and THAT to 3010. Wouldn't this arrangement require that no more than 10 words be allocated to THIS before parts of THAT is overwritten?
This is just a test setup, so there really aren't any semantics to what THIS and THAT point to. If this situation was seen in compiler generated code, it's likely THIS is pointing to an object that is no larger that 10 words, or that THIS is pointing to an Array object and THAT is pointing to one of the Array's members.
I guess it's up to the programmer to decide where these segments start on the RAM, and how much space to allocate each?
In general, only the compiler writes programs in VM language. It uses ARG and LCL to access function arguments and local variables; THIS points to the current object (like the this pointer in C++ and Java), and THAT is used for array indexing.

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

Re: Where to map lcl, arg, this, that

Mark
Much thanks, this clears up many of my questions, in particular about the THIS and THAT segments.

One thing I'm still not totally clear on though is why the LOCAL and ARGUMENT segments are mapped onto the the same RAM that is reserved for the stack as outlined in the Ch 7 notes (or perhaps I'm just understanding something).

The RAM usage table in section 7.3.1 specifies that addresses 256-2047 be used for the stack, and 2048-16483 be used for the heap. After running the BasicTestVME.tst script in the VM Emulator, RAM location 1 (accessed by the LCL symbol in Hack assembly language) is initialized with the value 300, which means the local segment begins at RAM location 300, which is part of the RAM allocation for the stack, correct? Would this not make the Local segment vulnerable to overwriting by the stack?

I understand that this is just an example, locations are arbitrarily chosen by the compiler (which is written by a programmer), etc. I'm sorry if I'm being pedantic, just want to make sure I have everything straight :)

Thanks again!

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

Re: Where to map lcl, arg, this, that

cadet1620
Administrator
The argument and local segments are supposed to be within the stack by design.

Look ahead to figure 8.4. It shows a stack frame and how argument and local are used to access function arguments and function local variables within the frame. As part of the code that your VM translator will emit for function calling, ARG and LCL will be set as shown.

In normal usage, ARG < LCL <= SP so the working stack won't overwrite the active stack frame. (LCL == SP if the function has no local variables.)

--Mark



Reply | Threaded
Open this post in threaded view
|

Re: Where to map lcl, arg, this, that

Mark
I see. Makes sense now.

Much thanks for the clarification!