Hello, this is Dr. Miller, and this
is Episode six point two of
Assembly.
Today, we're going to talk about the
stack.
So we have the stick, so
the stack is a region of Rampone and
by a register called S.B,
the extended stack pointer,
and then with the stack we can push
things onto and pop things off
of the stack.
So we'll have operations like push
and pop.
And then the stack is a last
in, first out
type of operation and type of usage.
And we also have the SS register, so
it is pointing to where stock memory
is, but you don't actually have to
even know that in general.
And so examples are pushier,
push, Ebix pop, Ebix,
add to E.S.P or subtract
from ESPE.
And then what do we use it for?
Mostly the stack is going to be used
for function calls, we can also
use it to store local variables,
parameters, and then
for a function that allows the
context to be saved.
And then we also will use it for a
temporary space.
So here are some example operations,
so we can have push X
where we take the ESP and we
subtract the way that memory
will be drawn as we draw
the low memory
addresses on up, and
we'll draw it like a a set
of plates where the top thing
goes on the top.
When we say push or when we say pop,
it'll come up from the top.
But technically it subtracts four
from the ESP register
and then it'll copy the data pointed
to by ESP.
So it'll copy that data onto the top
of the stack and then X
is either a register or an immediate
value or a hard coded value,
and then Pop will add four to Espe
and then say whatever is on top of
the stack into the register that's
given. So there'll be a register
that will be given here.
So here's an example.
So we have our original stack, so it
has some stuff on it.
We don't really know or care what
that is.
And then we move in, just register
some values. So 10 goes into X,
twenty goes into Ebix, thirty goes
in X, and then we're
going to execute this instruction
here. So we're going to push X
on top of the stack.
So X has a value of ten.
So that's now going to go on top
of the stack.
So we can see the top of the stack
points to the number 10 and we
again, we have all the original
stuff, so now we're going to push
X or X has a value
of thirty.
So we can see that that now goes on
top of the stack.
The next instruction, push SBX,
so now Ebix is going to go on top of
our stack.
So you can see a copy of the value
of 20 on top of the stack,
and so Bush added things onto the
stack and then papa is going to take
stuff off. So we say pop X
now, X should get whatever is
pointed to at the top of the stack.
So it had 10 and now it should have
20.
X is 20.
That thing is removed from the
stack. So now we still have 30
and 10 on top of our stack
and now we're going to say pop X,
so we'll take that off of the top of
the stack.
So now X is still 20, but X
gets the value that was on the
stack, which was 30, and now
X again is 30.
It had 30 before.
So that's an example.
Additionally, we have the operations
like call we can call a functioning,
so what call does is it pushes
the address of the next instruction
onto the stack and then
it copies IP to that new
function, location or address.
And then the reverse of that is the
return to return is going to
allow us to exit a function
so or return from a function.
So what that does is it pops off the
return address from the stack and
then moves IP to that new
address.
So we end up always having a call
to a function and then inside the
function when it's done, we'll have
a return.
And so that's how we can write a
program with functions.
Now, you've done this in class.
We have called a lot of different
functions.
So, for example, call, read in,
call, print and call, print and tell
all of those we are calling a
function and then we assume that
it's going to resume from the
location that we were at
before.
We also have the ability to allocate
space using add and subtract.
So, for example, we can say sub hesp
X and then I'll subtract X bytes
from ESP.
So typically when you enter a
function, you're going to do a
subtract on ESP to allocate
some temporary space that we can use
for local variables or buffer's.
And then at the end of the function
we can add back ESP, so
we'll typically add and subtract the
same number of bytes.
And so this will allocate that space
and we'll do that right before we
leave a function.
And then if you ever go through and
you are reverse engineering some
code, you might see that when a
function enters, it might do
something like and ESP and
then you've got a bunch of F's and a
zero.
And this is because it is trying to
make sure that E.S.P is on an even
boundary and this is a security
protection that some operating
systems and some compilers implement
to make it harder to attack
these type of systems.
And then we have a couple of special
operations like push aid and pop
aid, and so push aid is
going to push all of the registers
onto the stack, but it pushes
them in kind of a weird order,
right? So goes X, X,
SBX, the
value of Hesp before we did
the Push Aid SBP Essi
Eddi and then the pop
aid is going to do them completely
in reverse order and then the
original hesp is going to get
restored their hesp before
the the pop
operation occurred.
So today, we talked about the stack.
So here's our quiz, so
what is the following code do?
So go ahead and think about that,
because electricity, if you
understand what you think it does.
All right, so we had our original
stack, we pushed on
and then pushed on SBX,
so you had the number thirty three,
Ebix had the number 13.
So when we got it, 13 is on top of
the stack.
Here's another question.
So at the end, what is an X
and what is an X?
All right, so Yak's has 12 and
Ebix has 12, right?
They got both got pushed on their
right. So well, sorry, we pushed
Yak's on there and then we popped
SBX off.
Right. SBX got the value that was
copied on top of the stack.
So now Ebix has the same value as
X.
So looking forward, we are going to
talk about using the stack,
and if you have questions, you can
contact me at Miller, MJ, at UNCHR,
Edu, and you can find me on Twitter
at know how sturdy.