Hello, this is Dr. Miller, and this
is Episode seven point one of
assembly
today we're going to 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 cannot
use it.
So here's an example with several
different local variables, so, for
example, inside of the function FUX,
we have the local variable sum
and the local variable.
I while
in the main function we have a local
variable called Y and
a local variable called X,
and these are going to be used
inside of the functions.
And so programmers will often use
them when they are programing
programing 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 look at
a 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 EBP
register and then the first variable
is that EBP minus four,
and so SBP points
to the old value of SBP.
So if we go down EBP minus
four is going to be our first local
variable and then our second will
be at eight and then 12,
16 multiples of four.
So if you're going to allocate them,
you can say sub hesp,
where Hesp will point to SBP
and then if you have X number
of local variables, you take X
times. For now, I'll give you the
amount. So, for example, if
you have 10 variables,
then you'd say sub VSP
four times 10, which should be 40,
or if you just have one, you just
subtract four.
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, we can
increment that.
Now, we can't increment it directly
in memory and so we have to copy the
value from the
local variable.
So EBP minus four into a register,
say 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++ or Java
to the single command X plus
plus. So just to increment a
variable that we have.
So here's a couple of 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 can move zero into X,
one into Y and two into Z.
And so we have the notion of
thinking of them as just offsets.
And so X is offset for minus
four, Y is minus eight
and Z is minus twelve.
And then since we did a subtract, we
have to do an add at the end to
correct our stack.
Now, here we take the same thing and
we're going to do X is equal to Y
plus Z.
And so we set our initial values
here at the beginning, we still
subtract 12, but then
we might want to move in to
X, X
and then into X Y
and then add those two two together
and then store them at Z.
I may have done that a little
backwards.
Saw them in X, so 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
live.
So the example program that we gave
earlier where we can compile that
with GCC minus M thirty two
so that it is thirty two bit
and the name of our file is test
C in my example, you can call it
whatever you want and
then if you don't give it an output
file it by default creates Aidar
out on Linux.
And so then we can do a
object down, put to show us the
assembly version of that, and we'll
look at that hearing a little bit.
So, for example,
if we have our function called thoo,
if we look at a disassembly of
that, we will see that
we got zero going to the end of
our eight zero
going into VARE 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 vare
for one each time,
whereas the sum looks like it's
going into bar eight.
So we're loading the value
of AI
and then adding it to what's in
bar eight.
And then you can see at the bottom
here we see Move X,
SBP plus VARE eight.
And so that's going to copy the
value some into X, which is the
return register.
And then we were 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 we'll build an
entire program and
we'll basically implement from
scratch using local variables.
And then in a future lecture, we'll
talk about the enter and leave
instructions.
So the quiz the question is, what is
the offset of the third parameter?
That we can use our local variable
and that is zero XY
and it's actually negative zero
XY, right, because
we're going below the stack.
So if you have questions, you can
contact me at Miller, MJ, at
you and Keda, Edu and on Twitter
at has 30.