WEBVTT

00:01.280 --> 00:02.899
Hello, this is Dr. Miller, and this

00:02.900 --> 00:04.429
is Episode six point nine of

00:04.430 --> 00:05.430
Assembly

00:07.040 --> 00:08.059
today, where we're going to do is

00:08.060 --> 00:09.319
we're going to write a function from

00:09.320 --> 00:10.129
scratch.

00:10.130 --> 00:11.569
And what we're going to do is we're

00:11.570 --> 00:12.949
going to use parameters,

00:13.520 --> 00:14.598
the prolog and epilog.

00:15.080 --> 00:16.639
We're going to save registers and

00:16.640 --> 00:17.839
then we're going to call some

00:17.840 --> 00:19.040
external functions.

00:22.020 --> 00:23.369
So here's our problem to be solved.

00:23.820 --> 00:25.019
We want to write a program that

00:25.020 --> 00:26.759
prints out multiplication tables for

00:26.760 --> 00:28.500
the numbers one through nine,

00:29.010 --> 00:30.119
not including 10.

00:30.840 --> 00:32.114
And we're going to read in

00:32.670 --> 00:34.619
the how

00:34.620 --> 00:36.560
many multiples we want to print.

00:36.600 --> 00:38.759
So, for example, if we type three,

00:39.360 --> 00:41.340
then we'd print off four

00:41.370 --> 00:42.839
for the number of, say, five would

00:42.840 --> 00:44.910
print out five, 10 and 15,

00:44.940 --> 00:47.039
or if we did four or five, 10, 15

00:47.040 --> 00:48.040
and 20.

00:48.800 --> 00:50.489
And so we're going to we're going to

00:50.490 --> 00:52.254
start from the beginning and we will

00:52.350 --> 00:54.016
write an entire function and we'll

00:54.270 --> 00:55.860
do a minimal main program.

00:59.810 --> 01:01.009
So I'll start off, I'm going to

01:01.430 --> 01:03.145
define what my format string is for

01:03.380 --> 01:04.909
printing off the data, so

01:06.680 --> 01:08.120
I'll just define that first since

01:08.130 --> 01:09.319
I'm at the top of the file.

01:13.330 --> 01:14.999
And we'll just put up we're going to

01:15.010 --> 01:16.030
use print F

01:17.200 --> 01:18.699
so because we're going to use print

01:18.700 --> 01:20.529
F, we need to define that as

01:20.530 --> 01:21.579
an external function.

01:26.080 --> 01:27.305
OK, and then in our main,

01:28.150 --> 01:29.500
we're just going to read a number

01:29.530 --> 01:30.819
and we were going to push it on to

01:30.820 --> 01:32.019
the stack because we're going to use

01:32.020 --> 01:33.879
parameters and then we

01:33.880 --> 01:34.839
got to crack the stack.

01:34.840 --> 01:36.310
So we'll call read.

01:38.430 --> 01:40.450
And we will

01:40.730 --> 01:42.730
the value of that is stored in X,

01:42.900 --> 01:44.579
so push X.

01:45.630 --> 01:47.051
And then I'm going to call my

01:47.250 --> 01:48.250
function print.

01:51.340 --> 01:52.629
And then I got to correct the stack

01:52.630 --> 01:54.489
so I could either add DSP or I

01:54.490 --> 01:55.490
could say

01:56.320 --> 01:58.269
pop X, either one

01:58.270 --> 01:59.270
works.

02:00.970 --> 02:01.779
So now we're going to write our

02:01.780 --> 02:03.099
function. So I'll start off

02:03.730 --> 02:05.349
and give our function a name, which

02:05.350 --> 02:07.420
we've already said is print

02:08.530 --> 02:09.530
no.

02:11.340 --> 02:13.739
Now, I'll start off with the Prolog,

02:13.740 --> 02:15.870
so push BP,

02:16.740 --> 02:18.660
move BP

02:18.690 --> 02:19.690
E.S.P, come down

02:20.550 --> 02:22.080
here and define my epilog

02:23.220 --> 02:24.539
and I'll put a label here.

02:25.080 --> 02:27.029
Some of these later saying, when

02:27.030 --> 02:28.030
am I done?

02:36.180 --> 02:37.356
So put in my epilog, put

02:38.160 --> 02:39.924
it in my return at the end, and I've

02:39.960 --> 02:40.960
got my own label.

02:42.210 --> 02:44.520
All right, so we have our

02:44.790 --> 02:46.169
parameter on the stack.

02:46.170 --> 02:47.969
And what we're going to do is I'm

02:47.970 --> 02:50.069
going to use SBX in order to

02:50.610 --> 02:52.227
store what I'm incrementing stuff

02:52.590 --> 02:53.369
by.

02:53.370 --> 02:54.370
So.

03:01.970 --> 03:03.949
Oh, SBX is going to count

03:03.950 --> 03:05.979
our our number, our initial number,

03:05.990 --> 03:06.990
sorry.

03:13.750 --> 03:15.699
And then we're going to use Yak's

03:16.240 --> 03:17.857
for the multiple to print and I'm

03:18.070 --> 03:19.330
going to start that off at one.

03:24.760 --> 03:26.489
And then I'm going to use MDX.

03:29.190 --> 03:30.562
As the number that I need to

03:31.050 --> 03:32.669
add and I'm going to start off at

03:32.670 --> 03:34.139
zero because I want to incremented

03:34.140 --> 03:36.060
inside of my initialization.

03:40.460 --> 03:41.460
OK, so

03:42.410 --> 03:43.699
in my initialization,

03:46.160 --> 03:47.509
what I want to do is I want to

03:47.510 --> 03:49.029
increment so each time through,

03:49.580 --> 03:51.259
I want it to be one higher than what

03:51.260 --> 03:52.260
it was before.

03:55.160 --> 03:56.532
And then I want to use X for

03:57.050 --> 03:58.520
my loop, so X is going to say,

03:59.000 --> 04:00.770
how many times am I going to go in

04:01.190 --> 04:02.319
and print off this number?

04:02.330 --> 04:03.800
So it might be, you know, four

04:04.250 --> 04:06.020
or five, 10, what have you.

04:06.320 --> 04:08.509
So I'll use X for that.

04:09.620 --> 04:11.719
And then I'm using an eBay

04:11.730 --> 04:12.619
stack.

04:12.620 --> 04:13.699
So if you recall,

04:14.900 --> 04:16.999
SBP plus eight

04:17.000 --> 04:18.199
is our first parameter.

04:18.200 --> 04:19.729
So that should be the parameter that

04:19.730 --> 04:21.298
is up here that's getting pushed

04:21.709 --> 04:23.424
on to the stack because we used our

04:23.660 --> 04:24.680
standard prolog

04:27.380 --> 04:28.970
and then here I'm going to

04:30.020 --> 04:31.880
copy into X

04:33.320 --> 04:35.239
the value of X, and

04:35.240 --> 04:36.800
so that's going to write

04:37.070 --> 04:38.839
X is going to increment each time

04:38.840 --> 04:39.889
and it's going to say what I'm going

04:39.890 --> 04:41.605
to add to it. And so then each time

04:41.630 --> 04:43.198
through it'll be the next number

04:43.610 --> 04:44.610
in my loop.

04:47.800 --> 04:49.209
And then we'll copy that back, so

04:49.210 --> 04:50.680
we've incremented IDEX, copied

04:51.190 --> 04:53.079
the incremented version and

04:53.080 --> 04:55.199
then copied that back to Edds

04:55.420 --> 04:56.420
just to be safe.

04:59.510 --> 05:00.784
All right, and then inside

05:01.490 --> 05:02.813
of here, we want to be able

05:03.320 --> 05:05.480
to print off our number,

05:05.630 --> 05:06.850
so to print our number

05:07.520 --> 05:08.659
so the number we're going to print

05:08.660 --> 05:10.339
is going to be an X, so I'm going to

05:10.340 --> 05:12.649
push yak's.

05:15.270 --> 05:17.129
Push my format that I want

05:17.130 --> 05:19.019
to print, which I defined

05:19.020 --> 05:20.020
at the top call,

05:21.240 --> 05:23.170
print F,

05:24.540 --> 05:26.059
and then I got to fix the stack

05:26.430 --> 05:28.470
because I pushed two parameters on

05:28.830 --> 05:30.000
I need to fix those.

05:30.120 --> 05:32.130
And so I'll go ahead and

05:33.030 --> 05:35.189
add to E.S.P.

05:37.810 --> 05:39.280
Eight, right, because four for

05:39.670 --> 05:40.670
each one of those

05:41.560 --> 05:42.899
soon I'll call printf

05:43.480 --> 05:45.399
and then when I'm all done

05:45.610 --> 05:46.884
right each time through my

05:47.440 --> 05:49.780
loop, I want to

05:51.220 --> 05:52.220
add to

05:53.050 --> 05:55.029
Yak's the value that's in Ed.

05:55.610 --> 05:57.069
So if I'm counting my fives,

05:57.430 --> 05:59.439
Ed will have five and X

05:59.440 --> 06:00.969
will have five the first time and

06:00.970 --> 06:01.999
then 10. And then 15.

06:02.410 --> 06:03.869
And then 20 and twenty five.

06:05.350 --> 06:06.350
And then each time

06:07.180 --> 06:09.129
through here I want to loop back up

06:09.130 --> 06:10.130
to the

06:11.110 --> 06:12.339
top and this is where I'm going to

06:12.340 --> 06:13.870
print it again. So it's a loop

06:14.860 --> 06:17.019
and then I'll define a variable

06:17.020 --> 06:17.829
called top.

06:17.830 --> 06:19.029
And so that's where I'm going to go

06:19.030 --> 06:20.030
and print.

06:24.360 --> 06:25.928
So this will print off, so if we

06:26.160 --> 06:27.359
have the number one, it's going to

06:27.360 --> 06:28.569
print off one, two, three, four,

06:28.590 --> 06:30.899
five, if five is our maximum.

06:31.440 --> 06:32.909
And then what we want to do is we

06:32.910 --> 06:34.184
want to go now and do two,

06:34.740 --> 06:35.999
four, six, eight, ten,

06:36.780 --> 06:37.800
for our example.

06:38.130 --> 06:39.390
So we're going to have another loop,

06:39.430 --> 06:40.900
right. So we can't use to loop

06:41.670 --> 06:42.869
constructs. We would have to

06:42.870 --> 06:44.519
actually increment it

06:44.970 --> 06:45.970
ourselves.

06:46.380 --> 06:48.630
And so to do that, we're using SBX.

06:48.780 --> 06:50.699
So Ebix is our number that we want

06:50.700 --> 06:51.629
to do.

06:51.630 --> 06:53.100
So we'll go ahead and increment

06:54.240 --> 06:55.240
SBX

06:56.460 --> 06:57.930
and then compare

06:58.710 --> 06:59.837
SBX to the number, say,

07:00.660 --> 07:02.759
10. So I'm using

07:02.760 --> 07:03.749
10.

07:03.750 --> 07:05.220
We can we're going to go up to

07:05.640 --> 07:07.589
but not including 10 in this

07:07.590 --> 07:08.999
example, but you could change it

07:09.630 --> 07:11.039
to meet your needs.

07:11.970 --> 07:13.636
And then each time through, I will

07:14.010 --> 07:15.269
want to print a new lines that I

07:15.270 --> 07:16.439
know that I'm done with, say, the

07:16.440 --> 07:18.204
multiples of one, then the multiples

07:18.600 --> 07:20.240
of two and then multiples of three.

07:21.060 --> 07:22.740
So go ahead and call print

07:23.460 --> 07:24.460
new line.

07:25.460 --> 07:27.379
And then so I've done

07:27.380 --> 07:29.569
my comparison to SBX

07:29.570 --> 07:31.429
and 10, and so I can say

07:31.430 --> 07:32.430
jump, not if it's

07:33.320 --> 07:35.479
not zero, then I

07:35.990 --> 07:37.850
want to go back up to the Init part.

07:38.510 --> 07:40.099
And that's why I set up in it the

07:40.100 --> 07:40.909
way I did.

07:40.910 --> 07:42.199
Right. So it does some,

07:43.730 --> 07:45.019
you know, incrementing Edek.

07:45.030 --> 07:46.647
So the first time through Ed Exos

07:46.730 --> 07:48.347
one. Right, because I incremented

07:48.410 --> 07:49.410
from zero.

07:49.730 --> 07:51.859
So now it should go to two

07:51.860 --> 07:53.624
and then they copy two to X and then

07:53.690 --> 07:55.699
it should still be in IDEX and

07:55.700 --> 07:57.259
then we go through and print off.

07:57.260 --> 07:59.089
Right. Adding the number two

07:59.090 --> 08:00.949
each time to X which again is set

08:00.950 --> 08:01.950
to, to.

08:06.020 --> 08:07.880
OK, so.

08:09.580 --> 08:11.709
That should be the entirety

08:11.710 --> 08:13.327
of the part that we need in order

08:13.630 --> 08:14.630
to get stuff done.

08:18.260 --> 08:20.149
Looks like I didn't have any syntax

08:20.150 --> 08:22.119
errors, so if I want to print

08:22.130 --> 08:23.629
my multiples of five,

08:24.290 --> 08:25.390
we end up with a problem.

08:26.300 --> 08:28.339
And so the problem is that

08:28.340 --> 08:29.449
inside of

08:30.590 --> 08:32.119
the print out function,

08:32.419 --> 08:34.339
it actually destroys

08:34.340 --> 08:36.409
values for X,

08:36.490 --> 08:37.729
Edcon, X.

08:38.840 --> 08:40.519
So print F is messing up

08:40.909 --> 08:42.649
our variables that we're using A for

08:42.650 --> 08:44.629
the looping and B for our numbers.

08:44.630 --> 08:46.039
Right. Because the return result of

08:46.040 --> 08:47.559
a function is whether or not it

08:47.870 --> 08:49.579
was successful or the value that it

08:49.580 --> 08:50.580
needed.

08:50.720 --> 08:52.789
And so this is messing

08:52.790 --> 08:54.259
up all of our variables.

08:54.480 --> 08:56.480
And so what we need to do

08:56.780 --> 08:59.269
is we need to save our our registers

09:01.010 --> 09:02.010
that we are using.

09:02.090 --> 09:03.756
So in our function, we're using X,

09:04.370 --> 09:05.370
Ebix, so

09:06.290 --> 09:07.440
our X,

09:08.510 --> 09:10.429
X and X, so I can

09:10.430 --> 09:11.780
go ahead and save all of those.

09:19.860 --> 09:21.779
Before I call printf, so here I

09:21.780 --> 09:22.780
am

09:23.790 --> 09:25.520
calling printf

09:26.520 --> 09:27.899
and then I've got some work that I'm

09:27.900 --> 09:28.900
doing down here.

09:30.540 --> 09:32.220
So I want to restore my registers.

09:39.110 --> 09:40.727
So you should think about what is

09:41.000 --> 09:42.769
the order that I need to restore my

09:42.770 --> 09:43.770
registers?

09:45.280 --> 09:46.690
All right, so I'll do it.

09:46.720 --> 09:48.009
So we're going to do pop,

09:50.740 --> 09:51.740
pop,

09:52.990 --> 09:55.179
pop, CBX,

09:56.470 --> 09:58.149
pop x,

09:58.340 --> 10:00.159
right? So now I have restored all of

10:00.160 --> 10:01.630
my registers from the previous

10:01.690 --> 10:02.919
values that they had.

10:03.880 --> 10:05.289
Right. And they're saved on the

10:05.290 --> 10:07.149
stack. So now in printf messes

10:07.150 --> 10:08.150
with my function.

10:08.560 --> 10:10.539
Right. It won't actually hurt

10:10.540 --> 10:11.540
my registers.

10:25.120 --> 10:27.010
So I typed five for my number

10:27.310 --> 10:29.169
and so write the first

10:29.170 --> 10:30.444
loop, it's using one as my

10:31.210 --> 10:32.340
what I'm going to increment by.

10:32.350 --> 10:33.519
So I go one, two, three, four or

10:33.520 --> 10:34.489
five.

10:34.490 --> 10:36.429
And now in our function, right.

10:36.760 --> 10:38.159
Ed becomes too.

10:38.170 --> 10:39.369
And so that's what I'm incrementing

10:39.370 --> 10:41.649
by. So I got two, four, six,

10:41.650 --> 10:42.650
eight, ten and then

10:43.600 --> 10:46.000
three, six, nine, 12, 15

10:46.420 --> 10:48.519
doing each multiple up to

10:48.520 --> 10:50.360
but not including the number ten.

10:51.040 --> 10:52.479
And then if I enter the number ten.

10:52.480 --> 10:54.879
Right. So I print off ten multiples

10:54.880 --> 10:56.259
of each one of these numbers.

11:01.370 --> 11:02.659
Now, one of the things that we also

11:02.660 --> 11:04.179
talked about was saving all the

11:04.220 --> 11:05.886
registers that you use so that you

11:05.900 --> 11:06.900
don't mess them up.

11:07.220 --> 11:08.592
So, again, we could save our

11:08.660 --> 11:10.009
registers. So

11:10.580 --> 11:13.000
we used Ebix

11:13.010 --> 11:14.010
and ITCs,

11:15.380 --> 11:16.850
so I'll pick a random order.

11:25.870 --> 11:28.020
All right, so I did X,

11:28.410 --> 11:30.920
then X, then edicts, then SBX,

11:31.620 --> 11:32.999
so at the end of my function,

11:34.040 --> 11:35.519
if I can get down here.

11:38.280 --> 11:39.509
Now, my order is going to kill me

11:39.510 --> 11:40.619
here, so let's look.

11:45.010 --> 11:46.240
B and then D.

11:55.950 --> 11:57.839
B and then D and

11:57.840 --> 11:58.840
then A and then

11:59.820 --> 12:00.820
C.

12:07.160 --> 12:09.139
OK, so I just have to

12:09.140 --> 12:10.819
make sure that I know what the order

12:10.820 --> 12:12.584
was before I called the function and

12:12.590 --> 12:13.969
what the order is after

12:16.430 --> 12:18.289
and what we can do is we can see

12:18.290 --> 12:19.320
if we did it right.

12:19.400 --> 12:20.576
So we are getting ready.

12:20.960 --> 12:22.309
We're calling our function

12:22.880 --> 12:23.880
so we can.

12:27.490 --> 12:28.869
Dump our registers.

12:37.660 --> 12:38.799
Before and after.

12:44.160 --> 12:45.649
And so I'll just do two

12:46.490 --> 12:47.568
so we can see, but you

12:48.380 --> 12:49.380
could pick a different number

12:50.960 --> 12:52.829
so you can see is

12:52.850 --> 12:53.928
to that gets restored.

12:54.650 --> 12:55.826
Ebix is zero before zero

12:56.750 --> 12:59.059
after X.

13:00.710 --> 13:01.984
Looks like five nine eight

13:02.740 --> 13:04.330
eight four zero.

13:05.140 --> 13:06.365
Same thing there and then

13:07.030 --> 13:08.030
here, Fefe.

13:09.220 --> 13:10.659
Nine, eight, five,

13:11.080 --> 13:13.089
four, and that is also

13:13.090 --> 13:14.919
restored, so we can see that

13:14.920 --> 13:16.510
we didn't modify anything

13:16.780 --> 13:18.348
because we went through and made

13:18.760 --> 13:20.649
sure that all of the registered

13:20.650 --> 13:22.149
that we're using in our function, we

13:22.150 --> 13:23.326
saved before we modified

13:24.520 --> 13:26.469
them. And then when we got to the

13:26.470 --> 13:28.629
end, we restored the values

13:28.630 --> 13:29.799
back to where they were.

13:30.430 --> 13:32.169
We used our function Prolog.

13:32.170 --> 13:33.820
And and here's our epilog.

13:35.500 --> 13:37.539
We used our prolog at

13:37.540 --> 13:38.814
the very beginning, right,

13:39.610 --> 13:41.589
and then we had to

13:41.590 --> 13:42.913
fix the error when the fact

13:43.450 --> 13:44.589
that another function had

13:44.590 --> 13:46.354
obliterated our registers, we needed

13:46.450 --> 13:48.309
to save them and restore

13:48.340 --> 13:50.799
them before and after it obliterated

13:50.800 --> 13:52.059
them because we wanted to

13:52.720 --> 13:54.069
get back to the previous state that

13:54.070 --> 13:55.070
we were at.

13:55.120 --> 13:57.100
We also used one parameter,

13:57.540 --> 13:59.108
right? So EBP plus eight was our

13:59.440 --> 14:00.999
parameter. And we used that

14:01.270 --> 14:02.779
in order to determine how many times

14:02.780 --> 14:04.870
that we were going to do our our

14:05.260 --> 14:06.260
inner loop.

14:12.560 --> 14:14.539
So today, we looked at creating

14:14.540 --> 14:16.309
a function from the very beginning,

14:16.310 --> 14:17.719
using the conventions that we've

14:17.720 --> 14:19.489
talked about in previous lectures,

14:20.150 --> 14:21.816
and then we ran into a problem and

14:21.920 --> 14:23.684
we had to understand what is the way

14:23.840 --> 14:25.519
that I can solve it and why is it a

14:25.520 --> 14:26.839
problem? Why is it a problem that

14:26.840 --> 14:27.918
functions, obliterate,

14:28.820 --> 14:30.799
registers? And how do you fix

14:30.800 --> 14:31.800
that problem?

14:33.410 --> 14:34.609
Next time we're going to talk about

14:34.610 --> 14:36.739
a recursive function or a function

14:36.740 --> 14:37.740
that calls itself.

14:39.100 --> 14:40.299
And then if you have questions, you

14:40.300 --> 14:42.064
can contact me at Miller, MJ, at you

14:42.070 --> 14:43.779
and Kadiatu, you can find me on

14:43.780 --> 14:45.249
Twitter at Inelasticity.

