Hello. This is Dr Miller in this episode 14.1 of assembly.
Today we're gonna talk about C contracts, constructs and will give some examples, including unions and structures.
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.
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 uses. They may not see those in their data they lives or understand how they're being implemented by the compiler.
So we're given example unions.
So union is where we have a value and who 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 I P address, so you might want to represent it using bites or characters,
Um, and then you might want to load the entire thing using an imager. So that's one example of where we might use a union.
So here we have an example of the 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 afloat and an unsigned character.
And the way that the compiler determines what the size of this union is is it looks at this largest of the different types, so afloat is going to be four bites. An unsigned character is going to be six bites the rays side six, and so it'll make this union be six. So whichever one is the biggest right, 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 3.14 and then we go ahead and we actually print off all those values,
um, again using the character method of accessing that.
So in this example, we can see accessing this field using x dot f equals 2.14 and we don't have an offset. So we can see here that we're loading a floating point number and stored in the data section and has a particular offset
that's loaded onto our floating point stack. And then we're going to go ahead and do a floating point store,
and it's using ah e v P minus 14. And so that's the location that it is saving into.
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 gonna end up saving as e v P minus 14. So we load that address into et X.
We go ahead and load are offset or I into E X. We add the two together store that an E X,
and so now we're going to save the value F f into the location pointed to by E X, which again, is the offset plus the address
So a structure is a composite data type,
so it's gonna have similar syntax. But we have a bunch of variables that are all grouped together, and we can use one pointer in order to
I'm discussed 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 and imager and unsigned character array all sort 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,
um, are 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 overriding something. It's Each one has its own reserve location,
and so we can see also it sort of prince our character as zero x 61 which is the character? A. We can also see B
So today we talked about different see constructs and then we gave examples showing both unions and structures
Looking forward, we're actually gonna 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 will try and understand the structure and we're going to or union using it to bugger so weaken sort of view the code and see how it manipulates it within the assembly room.
So what Data type uses different memory locations for each variable?
That would be a structure
what determines the size of a union,
the sides of the largest member.
If you have questions, you can email me Miller MJ at you and Kate I e to you, and you can find me on Twitter at Milhouse 30