Hello, this is Dr. Miller and this
is Episode fourteen point two of
Assembly.
Today, we're going to talk about
structures, we'll talk about their
layout and offsets,
structure and layout.
So here we're given a structure.
So we have several different members
of Float F a character, see
an integer D and
unsigned character array called T
of size six.
And so we can see inside the code
that we are setting different values
of each member of the structure.
And so with the structure, the
layout starts at the first
declaration of the first variable,
and then it rounds to the nearest
even byte boundary for the next
size.
So four bytes it'll just put one
after the other starting at the
beginning and going through
for shorts.
If it's an address one, it'll
round up to two, four floats at
around up to four integers, four
and then longus on a 32 bit
architecture around to four and
on a sixty four will be down to
eight and then doubles around
to eight.
So those are just some examples.
Different ones will,
depending on the architecture, will
be down to different sizes.
But then if we're on a 32 bit
architecture, we'll run to
the nearest four by boundary.
So if we have a double, it'll
round to the nearest four by
boundary because it's just thirty
two bits.
And if we're on a 64 bit, then it
would go all the way up to possibly
an eight by boundary for that
structure.
So here we're going to have an
example and we're going to use the
original code that we saw.
So when we
have a float that's going to take up
four bytes and we can see that
zero starts at the beginning.
We can see that we have increasing
memory addresses.
So zero, one, two, three.
And then here we have increasing
memory addresses.
So they go up in size.
So our float takes four bytes.
And so we here we fill in the first
four.
Now we have a character, so that's
going to go at the next bite
within our next object is
going to be an integer.
And so instead of starting
it here, it rounds up to the nearest
four byte boundary.
So Energia takes up four bytes.
So it starts here and goes up.
And the reason why they round up to
this is because it's faster to
access things at even byte
boundaries as opposed to odd byte
boundaries. So the processor is
much, much faster if it can
do that, rounding automatically.
And then the last we have a
character array of size six.
So we start here at zero one, two,
three, four and five.
That gives us our size six.
So the offsets.
So here I took the C-code
and then I did a disassembly on it,
and we can see that
we have the different offsets.
So I've written them here and
we can see that our offsets are
starting SBP minus and then
we have a value.
So our base down here is Ebbw minus
20.
And then as we can go, we can see
our different offsets.
So our character is that minus one.
See, we have our
integer at minus 18
in Hex which is twenty four
decimals. I wrote the decimal over
here and then we can see
that our characters, we had a bite
minus 14 and then we can see
this is minus 13, minus 12,
minus 11, minus 10.
So we can see that it picked the
appropriate bite boundaries.
And the reason why it doesn't just
compress them willy nilly
is because if you write a program
using different compilers, it has
to be able to understand, OK, where
would the byte boundary be for this
type of structure?
And so it always follows these
different rules.
But again, if you want to understand
where it's going to go, you can go
ahead and disassemble it in order to
understand exactly how that
structure is working.
If you have if you know what it
looks like in C
and so then we can understand.
We can see that we're moving a bit
here or we're moving a D word here
for each one of those.
And we can see that it automatically
does the D word, even though we just
have faith.
And that is automatically set
by the compiler.
So today we talked about the
structure, layout and then offsets.
Looking forward, we'll take some
example structures and look how to
reverse engineer them and
then learn how to program them if we
wanted to manipulate them directly.
So here's a quiz, so given the
following structure, what would the
offsets be for each variable?
So here, if we have 30 to bit
the character, she is going to start
at zero, l will
start at four and then D
will start at eight.
And again, we're going negative.
So it's going to go the opposite
direction. And then let's look at 30
at 64 bit.
So for 64 bit, we got zero,
eight and 16,
so we will round this up to the
nearest eight by boundary and then
this one up to the nearest 16,
because this one will take actually
eight bytes.
So we can see the offsets here,
minus 20, minus 18 and minus
10.
If you have questions, you can
e-mail me millirems at you, Ankita
Edu, and you can find me on Twitter
at Malchow Sturdy.