Hello. This is Dr Miller and this is Episode 14.4 of Assembly.
Today we're gonna talk about memory allocation using Malik and then using that within structures and unions using Malik.
So memory allocation using Malik.
So we have the function called Malik, which is memory allocate. And it is in standard lib dot h.
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 Malik.
If we have some data that we want to resize, we can call Rielle IQ, 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.
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. I'm using the function called free, so Malik will allocate the memory and then free will give it back to the heat.
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 in pop things from the stack,
they go into out of that free memory. But then we also have a region called the Heap,
and so the heat 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 less, they 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 is Malik, 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.
I'm 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,
um, and that would allow us to just store one item. But then we could also have an array.
And so on array, we would have the size of a manager times how many items we want in there.
So the size of an int is going to be four bites. And so if we need 100 then we would get 400 bytes for that.
And then here you can see we're setting index, um, 99 to the value of negative one.
So here we can see our allocation of memory. And so when we look at the Malik, we can see that it's pushing the number four on top of the stack. And then it's calling Malik. And so that's going to allocate four bites.
I'm here. It's doing a subtract of 12 so that it can keep the
memory addresses on even bite boundaries. And then we're going to go ahead and modify
um E S P 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 e B P minus 12.
The next one is we set the value of X to 255.
And so 255 ends up being,
right here and so we can see that we load that address, I'm in t X and then we d reference it. And so, with just a regular into pointer, we we end up doing our d reference. And so that's the brackets around E X. And we assigned the value at that location.
Here we say sizes 100 So sizes E B P minus 16
and then we're gonna use size in our allocation. And so we go ahead and load that pointer,
Um, load that value into yea X, and then it ends up doing a shift right on it. So it knows that
manager is four. And so, instead of multiplying and just as a shift,
then we again subtract 12 and then push that value of V X. So it should have 100
and then shifted by the right.
I'm so it should be 400 that gets pushed onto the stack. And then Malik 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,
um, negative 99 to negative one.
And so what it's gonna do is it's gonna load that pointer that it saved a T v p minus 20 into yea X.
And then it's gonna add the offset. So it already calculated 99 times four gives us 296.
That pointer is updated by doing an ad. And then here we're moving native one into the
address at that location. And so this is how a standard
use of Malik is, and so we can see that we have
some examples of just a single image or 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
when so here I added a little bit more code so we could kind of see how this works.
So we can see. Um, here we have a sock call, and we just said it to a value so we could see what it is. So FAA is 250
so now it's going to go ahead and do the calculation. So it knows what the size of a struck sock,
um, adder underscore in is and that happens to be 16. So we can see a push of 16
right here before we do. Our Malik so means that a socket
So I gets allocated, then get saved at E V P
And then here we are calling the function socket.
And so it's using a F net and sock stream,
and so you can see that those parameters are set and then we also have a zero. So those air set inside of there, we call the socket.
And then we saved the value into E. V P minus 12.
And then again, we're going to load it and 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
I'm again. It's so uses a shift right by 31. And so that will test that top bit and see if it is a one or not.
And then I could just use test a l to see if that top it was set or not set.
So then we see that we're using the D reference or the arrow operator and so we can see the S in
ah, family. It's it gets set to a F net. And so here we can see that that, um,
address is loaded into yea X and then we're doing no offset here because this is actually the first
element of the structure, and we're giving it the value to so f underscore. I net
has the number two, and so they get saved directly into there.
But the 2nd 1 we're gonna do is gonna be the sin port
and so we can see here. It's during the call to H two n s
so we can see that's being done at the beginning. But then we can see again. We load the same pointer into,
Um, and then we're setting X into that location. So it's E d X plus two,
and so that plus two means that the port is that offset to,
and also we can look into the both of these. You 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 bites. 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 there.
So today we talked about memory allocation using Malik
and then how that would manifest itself in structures and you unions using Malik
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 that we have some code here, So we have a structure. We have a short and end a character and 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 let you go ahead and
So if we disassemble it, um, and look at it, we can see that see, 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 space.
So shorts gonna take up to round up to two. So that will take up four bites, cause an imager is next. We got four and eight,
and then a character will take up to if it's not followed by another character because the short needs to be on a to bite boundary.
So questions you can email me, Miller MJ at you and Kate out you to you, and you can find me on Twitter at Milhouse 30.