Hello. This is Dr Miller, and this is Episode 7.1 of Assembly.
Today we're gonna talk about local variables,
so local variables are variables that are used inside of a function, and then the value disappears. Or at least it's not accessible when the function is finished.
Actually, by definition, the value is still on the stack,
but it is not accessible, and we can't use it.
So here's an example with several different local variables. So, for example, inside of its function Fu, we have the local variable some and the local variable I
while in the main function we have a local variable called Why, in a local variable called X,
these are gonna you be used inside of the functions. And so programmers will often use them when they're programming programming in a high level language.
Now that's going to get lost. Those names will be lost, but we'll be able to see it when we reverse engineer or look at,
ah, high level language that gets compiled down, and that'll give us a good example of how we're going to be able to use them.
So local variables used the e BP register and then the first variable is that CBP minus four
and so e v p points to the old value of V B P. So if we go down, E v p minus four is gonna be our first local variable, and then our second role, be it eight and then 12 16 multiples of four.
So if you're going to allocate them, you can say sub BSP where e s P will point tvp
and then X. If you have x number of local variables, you take X times four. Now give you them out. So, for example, if you have 10 variables,
then you'd say sub e b E s p
four times 10. We should be 40
or if you just have one, you just subtract for
and so that's how you're going to do it inside of our program.
Here's another example. So if we have a local variable weaken increment that
now we can't increment it directly in memory and so we have to copy the value from the local variable CBP minus four into a register se X
and then increment that and then save that value back onto the stack. And so that's the equivalent of like a high level language
like C or C plus plus or java to the single command X plus. Plus so just to increment a variable that we have.
So here's a couple examples. If I have X equals zero y equals one and Z equals two in the high level language of C.
If I get that into assembly, I have three variables.
Times four gives me 12. So I need to subtract 12.
And then I could move zero into X one in the UAE and two and Dizzy.
And so we have the notion of thinking of them as just offsets. And so X is offset for minus for Why is minus eight and Z is minus 12?
And then, since we did a subtract, we have to do an ad at the end to correct our stack.
Now here we take the same thing, and we're gonna do X is equal to y plus Z.
And so we said our initial values here at the beginning we still subtract 12
but then we might want to move in to yea X
X and then into Pboc's why, and then add those to the two together and then store them a Z.
I may have done the little backwards, so them in excess, we load
y and Z and then store them back in X
so we can see how we can manipulate something that is in a local variable.
And we just have to be able to do our operations on that locally, inside of a register, and then we can save it back onto the stack.
And that's where our local variables will live.
So the example program that we gave earlier where we can compile that with GCC minus M 32 so that it has 32 bit
and the name of our file was test dot c. In my example, you can call it whatever you want.
Um and then if you don't give it out, profile it by default creates a doubt on Lennox.
And so then we can do a object dump, which will show us the assembly version of that, and we'll look at that here in a little bit.
for example, if we have our function called Fu, if we look at a disassembly of that,
we will see that we got zero going to the end of our
80 going into a bar. Four. It's hard to tell because both some starts at zero and I start to zero.
But if we look inside of here, we'll see that we're adding to bar for one each time,
whereas the some looks like it's going into Bar eight. So we're loading the value of
I and then adding it to what's in Hvar eight.
And then you can see at the bottom. Here we see movie a X e B P plus var eight. And so that's going to copy the value some into yea X, which is the return register. And then we're seeing our return at the end
and so we can see that there will be a clear,
definition of what gets produced in here based on the source code. And so there's a mapping that goes from source code to assembly. But as you can see, I gave that nice names in here like some and I and those all got lost. And so all we ended up with is a variable name.
So today we discussed local variables,
and in the future, we're going to give a local variable example. So we'll start off and will build on entire program.
And we'll basically implement from scratch using local variables.
And then, in a future lecture will talk about the intern. Leave instructions.
So the quiz. The question is, what is the offset of the third parameter
that we can use or local variable?
And that is zero XY,
and it's actually negatives. Iraq. See
right, because we're going below the stack.
So if you have questions, you can contact me at Miller MJ at you and Kate I e. To you and on Twitter at Milhouse 30.