SimpleAdd.vm, push constant 7

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

SimpleAdd.vm, push constant 7

gs99
First command is: push constant 7.

Page 131: “All memory segments are accessed by the same two commands:
push segment index Push the value of segment[index] onto the stack.”
pop segment index Pop the top stack value and store it in segment[index].”

So then all segment memories are collections, if they have indices?

So then “push constant 7” means push constant[7]?

The VM Emulator doesn’t show the “Constant” segment memory. How do I see what’s in this collection?
How do I see what’s in constant[2]?

Reply | Threaded
Open this post in threaded view
|

Re: SimpleAdd.vm, push constant 7

cadet1620
Administrator
The constant segment is a read-only virtual segment (does not have any memory associated with it); the value of constant[n] is always n. pop constant n is an illegal command.

Arrays are probably a more familiar concept for VM memory segments, rather than collections.

--Mark

Reply | Threaded
Open this post in threaded view
|

Re: SimpleAdd.vm, push constant 7

gs99
Thanks for clarifying that.

The book first says on page 131: “All the memory segments are accessed by the same two commands.”

So the reader is thinking “All the memory segments” when he reads the next two lines that talk about push and pop segment index… (segment[index]).
And concludes wrongfully that they all utilize an index in the normal way an [index] is utilized: to enable an identification of each item or value in a group (whether it’s called a collection or array). The index is not normally the value itself.
   
But you’re saying that the index of memory segment “constant” is not an index, but a value.  
Furthermore, it has nothing to do with memory.
So the effect is simply: push 7 (as we see push 2 on page 128).
But we need to use a “memory segment” to maintain structure; call it “constant”.

Are the memory segments in Figure 7.6 recognized elsewhere, or do they only refer to Hack? If they’re standards, do you know of some books or websites that provide explanations?
Reply | Threaded
Open this post in threaded view
|

Re: SimpleAdd.vm, push constant 7

cadet1620
Administrator
Also note that segment 'static n' does not have a normal index behavior either; it refers to assembly language variable file-name.n where file-name is the name of the .vm file being translated.

This VM language is specific to this course, but similar ideas will be used in any stack based VM.

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

Re: SimpleAdd.vm, push constant 7

sankar.lp.gym
In reply to this post by gs99
Where can i get more information on Hack Assembly code


Java Training in Chennai