WEBVTT

00:01.550 --> 00:02.959
Hello, this is Dr. Miller, and this

00:02.960 --> 00:04.609
is Episode seven point two of

00:04.610 --> 00:05.610
Assembly

00:06.620 --> 00:08.359
today, we're going to get to get an

00:08.360 --> 00:10.579
example of using local variables.

00:12.490 --> 00:14.058
So for our example, we are going

00:14.440 --> 00:16.204
to write a loop that's going to some

00:16.420 --> 00:18.279
a bunch of numbers and

00:18.280 --> 00:20.044
then inside of our main, we're going

00:20.290 --> 00:22.229
to use local variables also.

00:23.920 --> 00:25.719
So the example is here in C,

00:25.870 --> 00:27.429
and so we have, again, this function

00:27.430 --> 00:28.430
called FUX that has

00:29.320 --> 00:31.119
a sum. It adds up all the numbers

00:31.120 --> 00:32.740
from zero to ND,

00:33.730 --> 00:34.899
adds them to the sum and then

00:34.900 --> 00:36.609
returns it. And then inside of our

00:36.610 --> 00:38.325
main we're going to have some local

00:38.740 --> 00:40.420
variables. So we'll go ahead and

00:41.080 --> 00:42.399
start a project here.

00:52.620 --> 00:53.549
All right, so we're going to need a

00:53.550 --> 00:55.289
couple of different variables, so in

00:55.290 --> 00:57.149
the previous example, we saw that we

00:57.150 --> 00:58.179
had ganef and printf.

00:59.250 --> 01:00.359
So we're going to define some

01:00.360 --> 01:01.529
variables here at the top.

01:03.570 --> 01:05.099
This is the SCANA format

01:05.519 --> 01:07.379
with a bunch of bytes and it is

01:07.380 --> 01:08.480
a precent D

01:09.480 --> 01:10.869
terminate with a zero.

01:12.720 --> 01:14.069
Here's our print format.

01:21.030 --> 01:23.069
I'll go ahead and put a new line and

01:23.070 --> 01:24.120
terminate with zero,

01:25.350 --> 01:26.549
and then we're going to be using the

01:26.550 --> 01:28.559
functions, print F and scan

01:28.560 --> 01:29.560
F.

01:33.900 --> 01:35.219
So we'll just declare those as

01:35.220 --> 01:36.220
external

01:39.000 --> 01:40.079
and then instead of our main

01:40.080 --> 01:41.459
program, we're going to use a local

01:41.460 --> 01:43.769
variable for the value of the scarf.

01:44.520 --> 01:46.439
So to allocate space,

01:46.440 --> 01:48.389
we need to subtract from Hesp

01:48.390 --> 01:50.489
in order to give room.

01:56.710 --> 01:58.240
And then we're going to want to

01:58.900 --> 02:01.209
load the address of that variable,

02:02.260 --> 02:03.429
so we'll go ahead and load that

02:03.430 --> 02:04.479
anti-tax.

02:10.990 --> 02:12.849
And Leah is going to grab the

02:12.850 --> 02:15.279
address of the value at E.S.P,

02:15.400 --> 02:16.968
and so it'll just copy that into

02:17.080 --> 02:18.080
their.

02:19.140 --> 02:20.609
And then we need to push that onto

02:20.610 --> 02:21.982
the stack before we call our

02:22.500 --> 02:23.500
function,

02:24.900 --> 02:25.900
and then we also

02:27.090 --> 02:29.450
have to put on the format so

02:33.250 --> 02:34.669
we go ahead and push that.

02:35.190 --> 02:36.749
Now we can go ahead and call our

02:36.750 --> 02:37.619
scanner function.

02:37.620 --> 02:39.286
So it'll use the format that we've

02:39.630 --> 02:41.479
defined here with a percent D

02:41.850 --> 02:43.559
and then save into the pointer

02:43.890 --> 02:45.869
that is being saved onto the stack.

02:50.490 --> 02:51.539
And then we have to correct the

02:51.540 --> 02:53.490
stack for what we've been

02:53.820 --> 02:55.080
the arguments that we pushed on.

02:55.090 --> 02:57.030
So to add E.S.P

02:57.540 --> 02:58.863
eight and at the end of our

02:59.370 --> 03:01.019
function we allocated space for our

03:01.020 --> 03:02.639
variables. We going to go ahead and

03:02.640 --> 03:04.169
I'll put in the correction for that.

03:04.170 --> 03:07.050
So add E.S.P

03:07.500 --> 03:09.479
for so allocated a local

03:09.480 --> 03:10.705
variable up here and then

03:11.340 --> 03:12.930
we have to allocate it down here.

03:14.880 --> 03:16.319
All right. So now we've read the

03:16.320 --> 03:17.986
value from the user and then we're

03:18.150 --> 03:19.179
going to want to read

03:20.250 --> 03:22.014
that in. So we'll copy that from the

03:22.020 --> 03:23.020
stack.

03:23.820 --> 03:26.270
So we'll get the value from E.S.P,

03:26.280 --> 03:28.070
which is where our local variables

03:28.080 --> 03:29.080
at.

03:29.790 --> 03:30.959
And then for our function, we're

03:30.960 --> 03:32.340
going to push it on the stack.

03:35.640 --> 03:37.109
We'll go ahead and call our function

03:37.110 --> 03:38.610
and then correct this attack ad

03:38.630 --> 03:40.620
E.S.P for

03:41.130 --> 03:42.479
each variable took up one.

03:43.380 --> 03:44.699
All right, so now we're ready to

03:44.700 --> 03:45.539
write our function.

03:45.540 --> 03:46.618
So that's our function

03:47.370 --> 03:49.169
is going to try and some all of our

03:49.170 --> 03:49.979
numbers.

03:49.980 --> 03:50.999
We're going to call it FUX.

03:51.960 --> 03:53.549
So the first thing that we need is

03:53.550 --> 03:54.899
we need to have inside of our

03:54.900 --> 03:55.859
function. We have to have our

03:55.860 --> 03:58.169
Prolog, Push SBP,

03:58.890 --> 04:01.620
Move SBP, E.S.P,

04:02.790 --> 04:04.319
and then I'm going to allocate space

04:04.320 --> 04:05.580
for two local variables

04:06.450 --> 04:08.280
to sub E.S.P

04:08.520 --> 04:10.470
eight four for each one of them.

04:11.580 --> 04:12.809
And then inside my function, I'm

04:12.810 --> 04:14.530
going to use X and IDEX.

04:14.560 --> 04:16.139
I'll go ahead and do a push

04:16.709 --> 04:18.599
X, push

04:19.050 --> 04:21.239
X at the end of my function.

04:21.240 --> 04:23.370
I'll go ahead and pop IDEX.

04:24.890 --> 04:27.410
And Pop dcx,

04:28.220 --> 04:29.360
and then I'll put my

04:30.440 --> 04:32.089
correct my stack and then put my

04:32.090 --> 04:34.800
epilog so and E.S.P

04:35.190 --> 04:36.190
eight

04:39.200 --> 04:40.699
move E.S.P.

04:40.700 --> 04:41.700
SBP

04:43.040 --> 04:44.040
are

04:45.380 --> 04:47.269
AVP and

04:47.270 --> 04:48.270
return.

04:50.320 --> 04:52.289
I've some comments in here,

04:52.300 --> 04:53.300
say,

04:54.320 --> 04:55.529
just hours.

05:13.120 --> 05:14.120
OK, so now

05:14.980 --> 05:16.829
we're ready to get into the

05:16.840 --> 05:18.129
meat of it, and so we've got our

05:18.130 --> 05:19.130
local variables.

05:19.690 --> 05:21.356
So, for example, some I'm going to

05:21.370 --> 05:22.930
use that EBP minus for.

05:27.170 --> 05:28.549
So I'm going to save into the

05:28.550 --> 05:29.775
location at EBP minus for

05:30.470 --> 05:31.610
the value of zero.

05:32.140 --> 05:33.140
This is for some.

05:40.540 --> 05:42.459
For I am going to use minus eight

05:43.360 --> 05:45.040
and I'm also going to zero that out

05:45.410 --> 05:46.779
and here I'm putting some comments

05:46.780 --> 05:47.829
in so that we can see

05:49.930 --> 05:51.100
where all this works up.

05:52.460 --> 05:53.479
So now this is where I'm going to

05:53.480 --> 05:54.699
check my loop condition,

05:55.430 --> 05:56.430
I'll call it check.

05:58.070 --> 06:00.410
So what here, I wanted to check that

06:01.380 --> 06:03.050
out. I forgot a semicolon

06:03.770 --> 06:05.060
check

06:07.190 --> 06:08.980
that I

06:09.010 --> 06:11.319
is less and and

06:11.330 --> 06:12.589
and so and is going to be our

06:12.590 --> 06:13.766
parameter and I is going

06:14.480 --> 06:15.550
to be our local variable.

06:16.370 --> 06:18.709
So go ahead and copy rne into X.

06:22.340 --> 06:24.470
Remember, our parameter

06:25.010 --> 06:26.390
one is going to start at

06:26.960 --> 06:27.960
eight.

06:32.100 --> 06:33.276
Plus eight and then move

06:34.050 --> 06:35.309
into MDX.

06:37.820 --> 06:38.959
The value of I.

06:39.930 --> 06:41.309
I'll let you try and figure out

06:41.310 --> 06:42.310
which ones I.

06:43.340 --> 06:45.589
EVP minus

06:46.430 --> 06:47.430
eight.

06:54.370 --> 06:55.938
So here we're checking to see if

06:56.050 --> 06:57.069
that is the case and then we're

06:57.070 --> 06:58.389
going to jump down to label down

06:58.390 --> 07:00.056
here called Done, so Dunn is going

07:00.160 --> 07:01.179
to be what we're going to do at the

07:01.180 --> 07:02.180
very end.

07:03.620 --> 07:04.620
OK,

07:05.660 --> 07:07.099
so if we're not done, then we need

07:07.100 --> 07:08.949
to basically add I

07:08.990 --> 07:09.990
to some.

07:10.070 --> 07:11.149
So here we go.

07:16.380 --> 07:17.730
Go ahead and load some.

07:20.770 --> 07:21.880
Into Yak's

07:22.790 --> 07:25.569
on the ad.

07:28.730 --> 07:30.409
Now, from above, we can see that we

07:30.410 --> 07:31.459
loaded I

07:34.520 --> 07:36.470
into IDEX, and

07:36.540 --> 07:37.939
as we follow through, then we can

07:37.940 --> 07:40.009
just add I to

07:40.010 --> 07:41.509
some and now we've got to save it

07:41.510 --> 07:43.249
back to some.

07:50.130 --> 07:51.959
Now move EBP

07:52.040 --> 07:53.220
minus four.

07:58.740 --> 08:00.308
So now we've added that and then

08:00.390 --> 08:01.769
each time through our loop, we

08:01.770 --> 08:03.289
actually increment I so we need

08:03.600 --> 08:04.709
to load I again,

08:05.710 --> 08:06.749
although the hard way.

08:12.870 --> 08:14.370
So I is minus eight.

08:17.840 --> 08:19.100
I can just increment

08:19.790 --> 08:21.769
the X and then move into

08:21.770 --> 08:23.480
EBP minus eight,

08:25.580 --> 08:27.380
the new value add

08:27.410 --> 08:28.410
X.

08:30.030 --> 08:31.745
All right, and then when we're done

08:31.920 --> 08:33.029
incrementing, then we need to go

08:33.030 --> 08:34.745
back up to the check, so we'll just

08:34.919 --> 08:37.199
to jump check.

08:38.400 --> 08:40.164
And so then when we're all done, the

08:40.230 --> 08:42.329
value of some should be the

08:42.330 --> 08:43.529
addition of all of the numbers

08:43.530 --> 08:44.530
together.

08:44.970 --> 08:47.059
So down here, we need to go move

08:47.290 --> 08:48.290
X.

08:50.230 --> 08:52.389
GBP minus for.

08:58.940 --> 09:00.290
And it looks like I'm the type of

09:00.450 --> 09:01.730
EVP there we go.

09:04.020 --> 09:05.245
All right, so that should

09:05.880 --> 09:07.644
be all we need, right, so we'll jump

09:07.860 --> 09:08.729
back up to the top.

09:08.730 --> 09:10.709
I has been incremented, we reload it

09:11.190 --> 09:13.020
and then compare it against GNR,

09:13.470 --> 09:14.520
which is our number,

09:15.360 --> 09:17.219
and then we should be all right.

09:18.750 --> 09:19.877
So back in our main, we

09:20.580 --> 09:21.629
need to make sure

09:22.650 --> 09:24.360
that we

09:25.650 --> 09:27.539
print off our value that we were

09:27.540 --> 09:28.540
caring about,

09:30.090 --> 09:31.707
so the result of food is going to

09:31.860 --> 09:33.269
have value in X,

09:34.110 --> 09:35.940
so I can just push

09:36.570 --> 09:38.249
the value of X,

09:38.460 --> 09:39.660
I'm going to do printf

09:40.470 --> 09:41.470
push.

09:43.620 --> 09:45.929
Our print format called

09:45.930 --> 09:47.106
print F, and then I have

09:47.820 --> 09:48.820
to adjust the stack.

09:51.340 --> 09:52.957
Now, I could go ahead and correct

09:53.340 --> 09:54.340
this.

09:54.420 --> 09:55.859
I could make it just 12.

09:56.340 --> 09:57.779
But I'm just showing you how we

09:57.780 --> 09:58.956
would break this down at

09:59.700 --> 10:00.700
each step.

10:00.890 --> 10:02.039
All right. So Compiler is going to

10:02.040 --> 10:03.509
be smarter and more efficient than

10:03.510 --> 10:04.735
me. Instead of adding and

10:04.860 --> 10:06.059
subtracting all of these different

10:06.060 --> 10:07.470
times, it'll subtract once

10:08.010 --> 10:09.149
and then use that.

10:10.350 --> 10:11.940
And so here we can see that

10:13.290 --> 10:15.330
it should print off our value

10:17.580 --> 10:18.580
using print.

10:19.650 --> 10:21.330
Now, if I don't have any mistakes.

10:25.060 --> 10:27.159
It looks like line 50.

10:30.880 --> 10:32.200
See what I typed there on?

10:34.700 --> 10:36.049
Mel

10:38.540 --> 10:39.540
Watt, help.

10:48.960 --> 10:50.528
To his label, let's look at this

10:50.610 --> 10:51.659
line. Thirty eight

10:52.890 --> 10:53.890
cents a warning.

10:55.900 --> 10:57.272
Oh, I forgot to put my colon

10:57.850 --> 10:59.350
after my function name.

10:59.500 --> 11:00.500
There we go.

11:08.560 --> 11:09.789
And you can buy a hand.

11:10.860 --> 11:13.140
OK, add those together,

11:13.170 --> 11:14.609
and if you add up all the numbers

11:14.610 --> 11:16.559
from one to 10, you should get fifty

11:16.560 --> 11:17.339
five one.

11:17.340 --> 11:19.229
The three one plus two

11:19.230 --> 11:20.850
is three plus three is six.

11:22.000 --> 11:23.339
They were able to get it.

11:26.410 --> 11:27.639
So a couple of things that usually

11:27.640 --> 11:29.110
catch people are forgetting to

11:29.410 --> 11:31.359
correct the stack or push

11:31.360 --> 11:33.220
things on in the appropriate order

11:34.150 --> 11:35.326
and then referencing our

11:36.250 --> 11:37.818
variables, which have a negative

11:37.960 --> 11:39.381
sign here and our parameters,

11:40.030 --> 11:41.739
which have a plus sign down here.

11:42.480 --> 11:43.539
And so you've got to just make sure

11:43.540 --> 11:45.520
that you you get all those

11:45.760 --> 11:47.279
you save and restore registers,

11:47.620 --> 11:49.389
you allocate space and allocate

11:49.390 --> 11:51.280
space for the variables.

11:52.240 --> 11:53.319
So you got to make sure that you do

11:53.320 --> 11:54.579
all of that if you're going to do a

11:54.580 --> 11:55.580
properly.

12:02.840 --> 12:05.179
So today, we gave an example using

12:05.360 --> 12:06.409
local variables,

12:07.520 --> 12:08.719
and next time we're going to talk

12:08.720 --> 12:09.945
about the enter and leave

12:10.070 --> 12:11.070
instructions.

12:12.020 --> 12:13.070
Do you have any questions you can

12:13.340 --> 12:14.565
contact me at Miller, MJ,

12:15.470 --> 12:17.330
at you and Kada Edu and on Twitter

12:17.510 --> 12:18.290
Milans 30.

