Hello and welcome back.
In this lecture, we are going to learn about push and pop.
So let's open our template, second template.
So let's go to our code and let's try the examples of how to use push and pop.
So push basically means that you push something to the stack.
So here is the stack.
Here, a stack is just a part of memory.
So let's type the command push -.
So when you push -, whatever's in - will be pushed to the stack here.
So if we run now and step over.
And execute this.
Watch what happens to the value here.
So let's say we set our - to be one.
Right-click, modify value.
Set it to be one.
And then now I step over.
This one will be pushed to the stack here.
Step over, and you see.
Now you push - to the stack.
So now, now the stack here has got one.
Now let's try another one.
We are going to push our - to the stack.
So before that, let's modify our - by right-clicking it.
Modify it, uh, let's say to two.
So now our - has got two.
We're going to push this value to the stack.
So what command should we use?
Push.
All right.
So now we are going to execute this and watch what happens to the stack.
Step over.
And now the value in - has been pushed to the top of the stack.
So the top of the stack is the latest value being pushed.
The bottom of the stack are the older values.
So you will notice that one was pushed first, and then two is pushed next.
So one came from -, and two came from -.
So what's the usefulness of this?
The usefulness of this is that you can use this stack as a way to save the state of previous data,
so that you can make use of the register for other purposes.
And then later, when you want to restore the original values of the registers, you can pop these values
back to the registers.
So now that we have got a copy of the values - and - inside the stack, we can make use of the -
and - to do anything we want.
So for example, let's do something now.
Let's move, let's move,
uh, the, uh, hex code A.
Two -.
Okay.
So now I'm going to destroy the value in - with something else.
Overwrite it with a new value A.
So I step over that.
And now - has got A.
Now let's also overwrite - for another value called B.
So let's say I did this -, -, B.
And B, and I step over.
And you will see it's been.
Don't worry if it changes to - and -, it is still -.
So now B has overwritten the original value in - previously.
- and - has got one and two in.
Now we overwrite it with A and B.
Okay, so now we are going to restore the original values of - and - from the stack.
So to do that, we use the pop instruction.
So we use the pop instruction.
So we type pop.
So what pop does is it will take whatever is on top of the stack and restore it to whatever register
you specify.
So in this case, we want to restore the value two into -.
So we have to pop -.
So pop means to take whatever's on top of the stack and put it back in this register,
-.
So now let's run this, step over.
And you see now two has got restored back in -.
And now the two has been taken off the top of the stack.
So now the top of the stack becomes the value one.
So the meaning of pop means to take off whatever's on top of the stack.
So now next command is to pop -, because we want to restore this one back to -.
So we should type pop -.
So now let's run this command.
And you see now - became one.
So, and then you notice the stack.
Now the one has been taken off the top of the stack.
So this concept of the stack is like a pancake.
So in a pancake which is stacked, whatever is last in will be the first out.
So that is the concept, is same concept used in the stack, in the memory stack here.
So this is how we can make use of the stack to serve as a temporary storage for data.
And later on, when you want to restore that the register, you can just pop it back to the register.
One thing to note is the order of pop is very important.
If you push - first and - second, then when you pop, you have to pop them in reverse order.
So you need to pop - first, and then you pop -.
If you mess up this order, then what?
What in fact you are doing is to modify, the swap, the values.
So now in the next exercise, I want you to try this.
Write a program to swap values,
- and -.
So give that a try, and then watch the solution in the next lesson.
That's all for this video.
Thank you for watching.