Hello, this is Dr. Miller and this
is Episode fourteen point one of
Assembly.
Today, we're going to talk about the
contracts constructs and we'll give
some examples, including unions and
structures.
Constructs,
so a construct is a feature that
is provided to the programmer and
allows them to write programs
more efficiently with
their time instead of doing things
manually by hand.
And we have some constructs
that are visible to the user and
then some that are hidden by the
language.
So things that might be visible
would include structures and unions
and a little bit on switch
statements. But then jump tables
might be hidden from the user.
So they may not see those in their
data, their lives or understand
how they're being implemented
by the compiler.
So we'll give you an example, unions
say a union is where we have a
value and we might have several
different representation of that
using a variety of different formats
for the same thing.
And this allows a program to
use a programmer to use the same
memory, but they can change how
it's interpreted by the computer.
An example might be an IP address.
You might want to represent it using
bytes or characters,
and then you might want to load the
entire thing using an integer.
And so that's one example of where
we might use a union.
So here we have an example of a
union. We can see it's defined at
the top. It's called union
and our union is called my union.
And then it consists of a float and
an unsigned character.
And the way that the compiler
determines what the size of this
union is, is it looks at the largest
of the different types.
So offloads is going to be four
bytes.
An unsigned character is going to be
six bytes, the array size six.
And so it'll make this union be six.
So whichever one is the biggest
write, it wants to be able to hold
that. And so that's what it uses
when it determines the size.
And then we can see here we have a
loop that goes through all six
characters and just assigns them to
the value of F F.
And then we can see down here, we go
ahead and change that later and say,
oh, I want to save into that
the number three point one four.
And then we go ahead and we actually
print off all those values
again, using the character
method of accessing that.
So in this example, we
can see accessing this
field using
X equals three point one four.
And we don't have an offset.
So we can see here that we're
loading a floating point number and
it's stored in the data section and
it has a particular offset that's
loaded on to our floating point
stack. And then we're going to go
ahead and do a floating point store
and it's using
EBP minus 14.
And so that's the location that it
is saving it to.
And then this is an example where we
have an additional offset that
stored in there, so we can't just
directly access it, so
we have to add in or offset.
And so we can see here again,
the place that we're going to end up
saving is SBP minus 14.
So we load that address into IDEX,
we go ahead and load our offset or
I into IEX.
We add the two together, store that
an X.
And so now we're going to save the
value FNF into the location
pointed to by X, which again is
the offset plus the address.
Structures,
so a structure is a composite data
type, so it's going to have similar
syntax, but we have a bunch of
variables that are all grouped
together and we can use one pointer
in order to discuss
those. And so we can use this in
order to create composite data
structures that have many different
pieces of information inside of
them.
So here's an example again, similar
syntax. We define a list of things,
but here each one of them occupies
its own memory
location. So we have a flow to
character, an integer and unsigned
character array, all stored
in the same thing. And I can assign
values to each one of those
and they are independent of each
other.
Now, here is a debugging view of
that. So we can see in here that we
have our registers
and our stack.
And then inside of here, each one of
these statements is again going to
generate a variety of assembly
statements down here.
But when we look when we print off
what our structure is, we can see
that it holds all of the data
that we had saved in it.
Right. So it's not overwriting
something. It's each one has
its own reserve location.
And so we can see also it sort of
prints our character as zero
six one, which is the character
A, we can also see B
in there.
So today, we talked about different
constructs and then we gave examples
showing both unions and structures.
Looking forward, we're actually
going to look at the structure
layout and see how the access
works for that layout
for the different members inside of
there. So we'll go a little bit more
in depth with that.
And then also in the future, we'll
try and understand the structure and
we're going to or union
using a debugger so we can sort of
view the code and see how it
manipulates it within the assembly
room.
So here's our quiz,
so what data type uses different
memory locations for each variable?
That would be a structure.
What determines the size of a union?
The size of the largest member.
If you have questions, you can email
me millirems at you in Canada, Edu,
and you can find me on Twitter at
Milhouse 30.