# Local Variables Example

Video Activity
Join over 3 million cybersecurity professionals advancing their career
or

Course
Time
13 hours 15 minutes
Difficulty
Beginner
CEU/CPE
14
Video Transcription
00:01
Hello. This is Dr Miller, and this is Episode 7.2 of Assembly.
00:06
Today, we're going to get him, get an example of using local variables.
00:12
So for our example, we're going to write a loop that's going to some a bunch of numbers
00:18
and then inside of our main, we're going to use local variables also.
00:23
So the example is here and see. And so we have again this function called Fu
00:28
that has a summit, adds up all the numbers from zero to n
00:33
um, adds them to the some and then returns it and then inside of our main, we're gonna have some
00:38
local variables. So we'll go ahead in
00:41
start a project here.
00:52
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.
01:02
This is this can f format
01:04
a bunch of bites, and it is a percent d
01:08
terminate with a zero.
01:11
Here's our printed format.
01:21
I'll go ahead and put a new line and terminate with zero,
01:25
and then we're going to be using the functions
01:26
print effin, scan off,
01:33
so we'll just declare those was external,
01:36
okay?
01:38
And then inside of our main program, we're gonna use a local variable for the value of the scan F.
01:44
So to allocate space, we need to subtract from E S P in order to
01:49
give room.
01:56
And then we're going to want to
01:57
02:10
and l E A. Is going to grab the address of the value at GSP. And so it'll just copy that into there.
02:19
And then we need to push that onto the stack before we call our function.
02:23
And then
02:25
we also have to put on the format so
02:32
we'll go ahead and push that
02:35
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.
02:50
And then we have to correct the stack For what? We've been. The arguments we pushed on Soto add yes, p
02:57
eight.
02:58
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
03:05
yes, P four, right. So allocated a local variable up here and then we have two D allocated down here.
03:14
All right, so now we've read the value from the user, and then we're going to want to
03:19
read that in, so we'll copy that from the stack,
03:23
so we'll get the value from E S B, which is where our local variable is that.
03:29
And then for our function, we're going to push it on the stack.
03:35
We'll go ahead and call our function and then correct a stack. Add yes, p four, because each variable took up one.
03:43
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.
03:51
So the first thing that we need is we need to have inside of our function. We have to have a prologue Bush CBP
03:58
movie B, p E S p.
04:01
And then I'm gonna allocate space for two local variables.
04:05
The sub Yes, p 84 for each one of them.
04:11
And then inside my function, I'm gonna use e c x and Edie exiled. Go ahead and do a push
04:15
e c x push dx
04:19
At the end of my function, I'll go ahead and pop e d x
04:24
and pop ccx
04:28
and then I'll put my,
04:30
ah, correct for my stack and then put my epilogue So add yes, p eight
04:39
move. Yes, p e v p
04:43
ah
04:45
PVP on recheck.
04:49
04:54
just hers.
05:13
Okay,
05:14
so now we're ready to get into the meat of it. And so we've got our local variables.
05:18
So, for example, some I'm gonna use that e v p minus four.
05:27
So I'm gonna save into the location at E V P minus for the value of zero. This is for some.
05:32
Okay,
05:40
for I I'm gonna use minus eight,
05:43
and I'm also gonna zero that out. And here I'm putting some comments and so that we can see
05:48
where all this works out.
05:51
So now this is where I'm going to check my loop condition.
05:55
I'll call it check.
05:58
So what? Here. I wanted to check that.
06:00
Oh, I forgot a semicolon
06:06
that I is less than and s o n is going to be a parameter and I is going to be our local variable.
06:15
So go ahead and copy End into E C X.
06:21
Remember, our
06:23
parameter
06:24
one is going to start it
06:26
eight
06:30
bbp plus eight and then move into E t x
06:36
the value of I
06:39
I'll let you try and figure out which ones I
06:43
E v p minus
06:46
Kate.
06:54
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.
07:02
Okay,
07:04
So if we're not done, then we need to basically add I to some.
07:10
So here we go.
07:15
07:19
into yea X.
07:21
07:28
from above. We can see that we loaded I
07:33
into E d X and was we follow through. Then we can just add i
07:39
to some and now we've got to save it back to some
07:49
and I'll move e v p minus for
07:57
E X.
07:58
So now we've added that, and then each time through our loop, we actually increment I so we need to Lodi again.
08:05
I'll do it the hard way.
08:11
Eyes minus eight.
08:16
I could just increment DX and then move into E V P minus eight.
08:24
The new values E g X.
08:30
All right.
08:31
And then when we're done in commenting, then we need to go back up to the check. So just do jump
08:35
check.
08:37
And so then, when we're all done, the value of some should be the addition of all of the numbers together.
08:43
So down here, we need to do a movie. A X
08:50
e B p minus four.
08:58
It looks like I have a typo E bp. There we go.
09:03
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
09:11
and then compare it against n which is our number.
09:13
And then we should be all right.
09:18
So back in our main, we need to make sure
09:22
that we
09:24
print off our value that we were carrying about.
09:30
So the result of Fu is gonna have a value in the X
09:33
so I can just push
09:35
the value of e X. I'm gonna do print off
09:39
push,
09:43
are printed format, call, print F and then I have to adjust the stack
09:50
Now, I could go ahead and correct this, right? I could make it just 12
09:56
but I'm just showing you how we would break this down at each step.
10:00
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
10:07
and then use that.
10:09
And so here we can see that,
10:11
um, it should print off our value. Um,
10:16
using print off.
10:18
Now, if I don't have any mistakes
10:24
that looks like line 50.
10:30
See what I typed? Drawn?
10:33
No,
10:37
that would help.
10:48
It's his label. Must look at this line. 38.
10:52
It's a warning.
10:54
Oh, never had to put my colon after my function name. There we go.
11:07
And you can, by hand
11:09
hope
11:11
add those together. And if you add up all the numbers from 1 to 10 you should get 55 when the three
11:18
one plus two is three plus three is six.
11:22
They were able to get it.
11:26
So a couple things that usually catch people are forgetting to correct the stack or push things on in the appropriate order.
11:33
Um, and then referencing our variables which have a negative sign here and our parameters, which have a plus sign down here.
11:41
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.
11:50
So you got to make sure that you do all of that if you're gonna do it properly.
12:01
So today we gave an example using local variables.
12:07
And next time, we're going to talk about the enter and leave instructions.
12:11
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.
Up Next