WEBVTT

1
00:01.850 --> 00:03.320
Hello and welcome back.

2
00:03.320 --> 00:07.040
In this lecture, we are going to learn about push and pop.

3
00:07.400 --> 00:11.120
So let's open our template, second template.

4
00:13.530 --> 00:21.750
So let's go to our code and let's try the examples of how to use push and pop.

5
00:22.980 --> 00:27.630
So push basically means that you push something to the stack.

6
00:27.750 --> 00:28.980
So here is the stack.

7
00:28.980 --> 00:31.530
Here, a stack is just a part of memory.

8
00:32.220 --> 00:38.580
So let's type the command push -.

9
00:39.840 --> 00:45.420
So when you push -, whatever's in - will be pushed to the stack here.

10
00:46.110 --> 00:49.380
So if we run now and step over.

11
00:50.550 --> 00:52.080
And execute this.

12
00:52.560 --> 00:56.790
Watch what happens to the value here.

13
00:56.790 --> 00:59.160
So let's say we set our - to be one.

14
00:59.160 --> 01:00.900
Right-click, modify value.

15
01:01.680 --> 01:03.120
Set it to be one.

16
01:05.690 --> 01:07.400
And then now I step over.

17
01:07.400 --> 01:09.590
This one will be pushed to the stack here.

18
01:10.430 --> 01:11.990
Step over, and you see.

19
01:11.990 --> 01:15.230
Now you push - to the stack.

20
01:15.230 --> 01:17.870
So now, now the stack here has got one.

21
01:18.980 --> 01:20.480
Now let's try another one.

22
01:20.720 --> 01:22.790
We are going to push our - to the stack.

23
01:22.790 --> 01:26.330
So before that, let's modify our - by right-clicking it.

24
01:26.690 --> 01:31.280
Modify it, uh, let's say to two.

25
01:31.970 --> 01:33.650
So now our - has got two.

26
01:33.680 --> 01:36.620
We're going to push this value to the stack.

27
01:36.620 --> 01:38.600
So what command should we use?

28
01:39.800 --> 01:40.220
Push.

29
01:43.250 --> 01:43.520
All right.

30
01:43.520 --> 01:46.940
So now we are going to execute this and watch what happens to the stack.

31
01:47.390 --> 01:48.410
Step over.

32
01:48.980 --> 01:53.180
And now the value in - has been pushed to the top of the stack.

33
01:53.600 --> 01:57.800
So the top of the stack is the latest value being pushed.

34
01:58.010 --> 02:00.290
The bottom of the stack are the older values.

35
02:00.290 --> 02:06.620
So you will notice that one was pushed first, and then two is pushed next.

36
02:06.620 --> 02:10.370
So one came from -, and two came from -.

37
02:11.150 --> 02:13.160
So what's the usefulness of this?

38
02:13.190 --> 02:19.520
The usefulness of this is that you can use this stack as a way to save the state of previous data,

39
02:20.150 --> 02:23.810
so that you can make use of the register for other purposes.

40
02:23.810 --> 02:29.870
And then later, when you want to restore the original values of the registers, you can pop these values

41
02:29.870 --> 02:31.280
back to the registers.

42
02:31.310 --> 02:41.660
So now that we have got a copy of the values - and - inside the stack, we can make use of the -

43
02:41.660 --> 02:43.640
and - to do anything we want.

44
02:43.730 --> 02:46.100
So for example, let's do something now.

45
02:46.100 --> 02:48.230
Let's move, let's move,

46
02:48.230 --> 02:52.820
uh, the, uh, hex code A.

47
02:54.680 --> 02:55.580
Two -.

48
03:00.730 --> 03:01.420
Okay.

49
03:01.420 --> 03:07.450
So now I'm going to destroy the value in - with something else.

50
03:07.480 --> 03:09.610
Overwrite it with a new value A.

51
03:09.610 --> 03:10.660
So I step over that.

52
03:10.660 --> 03:12.610
And now - has got A.

53
03:12.910 --> 03:17.500
Now let's also overwrite - for another value called B.

54
03:17.890 --> 03:20.890
So let's say I did this -, -, B.

55
03:23.450 --> 03:26.510
And B, and I step over.

56
03:28.750 --> 03:29.950
And you will see it's been.

57
03:30.070 --> 03:34.030
Don't worry if it changes to - and -, it is still -.

58
03:35.050 --> 03:39.100
So now B has overwritten the original value in - previously.

59
03:39.130 --> 03:42.460
- and - has got one and two in.

60
03:42.640 --> 03:44.500
Now we overwrite it with A and B.

61
03:45.100 --> 03:51.250
Okay, so now we are going to restore the original values of - and - from the stack.

62
03:51.250 --> 03:53.500
So to do that, we use the pop instruction.

63
03:53.860 --> 03:55.360
So we use the pop instruction.

64
03:55.360 --> 03:56.440
So we type pop.

65
03:57.610 --> 04:04.720
So what pop does is it will take whatever is on top of the stack and restore it to whatever register

66
04:04.720 --> 04:05.560
you specify.

67
04:05.590 --> 04:11.020
So in this case, we want to restore the value two into -.

68
04:11.200 --> 04:13.840
So we have to pop -.

69
04:14.110 --> 04:20.140
So pop means to take whatever's on top of the stack and put it back in this register,

70
04:20.170 --> 04:21.070
-.

71
04:21.850 --> 04:24.010
So now let's run this, step over.

72
04:25.000 --> 04:29.020
And you see now two has got restored back in -.

73
04:29.050 --> 04:33.010
And now the two has been taken off the top of the stack.

74
04:33.010 --> 04:37.240
So now the top of the stack becomes the value one.

75
04:37.270 --> 04:40.900
So the meaning of pop means to take off whatever's on top of the stack.

76
04:41.530 --> 04:49.600
So now next command is to pop -, because we want to restore this one back to -.

77
04:49.630 --> 04:53.050
So we should type pop -.

78
04:54.650 --> 04:56.720
So now let's run this command.

79
04:56.840 --> 04:59.660
And you see now - became one.

80
04:59.930 --> 05:02.210
So, and then you notice the stack.

81
05:02.210 --> 05:04.940
Now the one has been taken off the top of the stack.

82
05:05.270 --> 05:09.680
So this concept of the stack is like a pancake.

83
05:09.860 --> 05:16.070
So in a pancake which is stacked, whatever is last in will be the first out.

84
05:16.490 --> 05:21.410
So that is the concept, is same concept used in the stack, in the memory stack here.

85
05:21.740 --> 05:28.040
So this is how we can make use of the stack to serve as a temporary storage for data.

86
05:28.040 --> 05:33.830
And later on, when you want to restore that the register, you can just pop it back to the register.

87
05:33.920 --> 05:37.730
One thing to note is the order of pop is very important.

88
05:37.760 --> 05:43.580
If you push - first and - second, then when you pop, you have to pop them in reverse order.

89
05:43.580 --> 05:46.820
So you need to pop - first, and then you pop -.

90
05:46.850 --> 05:49.700
If you mess up this order, then what?

91
05:49.850 --> 05:54.890
What in fact you are doing is to modify, the swap, the values.

92
05:54.890 --> 05:57.860
So now in the next exercise, I want you to try this.

93
05:57.980 --> 06:00.500
Write a program to swap values,

94
06:00.680 --> 06:01.850
- and -.

95
06:01.940 --> 06:07.340
So give that a try, and then watch the solution in the next lesson.

96
06:07.430 --> 06:09.020
That's all for this video.

97
06:09.020 --> 06:10.490
Thank you for watching.