In this unit, we are going to talk about memory segments and memory segment commands. So here once again is our overall VM language that consists of four categories of commands. We already explored the arithmetic logical commands in the previous unit. And now, we are going to move on to memory segment commands. Now let's start with the big picture. We have this arbitrary example of a high level piece of code and we want to translate it or compile it into VM code, something that we'll worry about later on in the course. Now, let's focus on this statement as an example, let c = s1 + y. The compiler will generate or will translate it into push s1, push y, add and pop c. And once again, how the compiler does it is something that we'll worry about later. Now, I want you to notice that something kind of gets lost in the translation. If we look at the high level, we know from our experience and from our knowledge of the high level syntax of this language is that different variables have different roles, right? S for example or s1 is a static variable. And c happens to be, let say c happens to be a local variable, and y is an argument. So these different roles or the variables is something that we also want to preserve of course, in the virtual machine obstruction. If we won't preserve it, things will go wrong in the execution of this program. So we need some mechanism to record the different roles of different variables in one's program. And we do this by introducing the notion of memory segments, which is part of our VM obstruction. So our stack machine is going to be equipped, not with a single memory segment as we had so far. But rather with several memory segments with names like argument, local, static and a few more as you'll see later on. Once we have these segments in place, the compiler can map the variables of the high level on these segments. And in this particular example, we'll get this particular mapping. And once we have this mapping in place, we can use it in order to generate code. So instead of push s1, let's see s1 is a static variable, so instead of saying push s1, we're going to say push static 0. And instead of pushing y, which is argument number 1, we're going to say push argument 1. And instead of saying add, well add is add, there's nothing to add. But instead of saying pop c, we are going to say pop local 2. So we see that once we use the segments in this way we're going to be able to preserve the role semantics of these variables also in the VM obstruction. But something interesting has happened, we lost the variable names in the process. And indeed, the VM obstruction does not recognize symbolic variable names. All the variables are replaced by references to memory segments as we see here. And this is not something which is unique to our VM. Our VM, by the way, is modeled after the JVM, the Java virtual machine, in broad terms, and the JVM also does not have symbolic variables. It only has references to symbolic memory segments. So as you see, we have extended the story and instead of interacting with the single memory stream, we now interact with several different memory segments. To which we also add a segment that we call constant, which is truly a virtual segment, because it contains just the numbers as 0, 1, 2 and so on and so forth. And once you have these segments in place, well the commands that you are going to use, the push and pop are going to take as arguments the name of the segment that you want to operate on and the index in that segment. So here are some examples, push constant 17, push local pop local 2, pop static 5, and so on and so forth. Now, you may ask yourself why do we need push constant 17, why can't we just say push 17? Well we think that it's nicer, that is nice that all the VM commands all the push pop commands will have the same syntax a consistent syntax. And this is a very small price to pay for this consistency. It also makes compilation easier, as we'll see down the road. All right, so in order to make life interesting, we actually have not four, but eight virtual memory segments. Why do we need so many segments? With names like local, argument, this, that, constant, static, pointer, and temp. Well, let us not forget where we are coming from. We are coming from the world of high level object oriented language. And there's a lot going on in high-level object-oriented programming, right? There are static variables, local variables, argument variables. There's the current object that a method is processing, and this object is also a bundle of variables. There's an array that the method can process and so on and so forth. So there's lots of semantics going on and in our virtual machine, we can handle all the semantics using the eight virtual segments that you see here. Now the good news is all these segments behave exactly the same way once we are within the VM obstruction. You push a value into a memory segment by saying push memory segment name and index. And so segment can be any one of these eight segment names and i is simply a non-negative constant. What about pop? Well, it's almost the same with the exception that you cannot pop into the constant segment because it doesn't make any sense. All right, so these are the push and pop commands operating on the memory segments. So here's a little quiz for you to practice what you've learned so far about memory segments. This is just a typical sort of state of a virtual machine with several memory segments present, with some values in them and some values in the stack. And let us assume that we want to carry out the cell door operation static two equals argument one. We don't we can not do something this and this syntax in a VM language. We have to use the available commands that we land so far. Now static 2 happens to be, let see, I'm sorry argument 1 happens to be 12. So in the end of the game we want to see this particular machine state, which is identical to the previous one except that static 2 now contains the value 12. So the question is, which VM commands will make this magic happen? So I want you to think about it for a minute or two, write down the commands. And then continue to run the video and you will see the answer. So here's the answer. In order to move the value of argument 1 to static 2, we must go through the stack. That's the only way to do it in a stack machine, so we push argument 1 onto the stack. And then we pop the topmost value, the value that we have just added onto static 2. And because these two commands kind of cancel each other stackwise, we see that the stack remained the same. A lot of drama happened, right? We added something, we took something away from the stack. But at the end of the game, the stack is intact. And we managed to move something from one segment to the other. And that's the only way to move values from one segment to another in our VM abstruction. So to recap, here again is our VM language. In the previous unit, we talked about the arithmetic logical commands. In this unit, we talked about the memory segment commands, and we are ready to move on and start talking about how we actually implement these commands and make them work for real. This will be done in the next unit.