WEBVTT

00:00.300 --> 00:01.859
Hello, this is Dr. Miller and this

00:01.860 --> 00:03.449
is Episode fourteen point four of

00:03.450 --> 00:04.450
Assembly.

00:06.550 --> 00:07.629
Today, we're going to talk about

00:07.630 --> 00:09.247
memory allocation using Malic and

00:09.550 --> 00:11.167
then using that within structures

00:11.560 --> 00:12.849
and unions using Mallick.

00:15.030 --> 00:16.859
So memory allocation using

00:16.860 --> 00:18.689
Mallik, so we

00:18.690 --> 00:20.258
have the function called Mallik,

00:20.640 --> 00:22.699
which is memory allocate

00:22.710 --> 00:24.399
and it is in standard LibertÃÂ©.

00:25.590 --> 00:27.149
And so what this does is it allows

00:27.150 --> 00:29.310
us to dynamically allocate memory.

00:29.670 --> 00:31.499
And the easiest way is to

00:31.500 --> 00:32.879
just allocate memory, which is

00:32.880 --> 00:33.880
called Marlock.

00:34.320 --> 00:36.359
If we have some data that we

00:36.360 --> 00:38.219
want to resize, we can call

00:38.220 --> 00:39.788
Reilich. And so that will change

00:40.320 --> 00:41.669
the size. Generally, you'll

00:42.240 --> 00:44.099
make that size larger so

00:44.100 --> 00:45.599
that you can store something bigger

00:45.600 --> 00:46.799
than what you initially thought.

00:47.670 --> 00:48.999
And then we also have Kellock.

00:49.020 --> 00:50.729
So that's clear allocate

00:51.130 --> 00:52.845
and that will set all of the values

00:52.950 --> 00:53.950
to zero.

00:54.600 --> 00:56.039
And then when you're done using that

00:56.040 --> 00:58.109
memory, you can give it back

00:58.800 --> 01:00.210
using the function called free.

01:00.420 --> 01:02.135
So Mallock will allocate the memory

01:02.430 --> 01:04.096
and then free will give it back to

01:04.260 --> 01:05.260
the heap.

01:06.030 --> 01:07.409
And so we have this notion that we

01:07.410 --> 01:09.076
have the stack, which we've talked

01:09.150 --> 01:11.010
about in class, we have some sort of

01:11.400 --> 01:12.723
area of free memory that we

01:13.290 --> 01:14.956
can use. So every time we push and

01:15.090 --> 01:16.500
pop things from the stack,

01:17.010 --> 01:18.599
they go into or out of that free

01:18.600 --> 01:20.119
memory. But then we also have a

01:20.190 --> 01:21.390
region called the heap.

01:22.170 --> 01:24.089
And so the heap is where

01:24.090 --> 01:25.756
we can dynamically allocate memory

01:26.250 --> 01:28.014
for maybe we don't need a particular

01:28.140 --> 01:29.700
amount any time.

01:29.730 --> 01:30.730
So, for example, if

01:31.650 --> 01:33.659
you have a text editor and

01:33.660 --> 01:35.069
you want to be able to load one

01:35.070 --> 01:37.079
kilobyte files, it might allocate

01:37.860 --> 01:39.629
one kilobyte worth of memory.

01:40.110 --> 01:41.999
But if you have a need for,

01:42.000 --> 01:43.709
let's say, one gigabyte of memory,

01:43.710 --> 01:45.269
then you can allocate that in the

01:45.270 --> 01:47.489
heap without making your program

01:47.490 --> 01:48.569
bloated when it's small.

01:49.530 --> 01:50.609
And then when you do these

01:50.610 --> 01:51.786
allocations, you can get

01:52.680 --> 01:54.329
a couple of things, either a stack

01:54.330 --> 01:55.769
overflow or you can run out of

01:55.770 --> 01:57.289
memory. So you try and allocate

01:57.300 --> 01:58.859
memory, not look and you don't have

01:58.860 --> 01:59.669
any.

01:59.670 --> 02:01.169
Or if you have a function that just

02:01.170 --> 02:02.549
keeps growing and you just keep

02:02.550 --> 02:03.869
calling it over and over again, you

02:03.870 --> 02:05.159
can get a stack overflow,

02:06.000 --> 02:07.170
often due to a mistake.

02:09.389 --> 02:11.609
So here's an example, so we have

02:12.030 --> 02:14.279
a value into pointer called X

02:14.280 --> 02:16.379
and we allocate the size of one

02:16.380 --> 02:17.380
integer and

02:18.240 --> 02:19.857
that would allow us to just store

02:20.190 --> 02:20.999
one item.

02:21.000 --> 02:22.830
But then we could also have an array

02:23.400 --> 02:24.839
and so an array we would have the

02:24.840 --> 02:26.457
size of an integer times how many

02:26.490 --> 02:27.730
items we want in there.

02:28.410 --> 02:30.389
So the size of an int is

02:30.390 --> 02:31.940
going to be four bytes.

02:31.950 --> 02:34.169
And so if we need 100,

02:34.170 --> 02:36.419
then we would get four hundred bytes

02:36.420 --> 02:37.420
for that.

02:37.800 --> 02:38.879
And then here you can see we're

02:38.880 --> 02:41.129
setting index 99

02:41.130 --> 02:43.139
to the value of negative one.

02:45.170 --> 02:47.179
So here we can see our allocation

02:47.180 --> 02:48.797
of memory. And so when we look at

02:48.860 --> 02:50.209
the mallock, we can see that it's

02:50.210 --> 02:51.484
pushing the number four on

02:52.160 --> 02:53.630
top of the stack and then it's

02:53.930 --> 02:54.709
calling mallock.

02:54.710 --> 02:56.299
And so that's going to allocate four

02:56.300 --> 02:57.879
bytes here.

02:57.890 --> 02:59.605
It's doing a subtract of 12 so that

02:59.780 --> 03:01.969
it can keep the memory addresses

03:01.970 --> 03:03.229
on even by boundaries.

03:03.410 --> 03:04.849
And then we're going to go ahead and

03:04.850 --> 03:07.190
modify ESP

03:07.220 --> 03:08.886
by 16, because that's what we did.

03:08.900 --> 03:10.519
We did 12 plus four and that gives

03:10.520 --> 03:11.520
us 16.

03:11.960 --> 03:13.249
And then we can see that the result

03:13.250 --> 03:14.720
is stored in a local variable.

03:14.930 --> 03:16.460
So SBP minus 12.

03:18.500 --> 03:19.999
The next one is we set the value of

03:20.000 --> 03:21.560
X to two hundred and fifty five

03:21.980 --> 03:23.839
and so two hundred and fifty five

03:24.230 --> 03:25.230
ends up being

03:26.450 --> 03:27.289
right here.

03:27.290 --> 03:28.789
And so we can see that we load that

03:28.790 --> 03:29.790
address into X

03:30.830 --> 03:32.180
and then we dereference it.

03:32.300 --> 03:34.189
And so with just a regular

03:34.190 --> 03:36.139
endpoint or we we end up doing

03:36.140 --> 03:37.549
our dereference and so that's the

03:37.550 --> 03:38.579
brackets around X and

03:39.470 --> 03:40.759
we assign the value at that

03:40.760 --> 03:41.760
location.

03:44.180 --> 03:46.039
Here we say size is one hundred, so

03:46.040 --> 03:48.139
sizes EBP minus 16.

03:49.350 --> 03:51.065
And then we're going to use size in

03:51.090 --> 03:53.009
our allocation and so we go ahead

03:53.010 --> 03:54.539
and load that pointer,

03:55.980 --> 03:57.629
load that value into X,

03:57.930 --> 03:59.849
and then it ends up doing a shift

03:59.850 --> 04:01.289
right on it so it knows that

04:01.740 --> 04:02.909
an integer is four.

04:02.940 --> 04:04.199
And so instead of multiplying and

04:04.200 --> 04:05.200
just does a shift,

04:06.060 --> 04:07.889
then we again subtract 12 and

04:07.890 --> 04:09.620
then push that value of X.

04:09.630 --> 04:11.639
So it should have one hundred and

04:11.640 --> 04:12.900
then shifted by the right.

04:13.650 --> 04:15.479
So it should be four hundred and

04:15.480 --> 04:16.768
that gets pushed onto the stack and

04:16.769 --> 04:18.288
then mallock will allocate that

04:18.750 --> 04:19.750
amount of memory.

04:21.190 --> 04:22.599
But then the interesting part here

04:22.600 --> 04:23.874
is that we see that in the

04:24.460 --> 04:25.809
next slide, we're going to go ahead

04:25.810 --> 04:27.182
and try and set the array at

04:27.910 --> 04:29.399
a specific offset.

04:29.420 --> 04:30.610
So in this example,

04:31.360 --> 04:32.919
negative ninety nine to negative

04:32.920 --> 04:33.920
one.

04:34.420 --> 04:35.469
And so what it's going to do is it's

04:35.470 --> 04:36.789
going to load that pointer that it's

04:36.790 --> 04:38.739
saved at EBP minus 20 into

04:38.740 --> 04:39.740
X, and then it's

04:40.600 --> 04:42.249
going to add the offset.

04:42.250 --> 04:44.169
So it already calculated ninety nine

04:44.200 --> 04:45.910
times for gives us three hundred

04:45.930 --> 04:46.930
ninety six.

04:47.290 --> 04:49.149
That pointer is updated

04:49.150 --> 04:50.522
by doing an ad and then here

04:50.980 --> 04:52.720
removing negative one into the

04:53.230 --> 04:54.649
address at that location.

04:54.970 --> 04:56.619
And so this is how is standard

04:57.340 --> 04:58.649
use of Nowack is.

04:58.660 --> 05:00.399
And so we can see that we have

05:00.730 --> 05:02.200
some examples of just a single

05:02.260 --> 05:03.975
integer pointer and then we have an

05:04.060 --> 05:05.060
entire array.

05:05.380 --> 05:06.639
And from the compiler, those are

05:06.640 --> 05:07.930
basically the same thing.

05:09.720 --> 05:10.919
So, Malik, for structures and

05:10.920 --> 05:11.920
unions.

05:12.940 --> 05:14.469
So here we're just going to use a

05:14.470 --> 05:16.329
standard socket is the

05:16.330 --> 05:17.653
example, and so here we are

05:18.220 --> 05:19.600
allocating some space

05:20.200 --> 05:21.939
using the size of a structure

05:21.940 --> 05:22.940
socket.

05:24.360 --> 05:25.928
And so here I added a little bit

05:25.980 --> 05:27.329
more code so we could kind of see

05:27.330 --> 05:29.189
how this works, so we

05:29.190 --> 05:31.019
can see here we

05:31.020 --> 05:32.879
have a soccer ball and we just set

05:32.880 --> 05:34.379
it to a value so we could see what

05:34.380 --> 05:35.948
it is. So far is two hundred and

05:35.970 --> 05:36.970
fifty.

05:38.090 --> 05:39.409
So now it's going to go ahead and do

05:39.410 --> 05:41.089
the calculation so it knows what the

05:41.090 --> 05:42.410
size of a struct sock

05:43.820 --> 05:45.850
atter underscore in is,

05:45.860 --> 05:47.509
and that happens to be 16.

05:47.510 --> 05:49.279
So we can see a push of 16

05:49.670 --> 05:51.319
right here before we do our MALLIK.

05:51.320 --> 05:52.879
So that means that a socket

05:53.180 --> 05:54.180
atter in is 16

05:55.370 --> 05:56.370
bytes.

05:56.580 --> 05:58.129
So that gets allocated, then gets

05:58.130 --> 05:59.689
saved at SBP

06:00.350 --> 06:01.350
minus 16.

06:02.960 --> 06:04.399
And then here we are calling the

06:04.400 --> 06:05.929
function socket,

06:06.350 --> 06:07.722
and so it's using af net and

06:08.360 --> 06:09.585
so extreme and so you can

06:10.310 --> 06:11.927
see that those parameters are set

06:12.500 --> 06:13.949
and then we also have a zero.

06:14.360 --> 06:15.487
So those are set inside

06:16.310 --> 06:17.878
of there. We call the socket and

06:18.170 --> 06:19.959
then we save the value into SBP

06:19.970 --> 06:20.970
minus 12.

06:21.410 --> 06:23.389
And then again, we're going to load

06:23.390 --> 06:24.790
it into do our comparison.

06:24.800 --> 06:26.839
So there's a lot packed into

06:26.840 --> 06:27.739
this statement here.

06:27.740 --> 06:29.239
So as you can see, that translates

06:29.240 --> 06:31.069
to quite a bit of assembly

06:31.070 --> 06:32.449
because we have to do the allocation

06:32.450 --> 06:34.279
and then also do our compare

06:34.760 --> 06:36.679
them again. So it uses a shift right

06:36.680 --> 06:37.669
by thirty one.

06:37.670 --> 06:39.319
And so that will test that top bit

06:39.320 --> 06:41.139
and see if it is a one or not.

06:41.570 --> 06:43.399
And then it can just use test A-L to

06:43.400 --> 06:44.968
see if that topic was set or not

06:45.110 --> 06:46.110
set.

06:48.100 --> 06:49.815
So then we see that we're using the

06:49.930 --> 06:51.839
dereference or the arrow operator

06:51.880 --> 06:54.040
and so we can see the in

06:54.790 --> 06:56.679
family, it's it gets set to af

06:56.680 --> 06:57.459
net.

06:57.460 --> 06:59.079
And so here we can see that that

07:00.040 --> 07:01.461
address is loaded into yak's.

07:01.990 --> 07:03.754
And then we are doing no offset here

07:03.940 --> 07:05.529
because this is actually the first

07:06.400 --> 07:08.259
element of the structure and

07:08.260 --> 07:09.429
we're giving it the value too.

07:09.430 --> 07:11.199
So A.F. Underscore INET

07:11.650 --> 07:13.329
has the number two and so that gets

07:13.330 --> 07:14.709
saved directly into there.

07:15.580 --> 07:17.079
But the second one we're going to do

07:17.080 --> 07:18.940
is going to be the sin port

07:19.780 --> 07:20.780
here.

07:22.390 --> 07:24.339
And so we can see here it's doing

07:24.340 --> 07:25.340
the call to H to

07:26.200 --> 07:28.119
Ness, so we can see that's

07:28.120 --> 07:29.559
being done at the beginning, but

07:29.560 --> 07:30.879
then we can see again, we load the

07:30.880 --> 07:32.620
same pointer into,

07:33.280 --> 07:34.280
um.

07:35.640 --> 07:36.640
Looks like IDEX and

07:37.560 --> 07:38.834
then we're setting ax into

07:39.660 --> 07:41.339
that location, so it's

07:41.640 --> 07:43.559
X plus two, and so

07:43.560 --> 07:45.269
that plus two means that the port is

07:45.270 --> 07:46.350
that offset too.

07:47.010 --> 07:48.509
And also we can look at the both of

07:48.510 --> 07:49.589
these and we can see that it's a

07:49.590 --> 07:51.449
word pointer that it's saving to

07:51.450 --> 07:52.319
those.

07:52.320 --> 07:53.700
And so that means that each one of

07:53.730 --> 07:54.719
these is two bytes.

07:54.720 --> 07:56.141
So this is it offset zero and

07:56.610 --> 07:58.589
then this is offset to and

07:58.590 --> 08:00.269
then we can see that it modified the

08:00.270 --> 08:02.279
address so that it got offset to

08:02.280 --> 08:03.280
inside of their.

08:05.590 --> 08:07.089
So today, we talked about memory

08:07.090 --> 08:08.439
allocation using mallock

08:08.950 --> 08:10.518
and then how that would manifest

08:10.810 --> 08:12.574
itself in structures and you unions'

08:13.180 --> 08:14.180
using Nowack.

08:15.720 --> 08:16.949
Looking forward, we're going to give

08:16.950 --> 08:18.359
some examples and we'll do an

08:18.360 --> 08:20.519
example from start to finish

08:20.520 --> 08:21.359
using a structure.

08:21.360 --> 08:22.360
And Malik.

08:23.480 --> 08:24.740
And then for our quiz.

08:25.910 --> 08:27.889
So here we can see we have some code

08:27.890 --> 08:29.779
here, so we have a structure, we

08:29.780 --> 08:31.639
have a short and end, a character in

08:31.640 --> 08:32.640
another short.

08:32.990 --> 08:34.519
And so your your job is to figure

08:34.520 --> 08:36.469
out what would the offset of CB and

08:36.470 --> 08:37.890
what would the offset of DB.

08:38.240 --> 08:39.955
So I'll let you go ahead and figure

08:40.220 --> 08:41.220
those out.

08:45.210 --> 08:46.710
So if we disassemble it

08:47.310 --> 08:48.959
and look at it, we can see that sea

08:48.960 --> 08:50.820
has offset of plus eight

08:51.240 --> 08:53.399
and D has offset of plus

08:53.400 --> 08:54.400
10.

08:54.510 --> 08:55.829
So remember, you got to figure out

08:55.830 --> 08:57.449
how much each one of these is going

08:57.450 --> 08:59.309
to take up in in space.

08:59.700 --> 09:01.199
So sure, it's going to take up to

09:01.200 --> 09:02.129
round up to two.

09:02.130 --> 09:03.389
So that will take up four bytes

09:03.390 --> 09:04.580
because an integer is next.

09:04.630 --> 09:05.855
We got four and eight and

09:06.630 --> 09:08.039
then a character will take up

09:08.730 --> 09:10.349
two if it's not followed by another

09:10.350 --> 09:11.639
character, because the short needs

09:11.640 --> 09:12.989
to be on a two by boundary.

09:15.310 --> 09:16.869
So you have questions, you can email

09:16.870 --> 09:18.909
me millirems at you in Canada, Edu,

09:18.910 --> 09:20.339
and you can find me on Twitter at

09:20.350 --> 09:21.100
Know How 30.

