Hello and welcome.
You may remember in a recent lecture we discussed what padding and alignment was.
Well, in this lecture, we're actually going to create some functions that deal with padding and alignment.
So why do we need to create those types of functions?
Because whilst parsing the bodies, we are going to have to align some offsets.
So why do we need to create some padding and alignment functions in this lecture?
Because whilst parsing bodies, we are going to need to align the variable sizes so that they're aligned
to those four by boundaries I discussed in the Padding and Alignment Lecture.
And since the primary objective, these past couple of lectures has been to implement code.
Preparing us for finishing the parsing of bodies and structures.
This is an appropriate time for us to implement those functions.
So what I want you to do, I want you to head over to help a dot C and we're just going to create some
very simple functions.
So we're going to start with padding.
So we're going to say in it padding and it file in it too.
So this will pad the value add to whatever this is.
So this is four and this value is 20.
It will pad it'll pad the number 20 up to those four by boundaries.
Okay.
So this is very simple to do.
We're going to say F two is below our equals zero.
Then there's nothing we can do to pad that and we're going to return zero.
We're now going to say F value modulus two is equal to zero, then return zero, otherwise return to
minus val modulus two and finally a modest two at the end.
So let's explain this.
So essentially if someone was to pass in 20 here, we would say, is 20 modulus four equal to zero?
And if it is, then there's no padding required.
So we return zero.
Otherwise we calculate the padding that is required.
So I'm going to open up Python just to show you how this works a little more mathematically.
And I don't have Python, so I'm going to install it and I'm just going to go Python.
And let's imagine the input value is 20, right?
So we're going to say 20 and the two variable is four.
So we want to align to four bytes.
I do that.
That is equal to zero.
So it's already aligned.
So there's zero bytes.
We need to add the 20 to make it align to four bytes.
Let's imagine another scenario.
We're going to do 21.
We can see that is not zero.
So we went onto line 37 and we do four -21 modulus for modulus four.
And what do we get?
We get three bytes.
So to pat this number, 21 to a four byte alignment, we need to add three bytes to it.
So this is what this function does.
It returns the amount of bytes we need to add to successfully pad a variable.
So now let's try 21 plus three, we get 24 is 24 aligned to four bytes.
So 24 modulus four is equal to zero.
So it's aligned.
So that's how padding works guys.
And we can really do this to any number someone can pass in five, three, two to the number to if they
want.
And it will, it will align it to a 532 byte boundary.
Right.
So, you know, it is very clever stuff.
And using this simple mathematics we can pad to any boundary we like because remember, the reason we're
aligning and padding is to avoid those double reads that I explained in the presentation about alignment.
We want to make it as easy as possible for the process to execute our code.
So we need to align and we need to pad OC.
So we're now going to create a alignment function.
So we're going to say in it a line, value in it, file in it too.
So.
Basically this is going to work out if we're not aligned and if we're not aligned, it's going to align
it.
So, you know, by here I added 21 plus three.
This is what the aligned value function will do.
It will figure out what the padding is and it will add to that to our value resulting in it being aligned.
Okay.
So we're going to say F Val Modulus two, and if this is equal to zero, as you know, that will mean
it's aligned.
Therefore, an F statement that takes a value of zero will not run.
So then we leave.
OC but if this is anything other than zero, then we know we're not aligned and we need to align.
So what do we do?
We go val plus equals padding val to OC because we know padding returns the amount of bytes we need
to add to the to the input value for it to be aligned.
So therefore we simply do the input value plus equals padding OC that's what that does and then it's
aligned.
So then we just return.
Val Simple as that.
That's our align value function, nice and easy.
So there can be times when someone might want to align a negative number.
So we're going to create a scenario for that and it aligned value be positive.
So we're going to pass in the value.
And the two first things first, we're going to assert that the two is above or equals zero OC We don't
want someone accidentally putting a negative value in the to misunderstanding the function.
And we're just going to say if the value is below zero, then two equals minus two.
So essentially if someone passes in -20 in Val and they pass in four in two, then four will become
minus four.
So we're aligning -20, it's a minus four OC and then we're just going to return a line value Val and
two OC.
So that's a nice simple function right there.
So the reason we need a function like this is because when dealing with things like stack variables,
we work down the stack.
So we're working with a lot of negative numbers and because of that we need special function for dealing
with that.
So essentially, if our values negative, it will basically just flip the two on its back kind of like
so it's negative as well so that it can still produce a correct value coming out of the aligned value
here.
Because if we rely just on a line value, we wouldn't get the same results we were expecting for negative
numbers.
So that's why we need this special function here that checks if it's negative and if it's negative,
it will treat it differently.
So I hope it makes sense so far.
I will test these functions with you before we end this lecture, just so you can get a bit of a better
idea of how they work.
Because I understand for people not familiar with padding and alignment, in most cases, you don't
need to be aware of these things.
In low level development, you absolutely need to understand padding and alignment.
So, you know, I will I will go through these functions with you.
So don't worry if you're confused, just keep keep following this.
So we're now going to create a some padding function.
So we're going to say init compute some padding struc vector vec.
That sounds pretty funny, doesn't it.
It's almost like I'm saying compute some padding, but now some padding, right?
So we're going to say init padding equals zero and it last type equals minus one.
We're going to say bull mix types equals false vector set peak pointer vector to zero struct node current
node equals vector peak pointer vec struct node last node equals null.
And then we're going to say, well, current node and guess what we're going to do in here?
The function should say at all, we're going to sum all of the padding in the vector OC and this function
will actually ignore any nodes that are not no type variable because the type of things that's going
to be passed into compute some padding literally going to be the statement vectors for bodies.
So you're going to have all sorts of things.
You're going to have return statement nodes, you're going to have if statement nodes loads and loads
of different things in there and most of it we don't care about.
But there will be some things we care about.
We care about variable nodes, for example, because this is what this function does.
It will compute the padding of all the variable nodes.
Okay.
So we're just going to say, if current node type does not equal variable, no type variable, I mean,
then current node equals vector peak pointer VEC and we're going to continue.
Otherwise it is a variable node.
So we're going to see padding plus plus equals current node var dot padding and have we actually created
that and we haven't created the padding variable yet.
Go to the structure node.
And just by here under the type we're going to see in it, padding.
So essentially, we will compute padding and store it in the variables.
We're not doing that yet, but we will be okay.
So padding plus equals current.
No var dot padding.
So that will add the known padding of this variable node to the padding variable.
Obviously this assumes later on that we've already computed what the padding is.
We haven't yet done that.
So obviously this function will return zero if you used it now.
But later on in the course we will be making it.
So we set the padding variable of variable nodes, this one by here and then this compute some padding
will obviously give us the sum of all the padding of all the variables and then we're just going to
say last type equals current node, var type, dot type, and then we're going to say last node equals
current node, and then we're just going to say current node equals vector peg pointer vec.
Finally we're going to return the padding.
Okay.
So that should be it.
Let's put these in the header file and then we're going to test them.
So we're going to copy and paste.
And I've copied too much though on a copy from Padding.
Search for viable sites.
And here
and we're just going to paste it there and obviously get rid of all the bodies and the header file.
So just give us access to these functions outside of the source file, as you're aware.
And there we go.
We got those functions nice and made.
Let's head over to Main see and just by here we're going to return zero.
So it doesn't bother even compiling, we're just going to try some of these alignments out.
So what we're going to say is we're going to say print off operator.
A Well, you know, the percentile, I mean, and we're going to say a line value and we're going to
just write a random number in here, five, 8 to 7, and we're going to align it to four bytes.
Okay.
And now I want us to actually build that with make.
And we're going to try Maine.
And what do we see?
We see 5828.
So it is aligned it to four bites, 5828.
Let's try and run Python.
See if.
That's right, 5828.
And we're going to say.
Modulus for we can see that as a zero.
So 5828 is aligned correctly.
So obviously if you think of this in terms of offsets and sizes, let's say we have a size of, I don't
know, two bytes.
Obviously when you then do modulus four on there.
So let's try to make main.
We can see that gives us fall.
So then we know we need to align those two bytes to four bytes.
So whatever that variable size was being two bytes, which just add two bytes to it essentially.
So there's our four.
Again, if we call our padding and we passed in the same thing, then we're going to get a two because
that's telling us that if our variable size is two and we want to add the four bytes, we need to add
another two bytes to it to get that result.
So yeah, they are, they are our padding functions.
So I hope that makes sense.
So essentially it aligns and computes the new size or the new offset of a variable to ensure that it's
on those four byte boundaries that I discussed in the presentation.
So there we go.
I hope that makes sense.
Let's remove this code so we don't forget.
And that concludes this lecture.
Thanks for watching.