13 hours 15 minutes
Hello. This is Dr Miller, and this is Episode 6.2 of Assembly.
Today we're going to talk about the stack,
so we have the stack.
So the stack is a region of ram pointed by a register called E S B. The extended stack pointer.
And then with the stack, we can push things onto in 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 stack memory is, but you don't actually have to even know that in general.
And so examples are pushy a X push ebx pop ebx add TSP or subtract from E S p,
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 allows the context to be saved.
And then we also will use it for a temporary space.
So here's some example. Operations so we can have push X.
Where will we take the S p and we subtract. The way that memory will be drawn is we draw the low memory addresses on up
and will 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 out from the top.
But technically it subtracts four from the E S P register,
and then it will copy the data pointed to by E S P. So it'll copy that data onto the top of the stack,
and then X is either a register or an immediate value or, ah, hard coded value.
And then pop will add for TSP and then save whatever is on top of the stack into the register that's given. So they'll be a registered there we 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 registers some values. So 10 goes into yea X 20 goes into BB X 30 goes into ccx
and then we're gonna execute this instruction here. So we're going to push Yea x on top of the stack so e X has a value of 10. 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 can we have all the original stuff.
So now we're gonna push e c x e c x has a value of 30
so we can see that that now goes on top of the stack,
the next instruction push ebx. So now EBX is gonna go on top of our stack
so you can see it copied the value of 20 on top of the stack.
And so push added things onto the stack and then pop is gonna take stuff off. So we say Poppy, a X
now e X should get whatever is pointed to at the top of the stack. So it had 10 and now it should have
20 minutes. Now Ta 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 E c X so we'll take that off of the top of the stack.
So now you exit still 20 but CCX gets the value that was on the stack, which was 30. And now Easy X again is 30. It had 30 before,
so that's an example.
Additionally, we have the operations like call we can call a function name. So what call does is it pushes the address of the next instruction onto the stack
and then a cop. Easy, I p, to that new function, location or address.
And then the reverse of that is the return. The return is going to allow us to exit a function
so your return from a function.
So what that does is it pops off the return address from the stack and then moves I p e i p to that new address.
So we end up always having a call to a function, and then inside the function when it's done, will 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 into call print end culprit, NL.
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 E S p x, and that will subtract X bytes from E S. P. So, typically, when you enter a function, you're going to do a subtract on E S p to allocate some temporary space that we can use for local variables or buffers.
And then at the end of the function, we can add back E S P so well typically add and subtract the same number of bytes.
And so this will analogue eight 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 A and E S p, and then you got a bunch of efs and a zero,
and this is because it is trying to make sure that yes, 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 80 and pop 80.
And so push i d is going to push all of the registers onto the stack, but it pushes them in kind of a weird order. Right, so cozy A x e c x e x e b x
The value of vsp Before we did the push a d e v p e s i e d i
and then the pop a d is going to do them completely in reverse order And then the original E s P is going to get restored other ESPN 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
go ahead and think about that. Pause a lecture, See if you understand what you think it does.
All right. So we had our original stack. We pushed on E X and then pushed on ebx.
So yea X had the number 33. Ebx had the number 13 so when we got at 13 is on top of the stack.
Here's another question.
So at the end, what is any X and what is an EBX?
All right, So yea X has 12 and EBI excess toe right. They got both got pushed on there,
right? So Well, sorry. We push TX on there and then we popped ebx off writes ebx got the value that was copied on top of the stack.
So now EBX has the same value A ZX.
So looking forward, we're going to talk about using the stack.
And if you have questions, you can contact me at Miller MJ at you and Kate. I e d u. And you can find me on Twitter at no house 30.
How to Use GDB (BSWJ)
In this part of the Breaking Stuff with Joe (BSWJ) series, Joe Perry demonstrates how ...
Certificate of Completion Offered
How to Use IDA (BSWJ)
The IDA is used throughout the IT and cybersecurity industries by exploit developers, vulnerability analysts, ...
Certificate of Completion Offered