Hello. This is Dr Miller, and this is Episode 6.3 of Assembly.
Today we're gonna start using the stack, and then we're gonna have an example of calling the function print f
So when we have a stack, one of the things that we're gonna want to do is we're gonna want to either push things on or pop things off.
And so every time that you push something on right, it modifies E S p.
And when you're done, you need to pop it back off. So an example would be pushy X push ebx, and then we can pop them in the reverse order.
So that's one way to restore the stack back to its original state, which is what we need to do all the time.
The other thing that we could do is we could push on to registers or a registered twice. And then we can just add TSB to correct the amount that we put on there.
If we don't do this, that our program is gonna crash and we'll show an example of that in a little while.
So, for example, you might want to save some data when you enter a function and we'll talk about how to create functions and Lex lecture.
So when you enter your function, you might want to push registers. So, for example, yea, x e c x e d x Yes, I
And then when you're done, you need to pop them off in the reverse order. It's a little last one that you pushed is the 1st 1 that you pop
You can see we did e d I E d x e c x and then e x.
And so when we create functions we're gonna want to enter, we're gonna save our our registers. And when we exit, we're going to restore them back in the reverse order that we had
so print f is a C function that has a variable amount of arguments, which means that you can have 01234 10 20 whatever the number of arguments that you want is
and we have this thing called a format string. So format string has thes percent characters, and then that gets replaced with the actual variables X y and see.
So what we're gonna do is we're gonna call a print f function.
And we gotta push our arguments in the reverse order because the first thing on is the last thing that we have in there.
So we'll go ahead and start our project here.
So I've already built the project.
So the first thing that we're gonna do is we're going to declare a couple variables and then we're going to define our format string.
So I'm gonna try and do the same format string that we had before.
I'm just typing that in
the percent de means that it's going to be a integer.
The percent C is a character,
and then the 10 is our new line in the zero terminates a string and then we'll declare a couple variable so x
Then I'll show some examples here,
so we have to push on our arguments in the reverse order. So I'm gonna push my character first
so I can define a character. So that is the character a right, so that would put it onto the stack. Now I could
use a register to put that in, but we'll just show pushing an immediate or just pushing ah, hard coded value onto the stack
here. We want to get the value of what? Why?
And I'm just using X because it's convenient.
Alright, so I've put them on in the reverse order. So I got my character is the first thing that I put on because it's the last thing in here.
Then I got why, then I got X. And now I need to put on my format string
We want the address of it. We don't want the actual data at it, so we're getting the address and they will push that on
Now we're ready to call our function.
So the function is called print half, but it doesn't exist in our normal spot. It exists in the sea library. So we have to tell it that
we have an external function called print F.
And then we can see here that we pushed on 1234 items onto the stack
and some when we're done, we have to correct the stacks. The easiest way to do that is to add
Or you could do 10 in Hex. Either one is fine. So 16 based 10 or we can do this to 10
So we built our project.
So now we can go ahead and run it
so printed off the data that we thought it would print off. So let's go back in.
Let's look at if we forgot to correct the stacks, I'll just comment on this line.
So now when I build my project,
it causes it to crash
pointers that air saved on the stack that if we don't restore the stack back to its original than it's gonna have problems.
And even if I if I did this and I let's say only corrected it by
cause I forgot about one of my arguments that I put on there,
clean my project and run it,
it also cause it to crash.
So one of the keys to understand here is that you want to make sure that you put the correct amount back on, so I'll do 16 and decimal
now. When we were on our function,
it works fine and it doesn't crash
and a segmentation fault. This means that the program tried to access a region of memory that it wasn't allowed to access.
All right, so today we looked at using the stack by calling our function that was called print. If
so, here's a couple quick questions when exiting a function. What operation? To perform on E. S. P.
So generally we add E S P and then some amount. And that's the amount that we allocated when we were entering the function or before we were calling a function.
And then how do we save registers?
So we pushed the register. So we looked at earlier. You push them and then you would pop them. So
push yea x or push ebx. Whatever the register that you want to save this
looking forward, we're going to look at calling functions and then even creating what we call recursive functions.
If you have questions, you can contact me at Miller MJ at you and Kate I e to you. And you can find me on Twitter at Milhouse 30