Hello, this is Dr. Miller, and this
is Episode seven point two of
Assembly
today, we're going to get to get an
example of using local variables.
So for our example, we are going
to write a loop that's going to some
a bunch of numbers and
then inside of our main, we're going
to use local variables also.
So the example is here in C,
and so we have, again, this function
called FUX that has
a sum. It adds up all the numbers
from zero to ND,
adds them to the sum and then
returns it. And then inside of our
main we're going to have some local
variables. So we'll go ahead and
start a project here.
All right, so we're going to need a
couple of different variables, so in
the previous example, we saw that we
had ganef and printf.
So we're going to define some
variables here at the top.
This is the SCANA format
with a bunch of bytes and it is
a precent D
terminate with a zero.
Here's our print format.
I'll go ahead and put a new line and
terminate with zero,
and then we're going to be using the
functions, print F and scan
F.
So we'll just declare those as
external
and then instead of our main
program, we're going to use a local
variable for the value of the scarf.
So to allocate space,
we need to subtract from Hesp
in order to give room.
And then we're going to want to
load the address of that variable,
so we'll go ahead and load that
anti-tax.
And Leah is going to grab the
address of the value at E.S.P,
and so it'll just copy that into
their.
And then we need to push that onto
the stack before we call our
function,
and then we also
have to put on the format so
we go ahead and push that.
Now we can go ahead and call our
scanner function.
So it'll use the format that we've
defined here with a percent D
and then save into the pointer
that is being saved onto the stack.
And then we have to correct the
stack for what we've been
the arguments that we pushed on.
So to add E.S.P
eight and at the end of our
function we allocated space for our
variables. We going to go ahead and
I'll put in the correction for that.
So add E.S.P
for so allocated a local
variable up here and then
we have to allocate it down here.
All right. So now we've read the
value from the user and then we're
going to want to read
that in. So we'll copy that from the
stack.
So we'll get the value from E.S.P,
which is where our local variables
at.
And then for our function, we're
going to push it on the stack.
We'll go ahead and call our function
and then correct this attack ad
E.S.P for
each variable took up one.
All right, so now we're ready to
write our function.
So that's our function
is going to try and some all of our
numbers.
We're going to call it FUX.
So the first thing that we need is
we need to have inside of our
function. We have to have our
Prolog, Push SBP,
Move SBP, E.S.P,
and then I'm going to allocate space
for two local variables
to sub E.S.P
eight four for each one of them.
And then inside my function, I'm
going to use X and IDEX.
I'll go ahead and do a push
X, push
X at the end of my function.
I'll go ahead and pop IDEX.
And Pop dcx,
and then I'll put my
correct my stack and then put my
epilog so and E.S.P
eight
move E.S.P.
SBP
are
AVP and
return.
I've some comments in here,
say,
just hours.
OK, so now
we're ready to get into the
meat of it, and so we've got our
local variables.
So, for example, some I'm going to
use that EBP minus for.
So I'm going to save into the
location at EBP minus for
the value of zero.
This is for some.
For I am going to use minus eight
and I'm also going to zero that out
and here I'm putting some comments
in so that we can see
where all this works up.
So now this is where I'm going to
check my loop condition,
I'll call it check.
So what here, I wanted to check that
out. I forgot a semicolon
check
that I
is less and and
and so and is going to be our
parameter and I is going
to be our local variable.
So go ahead and copy rne into X.
Remember, our parameter
one is going to start at
eight.
Plus eight and then move
into MDX.
The value of I.
I'll let you try and figure out
which ones I.
EVP minus
eight.
So here we're checking to see if
that is the case and then we're
going to jump down to label down
here called Done, so Dunn is going
to be what we're going to do at the
very end.
OK,
so if we're not done, then we need
to basically add I
to some.
So here we go.
Go ahead and load some.
Into Yak's
on the ad.
Now, from above, we can see that we
loaded I
into IDEX, and
as we follow through, then we can
just add I to
some and now we've got to save it
back to some.
Now move EBP
minus four.
So now we've added that and then
each time through our loop, we
actually increment I so we need
to load I again,
although the hard way.
So I is minus eight.
I can just increment
the X and then move into
EBP minus eight,
the new value add
X.
All right, and then when we're done
incrementing, then we need to go
back up to the check, so we'll just
to jump check.
And so then when we're all done, the
value of some should be the
addition of all of the numbers
together.
So down here, we need to go move
X.
GBP minus for.
And it looks like I'm the type of
EVP there we go.
All right, so that should
be all we need, right, so we'll jump
back up to the top.
I has been incremented, we reload it
and then compare it against GNR,
which is our number,
and then we should be all right.
So back in our main, we
need to make sure
that we
print off our value that we were
caring about,
so the result of food is going to
have value in X,
so I can just push
the value of X,
I'm going to do printf
push.
Our print format called
print F, and then I have
to adjust the stack.
Now, I could go ahead and correct
this.
I could make it just 12.
But I'm just showing you how we
would break this down at
each step.
All right. So Compiler is going to
be smarter and more efficient than
me. Instead of adding and
subtracting all of these different
times, it'll subtract once
and then use that.
And so here we can see that
it should print off our value
using print.
Now, if I don't have any mistakes.
It looks like line 50.
See what I typed there on?
Mel
Watt, help.
To his label, let's look at this
line. Thirty eight
cents a warning.
Oh, I forgot to put my colon
after my function name.
There we go.
And you can buy a hand.
OK, add those together,
and if you add up all the numbers
from one to 10, you should get fifty
five one.
The three one plus two
is three plus three is six.
They were able to get it.
So a couple of things that usually
catch people are forgetting to
correct the stack or push
things on in the appropriate order
and then referencing our
variables, which have a negative
sign here and our parameters,
which have a plus sign down here.
And so you've got to just make sure
that you you get all those
you save and restore registers,
you allocate space and allocate
space for the variables.
So you got to make sure that you do
all of that if you're going to do a
properly.
So today, we gave an example using
local variables,
and next time we're going to talk
about the enter and leave
instructions.
Do you have any questions you can
contact me at Miller, MJ,
at you and Kada Edu and on Twitter
Milans 30.