WEBVTT

00:01.070 --> 00:02.599
Hello, this is Dr. Miller, and this

00:02.600 --> 00:03.972
is Episode six point four of

00:04.190 --> 00:05.190
assembly today

00:06.260 --> 00:07.639
we're going to talk about creating a

00:07.640 --> 00:09.800
function and then running a debugger

00:09.830 --> 00:10.830
on that function.

00:12.460 --> 00:13.760
So creating functions.

00:15.070 --> 00:16.393
So to define a function, we

00:16.840 --> 00:18.300
basically need two things.

00:18.370 --> 00:20.085
The first thing is a label and that

00:20.140 --> 00:21.550
is the name of the function

00:22.120 --> 00:23.559
and then a return at the end.

00:24.340 --> 00:26.229
And then in order to execute

00:26.230 --> 00:28.059
the function, we use

00:28.060 --> 00:29.100
the call instruction.

00:29.650 --> 00:31.510
So Call says that we are going to

00:31.840 --> 00:33.409
start executing a function.

00:33.460 --> 00:34.719
What it's going to do is it's going

00:34.720 --> 00:36.279
to push the return address of the

00:36.280 --> 00:38.044
next instruction onto the top of the

00:38.140 --> 00:39.806
stack and then it's going to start

00:40.030 --> 00:41.180
executing that function.

00:41.200 --> 00:42.579
So to do that, it jumps to the

00:42.580 --> 00:43.750
address of the function.

00:44.200 --> 00:45.964
And then EIP is set to that function

00:46.390 --> 00:47.709
address and that's how it starts.

00:47.710 --> 00:49.869
Executing yippy always points

00:49.870 --> 00:51.189
to our current instruction.

00:52.510 --> 00:53.559
And then when the function is all

00:53.560 --> 00:54.932
done, we'll issue the return

00:55.360 --> 00:56.360
instruction.

00:56.470 --> 00:58.089
And so that will pop off the address

00:58.090 --> 00:59.379
that's on the top of the stack and

00:59.380 --> 01:01.490
set EIP to it and then continue.

01:01.510 --> 01:03.429
So you can see this will save that

01:03.430 --> 01:04.779
next instruction that we want to

01:04.780 --> 01:06.446
execute and then this will restore

01:06.730 --> 01:08.019
it when we are all done.

01:10.280 --> 01:11.269
And today, we're going to do a

01:11.270 --> 01:12.679
little bit of debugging just so that

01:12.680 --> 01:14.719
we can see exactly how

01:15.050 --> 01:16.389
the system works.

01:16.430 --> 01:18.349
So exactly how the stack is related

01:18.350 --> 01:19.759
to the function and the current

01:19.760 --> 01:20.809
instruction, it's going to be

01:20.810 --> 01:21.810
executed.

01:22.310 --> 01:24.349
So I think that the GEF or

01:24.350 --> 01:26.719
Jeff type of framework

01:26.720 --> 01:28.580
for Gbps is really nice.

01:29.030 --> 01:31.069
And so if you Google G.F.,

01:31.520 --> 01:32.509
you will find it.

01:32.510 --> 01:34.127
Here is a link to the GitHub page

01:34.430 --> 01:36.409
and then on their page they

01:36.410 --> 01:38.449
have how you can execute a script to

01:38.450 --> 01:40.049
just install it by default.

01:40.880 --> 01:42.154
And so this is the command

01:42.740 --> 01:44.719
that I ran inside of my terminal

01:44.720 --> 01:46.060
in order to get it installed.

01:46.370 --> 01:47.629
And so once you do that, then you

01:47.630 --> 01:48.630
can continue.

01:50.600 --> 01:51.829
So today we're going to create a

01:51.830 --> 01:52.830
simple function.

01:53.360 --> 01:55.489
And so the function is called clear

01:55.490 --> 01:56.359
X.

01:56.360 --> 01:57.920
All it does is move

01:58.220 --> 02:00.359
zero into X and then return.

02:01.010 --> 02:02.720
So you see that we have a label here

02:02.870 --> 02:05.269
that has a name and then a colon

02:05.270 --> 02:07.219
and then we have this is the body

02:07.220 --> 02:09.330
of our function moves X

02:09.350 --> 02:10.459
and then we're going to do a return.

02:11.120 --> 02:12.499
And then instead of our main, we're

02:12.500 --> 02:14.350
going to call this function.

02:14.750 --> 02:16.099
And then when we're all done, we'll

02:16.100 --> 02:17.472
go ahead and we can print up

02:17.840 --> 02:18.960
registers. And I have a couple

02:18.980 --> 02:20.149
different examples that will go

02:20.150 --> 02:21.150
through.

02:22.370 --> 02:23.659
So let's get that started.

02:25.860 --> 02:26.860
All right,

02:28.170 --> 02:30.479
so go ahead and edit our file

02:31.830 --> 02:33.299
and what we're going to do here is

02:33.300 --> 02:35.220
we're going to put a few commands in

02:35.460 --> 02:36.881
in order to help us visualize

02:37.740 --> 02:38.529
the stack.

02:38.530 --> 02:39.659
So I'm going to copy a couple of

02:39.660 --> 02:41.249
things and push them onto the stack.

02:48.920 --> 02:50.096
So I move feed beef into

02:50.870 --> 02:52.487
X and then I'll go ahead and push

02:52.580 --> 02:54.197
that on top of the stack and then

02:54.350 --> 02:55.350
move.

03:00.180 --> 03:01.180
Beef, beef,

03:02.400 --> 03:04.319
push that onto the top of the stack

03:06.510 --> 03:08.274
and then I'm going to end up calling

03:08.370 --> 03:09.509
my function, so.

03:12.890 --> 03:14.200
That's the name of my function

03:14.900 --> 03:16.339
now, if we notice inside of my

03:16.340 --> 03:17.908
program, I actually have already

03:18.740 --> 03:20.809
a function ASRM underscore

03:20.810 --> 03:22.182
main, that's the name of our

03:22.610 --> 03:24.169
function. And at the end it has a

03:24.170 --> 03:25.170
return.

03:25.370 --> 03:27.199
And we'll talk about enter and leave

03:27.740 --> 03:28.740
later on.

03:29.000 --> 03:30.199
But that's a function.

03:30.290 --> 03:31.711
All right. So generally, what

03:32.210 --> 03:33.499
you want to do is you want to have a

03:33.500 --> 03:35.749
function start and then end

03:35.750 --> 03:37.789
and then you define

03:37.790 --> 03:38.869
additional functions.

03:38.900 --> 03:39.979
So I'm going to come down here and

03:39.980 --> 03:41.029
define my function.

03:44.340 --> 03:46.319
So I have a label which is

03:46.320 --> 03:48.629
a string followed by a colon,

03:48.810 --> 03:50.770
and then do what I want to do, move

03:50.800 --> 03:52.409
the X zero

03:53.550 --> 03:54.869
and I'll do a return.

03:56.880 --> 03:58.349
And then if we recall from previous

03:58.350 --> 03:59.909
lectures, right, I pushed two things

03:59.910 --> 04:01.409
on to the top of the stack, and so I

04:01.410 --> 04:03.210
need to clear off the stack here.

04:06.880 --> 04:07.880
So that should I

04:08.800 --> 04:10.569
have four for each one of these,

04:10.690 --> 04:12.699
and so four times two is eight, so

04:12.700 --> 04:14.199
I have to add eight TSB.

04:16.440 --> 04:17.789
All right, so now we should be

04:17.790 --> 04:18.790
ready.

04:22.360 --> 04:23.889
And when we run it, it's not going

04:23.890 --> 04:25.180
to do anything,

04:26.470 --> 04:27.470
but that's OK.

04:27.550 --> 04:28.689
We're going to run it in the
debuggers.

04:28.690 --> 04:29.919
So we're going to see exactly how

04:29.920 --> 04:30.920
this works.

04:31.300 --> 04:33.129
So the debugger is called the Ganu

04:33.130 --> 04:34.550
debugger or GDB.

04:34.720 --> 04:36.109
So we'll go ahead and execute that

04:36.110 --> 04:38.019
key DB and then

04:38.020 --> 04:39.249
we give it the name of the program

04:39.250 --> 04:40.250
that we want to debug.

04:40.390 --> 04:42.519
And so this example is six

04:42.640 --> 04:43.839
point four

04:45.250 --> 04:46.250
be.

04:47.820 --> 04:49.649
So if you've run

04:49.650 --> 04:51.149
the gaff script that I put in the

04:51.150 --> 04:52.571
previous one, you'll see G.F.

04:53.370 --> 04:54.989
here and an arrow pointer

04:55.650 --> 04:56.939
and so here we can run a couple of

04:56.940 --> 04:57.940
different commands.

04:58.200 --> 05:00.029
So probably the simplest one is to

05:00.030 --> 05:00.929
set a breakpoint.

05:00.930 --> 05:02.819
So B is to set a breakpoint.

05:02.820 --> 05:04.049
And the function that I want to set

05:04.050 --> 05:06.600
a breakpoint is as an underscore

05:07.080 --> 05:09.440
main, and that is our main function.

05:09.450 --> 05:10.680
We can see it has a label.

05:10.710 --> 05:12.229
We could also put one it in the

05:12.240 --> 05:13.319
clear X.

05:16.150 --> 05:17.350
That's also a function.

05:18.430 --> 05:19.869
So now that we've set breakpoints,

05:19.870 --> 05:21.579
we can start running our program

05:22.180 --> 05:23.846
and this is where we're GEF really

05:23.920 --> 05:25.869
works well. So if I type are in

05:25.870 --> 05:26.870
order to run.

05:28.520 --> 05:30.259
Now we're going to see in here and

05:30.260 --> 05:31.549
the reason I made my terminal so

05:31.550 --> 05:33.314
small is that we can get all of this

05:33.320 --> 05:34.320
stuff on the screen.

05:35.060 --> 05:36.889
So if we see up here, we can

05:36.890 --> 05:38.439
see that we have registers.

05:38.840 --> 05:40.639
So this is showing us what all of

05:40.640 --> 05:42.259
the registers that we have inside of

05:42.260 --> 05:44.540
our program. So we've got X, X,

05:44.840 --> 05:46.459
EXP, SBP,

05:46.850 --> 05:48.949
Ayda IP and then our flags

05:49.880 --> 05:51.546
so we can see all of our registers

05:51.560 --> 05:52.879
as they currently are.

05:52.940 --> 05:54.110
We can see the stack.

05:54.590 --> 05:56.509
So on the stack you will see

05:56.780 --> 05:58.397
the very top is what's pointed to

05:58.670 --> 05:59.670
by ESP.

05:59.750 --> 06:01.514
And so as we push things, you'll see

06:01.580 --> 06:03.410
stuff move down the stack

06:04.340 --> 06:05.540
and then we can see the code.

06:05.570 --> 06:07.519
So this is where we're exactly

06:07.520 --> 06:08.520
we're running.

06:08.600 --> 06:10.550
And so we're at ASRM Main

06:10.910 --> 06:13.069
and it stopped at ASRM main plus

06:13.070 --> 06:14.295
four. So this is the next

06:14.330 --> 06:15.529
instruction that's going to get

06:15.530 --> 06:16.951
executed when we execute this

06:17.060 --> 06:18.060
function.

06:18.940 --> 06:19.989
And we've got a couple of different

06:19.990 --> 06:21.313
commands, so we've got step

06:21.550 --> 06:23.069
instruction in next and step

06:23.090 --> 06:25.179
construction, so ESSI

06:25.180 --> 06:26.829
or step instruction means do the

06:26.830 --> 06:29.079
next instruction and

06:29.080 --> 06:30.795
if there's a function call, go into

06:30.940 --> 06:31.940
that function call.

06:32.020 --> 06:33.879
Whereas next instruction will.

06:34.990 --> 06:35.990
Will.

06:36.730 --> 06:38.589
Step into that and so

06:38.590 --> 06:39.759
we'll see the difference here in a

06:39.760 --> 06:40.760
little bit.

06:41.260 --> 06:43.029
So I'm just going to use step

06:43.030 --> 06:44.030
instruction to see

06:45.010 --> 06:46.529
each one of these, and if I hit

06:46.600 --> 06:48.069
enter, it'll do the previous come

06:48.070 --> 06:49.736
in, so I'm just going to step each

06:49.930 --> 06:50.930
instruction.

06:51.280 --> 06:52.910
So here we can see we got moving

06:53.200 --> 06:54.279
and then feed beef.

06:54.760 --> 06:56.649
And so we should see a change

06:56.650 --> 06:58.269
here when we execute that command.

06:59.440 --> 07:01.008
So now we can see that X is feed

07:01.600 --> 07:02.600
beef.

07:03.300 --> 07:04.779
And so now it's going to do a push.

07:06.510 --> 07:08.176
So you should see that X should go

07:08.520 --> 07:10.080
on top of the stack.

07:11.920 --> 07:13.880
Right, so now we can see E.S.P

07:13.890 --> 07:16.040
is pointing to feed beef,

07:16.050 --> 07:17.569
so now that feed beef is on top

07:18.060 --> 07:19.060
of the stack.

07:19.440 --> 07:20.819
Now here we're going to copy beef

07:20.820 --> 07:22.230
beef, anti-tax.

07:23.940 --> 07:25.829
So now he has both beef

07:25.830 --> 07:27.089
and now this should get copied to

07:27.090 --> 07:28.369
the top of the stack and then feed

07:28.380 --> 07:29.820
beef will move down.

07:32.660 --> 07:34.519
OK, so now if I

07:34.520 --> 07:36.235
did at this point, right, we've put

07:36.410 --> 07:37.969
on those two items on top of our

07:37.970 --> 07:39.829
stack and then we're ready to call

07:39.830 --> 07:41.660
our function and we can see

07:41.700 --> 07:43.639
that the GEF is trying

07:43.640 --> 07:45.257
to show us what our function look

07:45.290 --> 07:46.907
like. Normally a function as more

07:46.910 --> 07:48.139
than just two instructions.

07:48.620 --> 07:49.639
This one only has two.

07:49.640 --> 07:50.809
So we're only going to see these.

07:50.810 --> 07:52.129
And then this stuff here is going to

07:52.130 --> 07:53.600
be garbage. That is not really

07:54.020 --> 07:55.369
instructions because the return is

07:55.370 --> 07:56.370
going to exit from that.

07:57.440 --> 07:59.420
And so here if I did

07:59.720 --> 08:00.994
next instruction, it would

08:01.610 --> 08:02.389
step over that.

08:02.390 --> 08:03.859
Call me when you get to see what

08:03.860 --> 08:05.379
happens, but we're going to use

08:05.690 --> 08:07.111
step instruction or go to the

08:07.520 --> 08:09.088
next one. And if it's a call, go

08:09.110 --> 08:10.510
into that call function.

08:11.600 --> 08:13.217
So we are going to see the return

08:13.790 --> 08:16.130
address. So the address of

08:16.160 --> 08:17.290
the next instruction.

08:17.300 --> 08:19.439
So after Assim underscore

08:19.460 --> 08:21.679
main plus 17, the next instruction

08:21.680 --> 08:23.029
is going to get executed and I can

08:23.030 --> 08:24.304
scroll up and we should be

08:24.860 --> 08:26.389
able to see the next instruction.

08:26.750 --> 08:28.070
Is this one right here?

08:28.370 --> 08:30.319
It is the ad ESP

08:30.320 --> 08:31.320
eight and its value

08:32.539 --> 08:33.539
is eighty, forty

08:34.580 --> 08:35.580
eight.

08:36.080 --> 08:37.159
Forty six.

08:37.400 --> 08:38.779
So that is the value that's going to

08:38.780 --> 08:40.158
get pushed on set because that's the

08:40.159 --> 08:41.569
next instruction that we want to

08:41.570 --> 08:43.138
execute from what we're at right

08:43.549 --> 08:44.479
now.

08:44.480 --> 08:45.649
So I'll go ahead and do step

08:45.650 --> 08:47.059
instruction. We should see

08:47.780 --> 08:49.519
that we will enter this function and

08:49.520 --> 08:51.019
then that return address is going to

08:51.080 --> 08:52.459
get pushed on top of the stack.

08:54.810 --> 08:56.459
So now if we look, we can see that

08:56.460 --> 08:58.224
that number that I just said earlier

08:58.320 --> 08:59.937
is pushed on top of the stack and

09:00.000 --> 09:01.259
there's an extra zero because it's

09:01.260 --> 09:02.580
got to fill all eight bits.

09:04.170 --> 09:05.787
And so now we're in our function.

09:06.090 --> 09:07.919
Our function is going to move zero

09:07.920 --> 09:09.419
into X and then it's going to

09:09.420 --> 09:10.769
return. And so it's going to pop

09:10.770 --> 09:12.359
this off of the stack and then we

09:12.360 --> 09:13.469
will be running that other

09:13.470 --> 09:14.939
instruction. So right now, we're in

09:14.940 --> 09:16.799
the function as it shows

09:16.800 --> 09:18.419
here. Clear X.

09:20.090 --> 09:21.289
So I'll go ahead and step.

09:23.130 --> 09:24.130
So now.

09:25.980 --> 09:27.869
Yak's has got to

09:27.870 --> 09:29.242
zero because that's what our

09:29.490 --> 09:30.959
function is going to do at this

09:30.960 --> 09:32.730
point, we're ready to do a return.

09:32.740 --> 09:34.619
So we're going to go back to our

09:34.620 --> 09:35.620
azem main.

09:36.080 --> 09:37.109
We're going to go back to this

09:37.110 --> 09:38.039
address right here.

09:38.040 --> 09:39.509
So this will get popped off of the

09:39.510 --> 09:40.510
stack.

09:44.380 --> 09:46.299
So now we can see X is zero, which

09:46.300 --> 09:47.769
is where you are, we wanted our

09:47.770 --> 09:49.629
function to do, and again,

09:49.630 --> 09:51.296
the top of the stack is beef, beef

09:51.640 --> 09:53.589
and then feed beef right in reverse

09:53.590 --> 09:54.760
order that we push them on.

09:56.590 --> 09:57.970
And so now if we step,

09:59.170 --> 10:01.059
we can see that those disappeared

10:01.060 --> 10:02.870
because we added our ATP

10:02.890 --> 10:04.360
and now ESP points to where it

10:04.510 --> 10:05.649
originally pointed when our

10:05.650 --> 10:06.820
functioning started.

10:07.660 --> 10:09.009
And so we ended up with the result

10:09.010 --> 10:10.480
of X being zero, which is what

10:10.870 --> 10:12.291
we wanted to do inside of our

10:12.610 --> 10:13.449
function.

10:13.450 --> 10:14.940
We had moved other values in there.

10:15.340 --> 10:16.450
And so now we're all done.

10:16.640 --> 10:18.257
So far, all done. We can hit C to

10:18.340 --> 10:20.019
continue and it'll continue running

10:20.020 --> 10:21.639
the program until it exits

10:22.330 --> 10:23.639
and says the program exited.

10:23.650 --> 10:24.650
Normally

10:26.770 --> 10:27.770
you quit.

10:31.060 --> 10:32.060
All right.

10:37.550 --> 10:39.289
So today, we talked about creating a

10:39.290 --> 10:41.054
function and then using our debugger

10:41.540 --> 10:42.889
on the function so we could kind of

10:42.890 --> 10:44.458
understand what happens when you

10:44.780 --> 10:46.219
call a function, what happens to the

10:46.220 --> 10:48.440
stack, what gets added to the stack

10:48.830 --> 10:50.496
when you do a call instruction and

10:50.660 --> 10:52.277
what happens when you do a return

10:52.730 --> 10:53.730
instruction?

10:55.170 --> 10:56.399
So looking forward, we're going to

10:56.400 --> 10:58.289
look at more complicated functions.

10:58.360 --> 10:59.369
All right, so we're going to add

10:59.370 --> 11:00.370
some parameters

11:01.350 --> 11:02.699
to it. We're going to also talk

11:02.700 --> 11:04.170
about how do we save registers

11:04.710 --> 11:05.759
so that way they don't get

11:05.760 --> 11:06.760
clobbered.

11:06.780 --> 11:08.219
And then we'll talk about recursive

11:08.220 --> 11:09.220
functions.

11:11.540 --> 11:12.799
If you have questions, you can email

11:12.800 --> 11:14.466
me at millirems, at Juniata Aidoo,

11:14.990 --> 11:16.459
and you can find me on Twitter at

11:16.460 --> 11:17.299
Milhouse 30.

