WEBVTT

00:01.500 --> 00:03.209
Hello, this is Dr. Miller, and this

00:03.210 --> 00:04.980
is Episode seven of Assembly.

00:06.640 --> 00:07.809
Today, we're going to talk about

00:07.810 --> 00:08.890
calling conventions,

00:10.210 --> 00:12.189
calling conventions, so

00:12.190 --> 00:13.479
calling conventions are the

00:13.480 --> 00:15.519
assumption that the software makes

00:15.520 --> 00:17.499
about a how a collar and a collie

00:17.500 --> 00:18.500
work.

00:18.610 --> 00:20.199
And so a color is a function that's

00:20.200 --> 00:21.549
going to call some function.

00:21.550 --> 00:23.469
A call is a function that is being

00:23.470 --> 00:24.699
called and entered.

00:25.660 --> 00:26.769
And so we have several different

00:26.770 --> 00:28.389
calling mentions that you will need

00:28.390 --> 00:29.390
to be aware of.

00:29.920 --> 00:32.320
We have see DEKLE Standard call

00:32.350 --> 00:33.673
this call and the different

00:34.270 --> 00:36.099
versions of Fast Call, so 30 to

00:36.160 --> 00:38.289
bit call and sixty for Bedfast

00:38.290 --> 00:39.290
call.

00:40.300 --> 00:41.709
Thirty two big conventions.

00:42.790 --> 00:44.619
So the sea declaration is

00:44.620 --> 00:46.530
the sea calling convention,

00:46.540 --> 00:48.280
so this was created by the

00:48.460 --> 00:50.020
people who created Eunuch's,

00:50.470 --> 00:51.548
and so they made a way

00:52.630 --> 00:54.669
of declaring

00:54.670 --> 00:56.799
function calls that

00:56.830 --> 00:58.569
the function that calls

00:58.720 --> 01:00.610
the COLLY.

01:02.190 --> 01:03.856
That the color function will clean

01:03.930 --> 01:05.859
up the stack, so this is fastened

01:05.860 --> 01:07.739
the standard call and

01:07.740 --> 01:09.059
then parameters are pushed down the

01:09.060 --> 01:10.889
stack, as we've done in right

01:10.890 --> 01:11.890
to left order.

01:13.040 --> 01:14.389
So, for example, at the end of the

01:14.390 --> 01:16.339
function, if you see a pop

01:16.340 --> 01:18.260
of SBP and then a return,

01:18.620 --> 01:19.845
then this is probably a C

01:20.630 --> 01:21.830
Dekle call.

01:23.090 --> 01:24.220
So here's an example.

01:24.230 --> 01:25.999
So we have this procedure called

01:26.000 --> 01:27.969
four zero one nine eight four.

01:28.010 --> 01:29.149
So this is out of reverse

01:29.150 --> 01:30.150
engineering.

01:30.380 --> 01:31.729
We can see that we pushed on two

01:31.730 --> 01:33.319
parameters and then we just pop

01:33.320 --> 01:34.850
those off into X.

01:35.480 --> 01:36.529
And then as we're going to the

01:36.530 --> 01:38.419
function, we'll see our our

01:38.420 --> 01:40.610
entry. So push up movie BPE,

01:40.670 --> 01:41.670
ESP if you see a pop

01:42.540 --> 01:44.020
SBP and then a return.

01:44.030 --> 01:45.499
And so we don't see any additional

01:45.500 --> 01:46.500
return on their

01:48.380 --> 01:49.549
now standard call.

01:49.550 --> 01:51.260
The Colly cleans up the stack

01:51.710 --> 01:53.149
and this is going to be easier to

01:53.150 --> 01:54.499
use except for when you have

01:54.500 --> 01:55.719
variable arguments.

01:55.760 --> 01:57.739
So an easy example

01:57.740 --> 01:58.999
to think about for variable

01:59.000 --> 02:00.980
arguments is the printout function.

02:01.550 --> 02:02.959
So printf you can give it one

02:02.960 --> 02:04.819
argument two, three, four or five,

02:04.820 --> 02:05.996
six, as many as you want

02:06.830 --> 02:08.360
as long as you have formatters in

02:08.389 --> 02:09.349
there.

02:09.350 --> 02:10.579
And so in that case, it's very

02:10.580 --> 02:12.148
difficult for it to clean up the

02:12.320 --> 02:13.729
stack in an appropriate way.

02:15.320 --> 02:17.084
And then in a standard call, we have

02:17.240 --> 02:19.069
registers like X, X

02:19.070 --> 02:20.120
and X.

02:20.450 --> 02:22.116
Our caller saved, which means that

02:22.340 --> 02:24.139
if you care about the results of

02:24.170 --> 02:25.885
those registers or the contents and

02:26.060 --> 02:27.049
you're worried about them being

02:27.050 --> 02:28.422
blown away, then you need to

02:28.910 --> 02:30.999
save them and then the COLLY

02:31.010 --> 02:32.774
has to save any other registers that

02:32.780 --> 02:33.830
it would modify.

02:35.020 --> 02:36.588
And so this standard call is the

02:36.820 --> 02:37.820
default for C and

02:38.650 --> 02:40.779
C plus programing on Microsoft

02:40.780 --> 02:42.139
Win32 API.

02:42.760 --> 02:44.379
So if you do any work with Windows

02:44.380 --> 02:46.095
32, you might have to deal with the

02:46.240 --> 02:47.889
fact that you're going to have a

02:47.890 --> 02:49.270
different calling convention.

02:49.810 --> 02:51.189
And so at the end, instead of just

02:51.190 --> 02:52.513
saying return, we would say

02:53.020 --> 02:54.784
Rete 12 and that would mean subtract

02:55.150 --> 02:56.889
12 by itself, top of the stack.

02:58.330 --> 02:59.970
So here, here's an example.

03:00.400 --> 03:01.809
So if you notice here, it says

03:01.810 --> 03:02.850
standard call.

03:02.890 --> 03:04.719
So the reverse engineering software

03:04.720 --> 03:06.759
can tell that this is

03:06.760 --> 03:07.760
a standard call.

03:08.110 --> 03:09.874
And we see at the end we have return

03:10.180 --> 03:11.552
N, which means N is how many

03:12.100 --> 03:13.179
bytes are going to pop off the

03:13.180 --> 03:14.079
stack.

03:14.080 --> 03:15.599
So it does its prolog does part

03:16.090 --> 03:17.619
of the epilog and then does this

03:17.620 --> 03:18.620
return.

03:18.880 --> 03:20.399
And so then you know that these

03:20.650 --> 03:22.149
bytes aren't going to be saved on

03:22.150 --> 03:23.780
the stack in the end.

03:23.800 --> 03:25.269
And so you have to take that into

03:25.270 --> 03:26.270
account.

03:28.350 --> 03:29.820
There's also the this call, so

03:30.330 --> 03:31.330
in C++, we have this

03:32.190 --> 03:33.479
notion that there is a this

03:33.480 --> 03:34.656
variable, so this points

03:35.550 --> 03:37.800
to the current object and it has a

03:38.010 --> 03:39.088
set of attributes that

03:39.870 --> 03:41.849
are linked on the stack, as well as

03:41.850 --> 03:43.499
functions that are linked onto the

03:43.500 --> 03:44.500
stack.

03:44.610 --> 03:46.325
And so all of this is pointed to by

03:46.380 --> 03:47.789
the X register.

03:47.970 --> 03:49.349
And so this register will point to

03:49.350 --> 03:50.519
those variables and those

03:50.520 --> 03:51.520
attributes.

03:51.630 --> 03:53.879
And then if you see X being used

03:53.880 --> 03:55.949
as a pointer in some notion,

03:55.960 --> 03:57.577
then what you're probably looking

03:57.810 --> 03:59.809
at is C++ code.

04:00.330 --> 04:01.659
And so here's an example.

04:02.490 --> 04:04.349
So it thinks that this is

04:04.350 --> 04:05.350
a this call.

04:06.330 --> 04:08.159
And when we get in here, we see that

04:08.640 --> 04:10.409
at no place up here, X

04:10.620 --> 04:12.869
has a value set, but X

04:12.870 --> 04:14.669
is getting moved into S.I

04:14.850 --> 04:16.740
and then that is being dereference.

04:17.250 --> 04:19.014
And so that means that this is a C++

04:19.769 --> 04:21.337
function where the assumption is

04:21.750 --> 04:24.029
X points to basically

04:24.030 --> 04:25.030
a function or a set

04:26.130 --> 04:26.969
of attributes.

04:26.970 --> 04:28.919
And so then that's being referenced.

04:29.130 --> 04:30.551
So here it's moving some data

04:31.170 --> 04:32.249
into that location.

04:32.250 --> 04:33.749
But that was only initialized from

04:33.750 --> 04:34.750
X, which wasn't set

04:35.910 --> 04:36.910
in the program.

04:37.950 --> 04:39.269
And then you can see that it also

04:39.270 --> 04:40.270
might return.

04:40.470 --> 04:42.599
Right, return for

04:42.630 --> 04:43.979
off the top of the stack.

04:44.010 --> 04:45.269
So that would be of this call.

04:47.260 --> 04:49.209
And then we have fast call, so part

04:49.210 --> 04:50.778
of the problem with the standard

04:51.250 --> 04:52.720
way that we pass parameters to

04:52.990 --> 04:55.120
functions is the fact that

04:55.390 --> 04:56.709
arguments have to be put on the

04:56.710 --> 04:58.749
stack and the stack is a much

04:58.750 --> 04:59.750
slower piece

05:00.610 --> 05:02.359
of memory than registers.

05:02.800 --> 05:04.417
And so if you're only using maybe

05:04.510 --> 05:06.339
one or two registers instead of

05:06.340 --> 05:07.689
throwing them onto the stack and

05:07.690 --> 05:09.307
then doing your function and then

05:09.490 --> 05:11.859
popping them back off or subtracting

05:11.860 --> 05:13.526
from the stack, they just put them

05:13.720 --> 05:14.720
in the registers.

05:15.490 --> 05:17.519
And so for the 30 bit Microsoft fast

05:17.530 --> 05:19.389
call, it uses the registers

05:19.390 --> 05:20.949
of X and IDEX

05:21.220 --> 05:22.569
and then the rest of the parameters

05:22.570 --> 05:23.529
are put on the stack.

05:23.530 --> 05:25.209
So if you only have two arguments,

05:25.570 --> 05:26.919
then it's going to run faster

05:26.920 --> 05:28.179
because they'll be in registers

05:28.360 --> 05:29.559
instead of on the stack.

05:31.650 --> 05:33.230
Sixty four bit conventions,

05:34.170 --> 05:36.209
so Microsoft has to find a 64

05:36.210 --> 05:37.210
bit convention and

05:38.040 --> 05:39.929
with 64 bit we have instead

05:39.930 --> 05:42.080
of just X, we have R6.

05:42.090 --> 05:44.279
So all of these are replaced by R

05:44.790 --> 05:45.749
and then there's additional

05:45.750 --> 05:47.069
registers that were beyond the

05:47.070 --> 05:48.070
normal ones.

05:48.270 --> 05:49.593
And so we can use our eight

05:50.250 --> 05:51.720
and our nine as our first four

05:52.140 --> 05:53.759
integer pointers or

05:54.330 --> 05:56.189
or integers in that order.

05:57.560 --> 05:59.079
And then for floating point, so

05:59.420 --> 06:01.339
the XM registers are

06:01.340 --> 06:02.779
the floating point registers that

06:02.780 --> 06:04.430
you can use in a program

06:04.850 --> 06:06.529
for doing floating point

06:07.490 --> 06:08.960
arithmetic that came with some

06:09.560 --> 06:11.510
later architectures.

06:12.800 --> 06:15.109
And so those first four

06:15.140 --> 06:16.659
zero through three are used for

06:16.670 --> 06:17.670
floating points.

06:18.200 --> 06:20.449
And then the color

06:20.450 --> 06:22.129
also has to allocate thirty two

06:22.130 --> 06:23.649
bytes of shadow space so we can

06:24.050 --> 06:25.079
save these registers.

06:25.250 --> 06:26.524
Asks already Zarit and are

06:27.230 --> 06:28.230
nine and then

06:29.120 --> 06:30.649
all of these registers are

06:30.650 --> 06:32.267
considered volatile, so the color

06:32.480 --> 06:33.529
has to save them.

06:34.280 --> 06:35.359
And then the rest of them are

06:35.360 --> 06:36.209
nonvolatile.

06:36.210 --> 06:38.189
Nicholai has to save them.

06:38.810 --> 06:40.035
And so these help to make

06:40.940 --> 06:42.439
it so that function calls can run

06:42.440 --> 06:44.057
faster and not have to access the

06:44.210 --> 06:45.210
stack.

06:45.230 --> 06:47.119
And with 64 bit, we end up

06:47.120 --> 06:47.989
with more registers.

06:47.990 --> 06:49.279
So you can see that we have

06:49.280 --> 06:50.456
registers like our eight

06:51.290 --> 06:52.662
or nine or 10 all the way up

06:53.180 --> 06:54.180
to our 15.

06:54.590 --> 06:56.207
And so these additional registers

06:56.390 --> 06:58.009
allow us to access

06:58.370 --> 06:59.370
more memory easier

07:00.380 --> 07:01.369
in sixty four bit.

07:01.370 --> 07:03.134
And that's one of the reasons why 64

07:03.380 --> 07:04.879
bit can be faster because it has

07:04.880 --> 07:06.919
more registers and the registers

07:06.920 --> 07:07.970
are faster than RAM.

07:10.420 --> 07:12.279
And then we have so for ARM, we have

07:12.280 --> 07:13.554
the system five sixty four

07:14.170 --> 07:16.389
bit ABI or application

07:16.390 --> 07:18.309
binary interface and this

07:18.310 --> 07:19.535
defines that it registers

07:20.470 --> 07:22.300
like Ardai RSI

07:22.930 --> 07:24.759
are what the pointers

07:24.760 --> 07:25.719
can be put into.

07:25.720 --> 07:27.159
So it just has a different set of

07:27.160 --> 07:28.389
registers that we can use.

07:28.960 --> 07:30.699
And then floating point registers

07:31.390 --> 07:33.279
zero through seven or eight of

07:33.280 --> 07:34.290
them can be used.

07:34.330 --> 07:35.620
Right. And so that's the

07:36.400 --> 07:37.400
64 bit application

07:39.010 --> 07:40.839
binary interface that can be used

07:42.790 --> 07:43.790
with the AMD 64

07:44.920 --> 07:45.920
bit.

07:46.850 --> 07:47.850
Architecture.

07:49.500 --> 07:50.940
So today, we talked about some of

07:50.970 --> 07:52.700
the different calling conventions,

07:52.750 --> 07:54.989
the standard see Dekel this

07:54.990 --> 07:56.009
and then a couple of different

07:56.010 --> 07:57.670
variants of Fast Call.

07:59.070 --> 08:00.419
Looking forward, we're going to look

08:00.420 --> 08:01.769
at local variables.

08:01.770 --> 08:03.569
So local variables are a place where

08:03.570 --> 08:05.100
we can temporarily store data

08:05.580 --> 08:06.959
inside of our functions and we'll

08:06.960 --> 08:09.000
look at how we can save data there

08:09.030 --> 08:10.030
or read data

08:11.100 --> 08:12.269
from local variables.

08:14.380 --> 08:15.809
So which registers I use for the

08:15.810 --> 08:17.476
first two parameters on Microsoft,

08:17.970 --> 08:19.260
32 bit call.

08:21.630 --> 08:23.129
Echikson IDEX,

08:25.110 --> 08:26.069
if you have questions, you can

08:26.070 --> 08:27.393
contact me at Miller, MJ at

08:27.990 --> 08:29.754
Unclutter Edu and you can find me on

08:29.910 --> 08:31.380
Twitter at Milhouse 30.

