WEBVTT

00:00.610 --> 00:02.229
Hello, this is Dr. Miller, and this

00:02.230 --> 00:03.639
is Episode seven point one of

00:03.640 --> 00:04.640
assembly

00:05.470 --> 00:06.609
today we're going to talk about

00:06.610 --> 00:07.610
local variables.

00:09.310 --> 00:11.019
So local variables are variables

00:11.020 --> 00:12.669
that are used inside of a function

00:12.670 --> 00:14.619
and then the value disappears,

00:14.620 --> 00:16.419
or at least it's not accessible when

00:16.420 --> 00:17.500
the function is finished,

00:18.340 --> 00:19.929
actually. By definition, the value

00:19.930 --> 00:21.204
is still on the stack, but

00:21.940 --> 00:23.829
it is not accessible and we cannot

00:23.830 --> 00:24.830
use it.

00:25.810 --> 00:27.489
So here's an example with several

00:27.490 --> 00:29.156
different local variables, so, for

00:29.320 --> 00:31.420
example, inside of the function FUX,

00:31.870 --> 00:33.549
we have the local variable sum

00:33.760 --> 00:34.869
and the local variable.

00:34.870 --> 00:35.870
I while

00:36.700 --> 00:38.409
in the main function we have a local

00:38.410 --> 00:40.329
variable called Y and

00:40.330 --> 00:41.950
a local variable called X,

00:42.550 --> 00:43.929
and these are going to be used

00:43.930 --> 00:45.106
inside of the functions.

00:45.130 --> 00:47.109
And so programmers will often use

00:47.110 --> 00:48.459
them when they are programing

00:48.790 --> 00:50.619
programing in a high level language.

00:51.340 --> 00:53.350
Now that's going to get lost.

00:53.380 --> 00:55.149
Those names will be lost, but we'll

00:55.150 --> 00:56.799
be able to see it when we reverse

00:56.800 --> 00:57.880
engineer look at

00:58.620 --> 00:59.979
a high level language that gets

00:59.980 --> 01:01.509
compiled down and that'll give us a

01:01.510 --> 01:03.009
good example of how we're going to

01:03.010 --> 01:04.010
be able to use them.

01:05.930 --> 01:07.939
So local variables used the EBP

01:07.940 --> 01:10.099
register and then the first variable

01:10.100 --> 01:11.689
is that EBP minus four,

01:12.290 --> 01:13.290
and so SBP points

01:14.180 --> 01:15.790
to the old value of SBP.

01:15.800 --> 01:17.719
So if we go down EBP minus

01:17.720 --> 01:19.279
four is going to be our first local

01:19.280 --> 01:20.897
variable and then our second will

01:21.170 --> 01:22.346
be at eight and then 12,

01:23.060 --> 01:25.220
16 multiples of four.

01:27.250 --> 01:29.019
So if you're going to allocate them,

01:29.020 --> 01:30.819
you can say sub hesp,

01:31.150 --> 01:33.099
where Hesp will point to SBP

01:33.550 --> 01:34.971
and then if you have X number

01:35.710 --> 01:37.359
of local variables, you take X

01:37.360 --> 01:38.977
times. For now, I'll give you the

01:39.250 --> 01:41.169
amount. So, for example, if

01:41.170 --> 01:42.640
you have 10 variables,

01:43.090 --> 01:45.340
then you'd say sub VSP

01:46.210 --> 01:48.340
four times 10, which should be 40,

01:48.820 --> 01:50.169
or if you just have one, you just

01:50.170 --> 01:51.170
subtract four.

01:51.910 --> 01:53.229
And so that's how you're going to do

01:53.230 --> 01:54.510
it inside of our program.

01:56.760 --> 01:58.169
Here's another example, so if we

01:58.170 --> 01:59.969
have a local variable, we can

01:59.970 --> 02:00.970
increment that.

02:01.230 --> 02:02.849
Now, we can't increment it directly

02:02.850 --> 02:04.709
in memory and so we have to copy the

02:04.710 --> 02:05.710
value from the

02:06.540 --> 02:07.319
local variable.

02:07.320 --> 02:09.569
So EBP minus four into a register,

02:09.570 --> 02:10.570
say X and then

02:11.520 --> 02:13.529
increment that and then save

02:13.530 --> 02:15.779
that value back onto the stack.

02:15.990 --> 02:17.689
And so that's the equivalent of like

02:17.700 --> 02:18.974
a high level language like

02:19.650 --> 02:21.479
C or C++ or Java

02:21.480 --> 02:22.852
to the single command X plus

02:23.310 --> 02:24.682
plus. So just to increment a

02:24.870 --> 02:25.979
variable that we have.

02:28.230 --> 02:29.759
So here's a couple of examples, if I

02:29.760 --> 02:31.328
have X equals zero, Y equals one

02:31.800 --> 02:33.466
and Z equals two in the high level

02:33.720 --> 02:35.639
language of C, if

02:35.640 --> 02:37.169
I get that into assembly, I have

02:37.170 --> 02:38.310
three variables

02:39.120 --> 02:41.009
times four gives me 12,

02:41.010 --> 02:42.689
so I need to subtract 12

02:43.230 --> 02:45.449
and then I can move zero into X,

02:45.450 --> 02:47.530
one into Y and two into Z.

02:48.090 --> 02:49.259
And so we have the notion of

02:49.260 --> 02:51.690
thinking of them as just offsets.

02:51.780 --> 02:53.729
And so X is offset for minus

02:53.730 --> 02:55.680
four, Y is minus eight

02:55.980 --> 02:57.949
and Z is minus twelve.

02:58.680 --> 03:00.299
And then since we did a subtract, we

03:00.300 --> 03:01.799
have to do an add at the end to

03:01.800 --> 03:02.800
correct our stack.

03:05.190 --> 03:06.599
Now, here we take the same thing and

03:06.600 --> 03:08.069
we're going to do X is equal to Y

03:08.070 --> 03:09.070
plus Z.

03:10.010 --> 03:11.959
And so we set our initial values

03:11.960 --> 03:13.759
here at the beginning, we still

03:13.760 --> 03:14.789
subtract 12, but then

03:15.680 --> 03:17.003
we might want to move in to

03:17.690 --> 03:19.969
X, X

03:19.970 --> 03:21.949
and then into X Y

03:21.950 --> 03:23.899
and then add those two two together

03:24.230 --> 03:25.520
and then store them at Z.

03:26.000 --> 03:27.109
I may have done that a little

03:27.110 --> 03:28.110
backwards.

03:28.190 --> 03:29.900
Saw them in X, so we load

03:30.500 --> 03:32.479
Y and Z and then store them

03:32.480 --> 03:33.480
back in X.

03:34.760 --> 03:35.936
So we can see how we can

03:36.590 --> 03:38.207
manipulate something that is in a

03:38.270 --> 03:40.489
local variable and

03:40.490 --> 03:42.169
we just have to be able to do our

03:42.170 --> 03:43.787
operations on that locally inside

03:44.270 --> 03:45.889
of a register, and then we can save

03:45.890 --> 03:47.210
it back onto the stack.

03:47.630 --> 03:49.180
And that's where our local variables

03:49.210 --> 03:50.210
live.

03:52.360 --> 03:54.039
So the example program that we gave

03:54.040 --> 03:55.657
earlier where we can compile that

03:55.960 --> 03:57.283
with GCC minus M thirty two

03:58.060 --> 03:59.590
so that it is thirty two bit

04:00.070 --> 04:02.060
and the name of our file is test

04:02.140 --> 04:03.708
C in my example, you can call it

04:04.030 --> 04:06.009
whatever you want and

04:06.010 --> 04:07.725
then if you don't give it an output

04:07.990 --> 04:09.899
file it by default creates Aidar

04:09.940 --> 04:11.110
out on Linux.

04:12.150 --> 04:13.800
And so then we can do a

04:14.190 --> 04:15.709
object down, put to show us the

04:16.079 --> 04:17.794
assembly version of that, and we'll

04:17.880 --> 04:19.229
look at that hearing a little bit.

04:20.870 --> 04:21.870
So, for example,

04:22.850 --> 04:24.829
if we have our function called thoo,

04:24.950 --> 04:26.929
if we look at a disassembly of

04:26.930 --> 04:28.008
that, we will see that

04:29.180 --> 04:30.699
we got zero going to the end of

04:31.040 --> 04:32.899
our eight zero

04:32.900 --> 04:34.200
going into VARE four.

04:34.220 --> 04:35.699
It's hard to tell because both some

04:35.720 --> 04:37.970
starts at zero and I start to zero.

04:39.090 --> 04:40.854
But if we look inside of here, we'll

04:41.070 --> 04:42.899
see that we're adding to vare

04:42.900 --> 04:44.970
for one each time,

04:45.540 --> 04:47.279
whereas the sum looks like it's

04:47.280 --> 04:48.949
going into bar eight.

04:48.960 --> 04:51.119
So we're loading the value

04:51.120 --> 04:52.949
of AI

04:53.100 --> 04:54.619
and then adding it to what's in

04:55.290 --> 04:56.290
bar eight.

04:56.550 --> 04:58.079
And then you can see at the bottom

04:58.080 --> 04:59.910
here we see Move X,

05:00.240 --> 05:02.069
SBP plus VARE eight.

05:02.280 --> 05:04.199
And so that's going to copy the

05:04.200 --> 05:05.719
value some into X, which is the

05:06.060 --> 05:07.199
return register.

05:07.210 --> 05:08.939
And then we were seeing our return

05:08.940 --> 05:09.940
at the end.

05:11.220 --> 05:12.837
And so we can see that there will

05:13.050 --> 05:14.050
be a clear.

05:16.850 --> 05:17.977
Definition of what gets

05:18.680 --> 05:20.750
produced in here based on

05:20.780 --> 05:22.279
the source code and so there's a

05:22.280 --> 05:23.779
mapping that goes from source code

05:23.780 --> 05:25.348
to assembly, but as you can see,

05:25.790 --> 05:27.015
I gave that nice names in

05:27.620 --> 05:29.360
here, like some and I

05:29.780 --> 05:30.907
and those all got lost.

05:31.100 --> 05:33.110
And so all we ended up with is a

05:33.380 --> 05:34.380
variable name.

05:39.270 --> 05:40.642
So today, we discussed local

05:40.860 --> 05:41.860
variables

05:42.690 --> 05:43.829
and in the future, we're going to

05:43.830 --> 05:45.629
give a local variable example, so

05:45.630 --> 05:47.189
we'll start off and we'll build an

05:47.190 --> 05:49.019
entire program and

05:49.020 --> 05:50.369
we'll basically implement from

05:50.370 --> 05:52.049
scratch using local variables.

05:52.620 --> 05:53.819
And then in a future lecture, we'll

05:53.820 --> 05:55.290
talk about the enter and leave

05:55.320 --> 05:56.320
instructions.

05:58.350 --> 06:00.089
So the quiz the question is, what is

06:00.090 --> 06:01.889
the offset of the third parameter?

06:02.820 --> 06:05.039
That we can use our local variable

06:05.790 --> 06:07.559
and that is zero XY

06:08.010 --> 06:09.899
and it's actually negative zero

06:09.910 --> 06:10.910
XY, right, because

06:11.850 --> 06:13.169
we're going below the stack.

06:14.640 --> 06:15.899
So if you have questions, you can

06:15.900 --> 06:17.272
contact me at Miller, MJ, at

06:17.730 --> 06:19.769
you and Keda, Edu and on Twitter

06:19.800 --> 06:20.910
at has 30.

