Hello, this is Dr. Miller and this
is Episode fourteen point four of
Assembly.
Today, we're going to talk about
memory allocation using Malic and
then using that within structures
and unions using Mallick.
So memory allocation using
Mallik, so we
have the function called Mallik,
which is memory allocate
and it is in standard LibertÃÂ©.
And so what this does is it allows
us to dynamically allocate memory.
And the easiest way is to
just allocate memory, which is
called Marlock.
If we have some data that we
want to resize, we can call
Reilich. And so that will change
the size. Generally, you'll
make that size larger so
that you can store something bigger
than what you initially thought.
And then we also have Kellock.
So that's clear allocate
and that will set all of the values
to zero.
And then when you're done using that
memory, you can give it back
using the function called free.
So Mallock will allocate the memory
and then free will give it back to
the heap.
And so we have this notion that we
have the stack, which we've talked
about in class, we have some sort of
area of free memory that we
can use. So every time we push and
pop things from the stack,
they go into or out of that free
memory. But then we also have a
region called the heap.
And so the heap is where
we can dynamically allocate memory
for maybe we don't need a particular
amount any time.
So, for example, if
you have a text editor and
you want to be able to load one
kilobyte files, it might allocate
one kilobyte worth of memory.
But if you have a need for,
let's say, one gigabyte of memory,
then you can allocate that in the
heap without making your program
bloated when it's small.
And then when you do these
allocations, you can get
a couple of things, either a stack
overflow or you can run out of
memory. So you try and allocate
memory, not look and you don't have
any.
Or if you have a function that just
keeps growing and you just keep
calling it over and over again, you
can get a stack overflow,
often due to a mistake.
So here's an example, so we have
a value into pointer called X
and we allocate the size of one
integer and
that would allow us to just store
one item.
But then we could also have an array
and so an array we would have the
size of an integer times how many
items we want in there.
So the size of an int is
going to be four bytes.
And so if we need 100,
then we would get four hundred bytes
for that.
And then here you can see we're
setting index 99
to the value of negative one.
So here we can see our allocation
of memory. And so when we look at
the mallock, we can see that it's
pushing the number four on
top of the stack and then it's
calling mallock.
And so that's going to allocate four
bytes here.
It's doing a subtract of 12 so that
it can keep the memory addresses
on even by boundaries.
And then we're going to go ahead and
modify ESP
by 16, because that's what we did.
We did 12 plus four and that gives
us 16.
And then we can see that the result
is stored in a local variable.
So SBP minus 12.
The next one is we set the value of
X to two hundred and fifty five
and so two hundred and fifty five
ends up being
right here.
And so we can see that we load that
address into X
and then we dereference it.
And so with just a regular
endpoint or we we end up doing
our dereference and so that's the
brackets around X and
we assign the value at that
location.
Here we say size is one hundred, so
sizes EBP minus 16.
And then we're going to use size in
our allocation and so we go ahead
and load that pointer,
load that value into X,
and then it ends up doing a shift
right on it so it knows that
an integer is four.
And so instead of multiplying and
just does a shift,
then we again subtract 12 and
then push that value of X.
So it should have one hundred and
then shifted by the right.
So it should be four hundred and
that gets pushed onto the stack and
then mallock will allocate that
amount of memory.
But then the interesting part here
is that we see that in the
next slide, we're going to go ahead
and try and set the array at
a specific offset.
So in this example,
negative ninety nine to negative
one.
And so what it's going to do is it's
going to load that pointer that it's
saved at EBP minus 20 into
X, and then it's
going to add the offset.
So it already calculated ninety nine
times for gives us three hundred
ninety six.
That pointer is updated
by doing an ad and then here
removing negative one into the
address at that location.
And so this is how is standard
use of Nowack is.
And so we can see that we have
some examples of just a single
integer pointer and then we have an
entire array.
And from the compiler, those are
basically the same thing.
So, Malik, for structures and
unions.
So here we're just going to use a
standard socket is the
example, and so here we are
allocating some space
using the size of a structure
socket.
And so here I added a little bit
more code so we could kind of see
how this works, so we
can see here we
have a soccer ball and we just set
it to a value so we could see what
it is. So far is two hundred and
fifty.
So now it's going to go ahead and do
the calculation so it knows what the
size of a struct sock
atter underscore in is,
and that happens to be 16.
So we can see a push of 16
right here before we do our MALLIK.
So that means that a socket
atter in is 16
bytes.
So that gets allocated, then gets
saved at SBP
minus 16.
And then here we are calling the
function socket,
and so it's using af net and
so extreme and so you can
see that those parameters are set
and then we also have a zero.
So those are set inside
of there. We call the socket and
then we save the value into SBP
minus 12.
And then again, we're going to load
it into do our comparison.
So there's a lot packed into
this statement here.
So as you can see, that translates
to quite a bit of assembly
because we have to do the allocation
and then also do our compare
them again. So it uses a shift right
by thirty one.
And so that will test that top bit
and see if it is a one or not.
And then it can just use test A-L to
see if that topic was set or not
set.
So then we see that we're using the
dereference or the arrow operator
and so we can see the in
family, it's it gets set to af
net.
And so here we can see that that
address is loaded into yak's.
And then we are doing no offset here
because this is actually the first
element of the structure and
we're giving it the value too.
So A.F. Underscore INET
has the number two and so that gets
saved directly into there.
But the second one we're going to do
is going to be the sin port
here.
And so we can see here it's doing
the call to H to
Ness, so we can see that's
being done at the beginning, but
then we can see again, we load the
same pointer into,
um.
Looks like IDEX and
then we're setting ax into
that location, so it's
X plus two, and so
that plus two means that the port is
that offset too.
And also we can look at the both of
these and we can see that it's a
word pointer that it's saving to
those.
And so that means that each one of
these is two bytes.
So this is it offset zero and
then this is offset to and
then we can see that it modified the
address so that it got offset to
inside of their.
So today, we talked about memory
allocation using mallock
and then how that would manifest
itself in structures and you unions'
using Nowack.
Looking forward, we're going to give
some examples and we'll do an
example from start to finish
using a structure.
And Malik.
And then for our quiz.
So here we can see we have some code
here, so we have a structure, we
have a short and end, a character in
another short.
And so your your job is to figure
out what would the offset of CB and
what would the offset of DB.
So I'll let you go ahead and figure
those out.
So if we disassemble it
and look at it, we can see that sea
has offset of plus eight
and D has offset of plus
10.
So remember, you got to figure out
how much each one of these is going
to take up in in space.
So sure, it's going to take up to
round up to two.
So that will take up four bytes
because an integer is next.
We got four and eight and
then a character will take up
two if it's not followed by another
character, because the short needs
to be on a two by boundary.
So you have questions, you can email
me millirems at you in Canada, Edu,
and you can find me on Twitter at
Know How 30.