WEBVTT

00:02.040 --> 00:03.569
Hello, this is Dr. Miller, and this

00:03.570 --> 00:05.189
is Episode six point two of

00:05.190 --> 00:06.190
Assembly.

00:09.160 --> 00:10.389
Today, we're going to talk about the

00:10.390 --> 00:11.390
stack.

00:12.210 --> 00:13.386
So we have the stick, so

00:14.100 --> 00:15.864
the stack is a region of Rampone and

00:15.960 --> 00:18.170
by a register called S.B,

00:18.330 --> 00:19.709
the extended stack pointer,

00:20.580 --> 00:22.229
and then with the stack we can push

00:22.230 --> 00:24.329
things onto and pop things off

00:24.330 --> 00:25.169
of the stack.

00:25.170 --> 00:26.836
So we'll have operations like push

00:26.970 --> 00:27.970
and pop.

00:28.650 --> 00:30.022
And then the stack is a last

00:30.780 --> 00:32.310
in, first out

00:32.670 --> 00:35.130
type of operation and type of usage.

00:36.080 --> 00:38.149
And we also have the SS register, so

00:38.450 --> 00:40.219
it is pointing to where stock memory

00:40.220 --> 00:41.569
is, but you don't actually have to

00:41.570 --> 00:43.070
even know that in general.

00:43.970 --> 00:45.469
And so examples are pushier,

00:45.890 --> 00:48.049
push, Ebix pop, Ebix,

00:48.440 --> 00:50.389
add to E.S.P or subtract

00:50.390 --> 00:51.390
from ESPE.

00:53.260 --> 00:54.579
And then what do we use it for?

00:55.000 --> 00:56.559
Mostly the stack is going to be used

00:56.560 --> 00:58.079
for function calls, we can also

00:58.570 --> 01:00.340
use it to store local variables,

01:00.550 --> 01:02.409
parameters, and then

01:02.410 --> 01:03.609
for a function that allows the

01:03.610 --> 01:04.870
context to be saved.

01:05.500 --> 01:06.819
And then we also will use it for a

01:06.820 --> 01:07.959
temporary space.

01:09.650 --> 01:11.414
So here are some example operations,

01:11.840 --> 01:13.489
so we can have push X

01:14.090 --> 01:15.462
where we take the ESP and we

01:15.860 --> 01:18.019
subtract the way that memory

01:18.020 --> 01:19.879
will be drawn as we draw

01:19.880 --> 01:20.880
the low memory

01:21.800 --> 01:22.800
addresses on up, and

01:23.930 --> 01:25.939
we'll draw it like a a set

01:25.940 --> 01:27.919
of plates where the top thing

01:27.920 --> 01:28.819
goes on the top.

01:28.820 --> 01:30.859
When we say push or when we say pop,

01:30.860 --> 01:31.879
it'll come up from the top.

01:32.450 --> 01:34.099
But technically it subtracts four

01:34.100 --> 01:35.689
from the ESP register

01:36.320 --> 01:37.969
and then it'll copy the data pointed

01:37.970 --> 01:39.379
to by ESP.

01:39.410 --> 01:41.299
So it'll copy that data onto the top

01:41.300 --> 01:42.427
of the stack and then X

01:43.340 --> 01:45.104
is either a register or an immediate

01:45.410 --> 01:47.169
value or a hard coded value,

01:47.960 --> 01:50.329
and then Pop will add four to Espe

01:50.330 --> 01:51.799
and then say whatever is on top of

01:51.800 --> 01:53.466
the stack into the register that's

01:53.480 --> 01:55.049
given. So there'll be a register

01:55.050 --> 01:56.050
that will be given here.

01:57.680 --> 01:58.790
So here's an example.

01:59.240 --> 02:00.769
So we have our original stack, so it

02:00.770 --> 02:01.639
has some stuff on it.

02:01.640 --> 02:02.809
We don't really know or care what

02:02.810 --> 02:03.810
that is.

02:03.830 --> 02:05.209
And then we move in, just register

02:05.210 --> 02:07.420
some values. So 10 goes into X,

02:07.430 --> 02:09.096
twenty goes into Ebix, thirty goes

02:09.289 --> 02:10.289
in X, and then we're

02:11.180 --> 02:12.469
going to execute this instruction

02:12.470 --> 02:14.419
here. So we're going to push X

02:14.420 --> 02:15.289
on top of the stack.

02:15.290 --> 02:16.466
So X has a value of ten.

02:17.210 --> 02:19.069
So that's now going to go on top

02:19.070 --> 02:20.070
of the stack.

02:20.940 --> 02:22.229
So we can see the top of the stack

02:22.230 --> 02:23.700
points to the number 10 and we

02:23.770 --> 02:24.899
again, we have all the original

02:24.900 --> 02:26.517
stuff, so now we're going to push

02:26.910 --> 02:29.069
X or X has a value

02:29.070 --> 02:30.070
of thirty.

02:30.630 --> 02:32.345
So we can see that that now goes on

02:32.400 --> 02:33.400
top of the stack.

02:34.360 --> 02:36.249
The next instruction, push SBX,

02:36.250 --> 02:38.199
so now Ebix is going to go on top of

02:38.200 --> 02:39.200
our stack.

02:40.030 --> 02:41.349
So you can see a copy of the value

02:41.350 --> 02:42.819
of 20 on top of the stack,

02:43.910 --> 02:45.489
and so Bush added things onto the

02:45.490 --> 02:47.254
stack and then papa is going to take

02:47.350 --> 02:49.810
stuff off. So we say pop X

02:50.350 --> 02:51.771
now, X should get whatever is

02:52.120 --> 02:53.620
pointed to at the top of the stack.

02:53.630 --> 02:55.810
So it had 10 and now it should have

02:56.440 --> 02:57.440
20.

02:57.700 --> 02:58.729
X is 20.

02:59.050 --> 03:00.309
That thing is removed from the

03:00.310 --> 03:02.139
stack. So now we still have 30

03:02.140 --> 03:03.719
and 10 on top of our stack

03:04.510 --> 03:06.550
and now we're going to say pop X,

03:06.580 --> 03:08.019
so we'll take that off of the top of

03:08.020 --> 03:09.020
the stack.

03:09.050 --> 03:11.229
So now X is still 20, but X

03:11.230 --> 03:12.459
gets the value that was on the

03:12.460 --> 03:13.832
stack, which was 30, and now

03:14.380 --> 03:15.759
X again is 30.

03:15.760 --> 03:16.779
It had 30 before.

03:18.210 --> 03:19.319
So that's an example.

03:21.450 --> 03:23.214
Additionally, we have the operations

03:23.460 --> 03:25.910
like call we can call a functioning,

03:26.070 --> 03:27.540
so what call does is it pushes

03:27.930 --> 03:29.519
the address of the next instruction

03:29.520 --> 03:30.647
onto the stack and then

03:31.560 --> 03:33.479
it copies IP to that new

03:33.480 --> 03:35.550
function, location or address.

03:36.460 --> 03:38.199
And then the reverse of that is the

03:38.200 --> 03:39.572
return to return is going to

03:40.030 --> 03:41.649
allow us to exit a function

03:42.100 --> 03:43.639
so or return from a function.

03:44.230 --> 03:45.994
So what that does is it pops off the

03:46.030 --> 03:47.647
return address from the stack and

03:47.860 --> 03:49.085
then moves IP to that new

03:49.810 --> 03:50.810
address.

03:51.040 --> 03:52.657
So we end up always having a call

03:53.020 --> 03:54.489
to a function and then inside the

03:54.490 --> 03:56.109
function when it's done, we'll have

03:56.110 --> 03:57.110
a return.

03:57.130 --> 03:58.540
And so that's how we can write a

03:58.660 --> 04:00.009
program with functions.

04:00.400 --> 04:01.879
Now, you've done this in class.

04:01.930 --> 04:03.759
We have called a lot of different

04:03.760 --> 04:04.749
functions.

04:04.750 --> 04:06.560
So, for example, call, read in,

04:06.610 --> 04:08.620
call, print and call, print and tell

04:08.950 --> 04:10.419
all of those we are calling a

04:10.420 --> 04:11.799
function and then we assume that

04:11.800 --> 04:13.221
it's going to resume from the

04:13.540 --> 04:14.716
location that we were at

04:15.370 --> 04:16.370
before.

04:18.500 --> 04:20.809
We also have the ability to allocate

04:20.810 --> 04:23.209
space using add and subtract.

04:23.720 --> 04:26.029
So, for example, we can say sub hesp

04:26.060 --> 04:27.628
X and then I'll subtract X bytes

04:28.160 --> 04:29.160
from ESP.

04:29.570 --> 04:30.799
So typically when you enter a

04:30.800 --> 04:32.179
function, you're going to do a

04:32.180 --> 04:34.009
subtract on ESP to allocate

04:34.010 --> 04:35.774
some temporary space that we can use

04:35.960 --> 04:37.909
for local variables or buffer's.

04:38.780 --> 04:40.279
And then at the end of the function

04:40.280 --> 04:41.407
we can add back ESP, so

04:42.290 --> 04:44.054
we'll typically add and subtract the

04:44.150 --> 04:45.440
same number of bytes.

04:46.070 --> 04:48.079
And so this will allocate that space

04:48.080 --> 04:49.697
and we'll do that right before we

04:49.730 --> 04:50.730
leave a function.

04:51.320 --> 04:53.035
And then if you ever go through and

04:53.060 --> 04:54.499
you are reverse engineering some

04:54.500 --> 04:56.019
code, you might see that when a

04:56.270 --> 04:57.499
function enters, it might do

04:57.500 --> 04:58.774
something like and ESP and

04:59.450 --> 05:01.069
then you've got a bunch of F's and a

05:01.070 --> 05:02.070
zero.

05:02.240 --> 05:03.949
And this is because it is trying to

05:03.950 --> 05:06.049
make sure that E.S.P is on an even

05:06.050 --> 05:08.269
boundary and this is a security

05:08.270 --> 05:09.740
protection that some operating

05:09.860 --> 05:11.624
systems and some compilers implement

05:11.870 --> 05:13.729
to make it harder to attack

05:13.730 --> 05:14.869
these type of systems.

05:17.560 --> 05:19.119
And then we have a couple of special

05:19.120 --> 05:20.688
operations like push aid and pop

05:20.830 --> 05:21.957
aid, and so push aid is

05:22.810 --> 05:24.476
going to push all of the registers

05:24.610 --> 05:26.529
onto the stack, but it pushes

05:26.530 --> 05:28.059
them in kind of a weird order,

05:28.090 --> 05:29.779
right? So goes X, X,

05:30.480 --> 05:32.139
SBX, the

05:32.350 --> 05:34.269
value of Hesp before we did

05:34.270 --> 05:36.730
the Push Aid SBP Essi

05:36.760 --> 05:37.789
Eddi and then the pop

05:38.740 --> 05:40.406
aid is going to do them completely

05:40.600 --> 05:41.949
in reverse order and then the

05:41.950 --> 05:43.371
original hesp is going to get

05:43.720 --> 05:44.994
restored their hesp before

05:45.610 --> 05:47.140
the the pop

05:47.440 --> 05:48.879
operation occurred.

05:51.410 --> 05:53.360
So today, we talked about the stack.

05:54.630 --> 05:55.708
So here's our quiz, so

05:56.460 --> 05:57.839
what is the following code do?

05:59.210 --> 06:00.409
So go ahead and think about that,

06:00.410 --> 06:01.699
because electricity, if you

06:01.700 --> 06:03.079
understand what you think it does.

06:07.020 --> 06:08.369
All right, so we had our original

06:08.370 --> 06:10.019
stack, we pushed on

06:10.590 --> 06:12.029
and then pushed on SBX,

06:12.510 --> 06:14.459
so you had the number thirty three,

06:14.490 --> 06:16.100
Ebix had the number 13.

06:16.110 --> 06:17.776
So when we got it, 13 is on top of

06:17.850 --> 06:18.850
the stack.

06:20.000 --> 06:21.079
Here's another question.

06:22.140 --> 06:24.359
So at the end, what is an X

06:24.360 --> 06:25.709
and what is an X?

06:30.390 --> 06:32.249
All right, so Yak's has 12 and

06:32.250 --> 06:33.420
Ebix has 12, right?

06:33.450 --> 06:35.010
They got both got pushed on their

06:35.650 --> 06:37.218
right. So well, sorry, we pushed

06:37.470 --> 06:39.209
Yak's on there and then we popped

06:39.210 --> 06:40.260
SBX off.

06:40.280 --> 06:42.089
Right. SBX got the value that was

06:42.090 --> 06:43.519
copied on top of the stack.

06:44.130 --> 06:45.747
So now Ebix has the same value as

06:45.990 --> 06:46.990
X.

06:48.890 --> 06:50.389
So looking forward, we are going to

06:50.390 --> 06:51.860
talk about using the stack,

06:52.940 --> 06:54.379
and if you have questions, you can

06:54.380 --> 06:56.449
contact me at Miller, MJ, at UNCHR,

06:56.450 --> 06:58.399
Edu, and you can find me on Twitter

06:58.400 --> 06:59.510
at know how sturdy.

