Hello. This is Dr Miller, and this is Episode 7.2 of Assembly.
Today, we're going to get him, get an example of using local variables.
So for our example, we're 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 and see. And so we have again this function called Fu
that has a summit, adds up all the numbers from zero to n
um, adds them to the some and then returns it and then inside of our main, we're gonna have some
local variables. So we'll go ahead in
start a project here.
All right, so we're gonna need a couple different variables. So in the previous example, we saw that we had scan f and print F, so we're gonna define some variables here at the top.
This is this can f format
a bunch of bites, and it is a percent d
terminate with a zero.
Here's our printed 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 effin, scan off,
so we'll just declare those was external,
And then inside of our main program, we're gonna use a local variable for the value of the scan F.
So to allocate space, we need to subtract from E S P in order to
And then we're going to want to
load the address of that variable so we'll go ahead and load that in E X
and l E A. Is going to grab the address of the value at GSP. And so it'll just copy that into there.
And then we need to push that onto the stack before we call our function.
we also have to put on the format so
we'll go ahead and push that
now we can go ahead and call our scanner functions so it will use the format that we've defined here with a percent d and then save into the pointer that is being saved onto this tack.
And then we have to correct the stack For what? We've been. The arguments we pushed on Soto add yes, p
And at the end of our function, we allocated space for our variables. We're going to go ahead and I'll put in the correction for that. So add
yes, P four, right. So allocated a local variable up here and then we have two D allocated 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 B, which is where our local variable is that.
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 a stack. Add yes, p four, because each variable took up one.
All right, so now we're ready to ride our function. So let's our function is going to try and some all of our numbers, we're gonna call it Fu.
So the first thing that we need is we need to have inside of our function. We have to have a prologue Bush CBP
And then I'm gonna allocate space for two local variables.
The sub Yes, p 84 for each one of them.
And then inside my function, I'm gonna use e c x and Edie exiled. Go ahead and do a push
At the end of my function, I'll go ahead and pop e d x
and then I'll put my,
ah, correct for my stack and then put my epilogue So add yes, p eight
Wasn't comments in here say
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 gonna use that e v p minus four.
So I'm gonna save into the location at E V P minus for the value of zero. This is for some.
for I I'm gonna use minus eight,
and I'm also gonna zero that out. And here I'm putting some comments and so that we can see
where all this works out.
So now this is where I'm going to check my loop condition.
So what? Here. I wanted to check that.
Oh, I forgot a semicolon
that I is less than and s o n is going to be a parameter and I is going to be our local variable.
So go ahead and copy End into E C X.
one is going to start it
bbp plus eight and then move into E t x
I'll let you try and figure out which ones I
So here we're checking to see if that is the case, and then we're gonna jump down to label down here called Done So Done is going to be what we're gonna do at the very end.
So if we're not done, then we need to basically add I to some.
Go ahead and load some
from above. We can see that we loaded I
into E d X and was we follow through. Then we can just add i
to some and now we've got to save it back to some
and I'll move e v p minus for
So now we've added that, and then each time through our loop, we actually increment I so we need to Lodi again.
I'll do it the hard way.
I could just increment DX and then move into E V P minus eight.
The new values E g X.
And then when we're done in commenting, then we need to go back up to the check. So just do jump
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 do a movie. A X
It looks like I have a typo E bp. 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 incremental. We reload it
and then compare it against n which is our number.
And then we should be all right.
So back in our main, we need to make sure
print off our value that we were carrying about.
So the result of Fu is gonna have a value in the X
the value of e X. I'm gonna do print off
are printed format, call, print F and then I have to adjust the stack
Now, I could go ahead and correct this, right? 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 gonna be smarter and more efficient than me. Instead of adding and subtracting all of these different times, it'll subtract once
And so here we can see that,
um, it should print off our value. Um,
Now, if I don't have any mistakes
that looks like line 50.
See what I typed? Drawn?
It's his label. Must look at this line. 38.
Oh, never had to put my colon after my function name. There we go.
And you can, by hand
add those together. And if you add up all the numbers from 1 to 10 you should get 55 when the three
one plus two is three plus three is six.
They were able to get it.
So a couple things that usually catch people are forgetting to correct the stack or push things on in the appropriate order.
Um, and then referencing our variables which have a negative sign here and our parameters, which have a plus sign down here.
So you got to just make sure that you get all those you save and restore registers. You allocate space and de allocates space for the variables.
So you got to make sure that you do all of that if you're gonna do it properly.
So today we gave an example using local variables.
And next time, we're going to talk about the enter and leave instructions.
If you have any questions, you can talk. Contact me at Miller MJ at you and Kate, I e D U and on Twitter at Mail House 30.