‫So we're now going to take a quick look at the stack now, all the sectors allows you to store temporary
‫information that you don't plan on always keeping.
‫And it's a very clever concept how this works.
‫So what I want you to do, I want you to go push zero x f.
‫If that's all I wanted to do, I wanted to press, emulate and just press a single step, and now once
‫you've done that, you can see that the ESP has changed.
‫If you press step back, you see f f a single step forward.
‫Now, this has changed FC so the stack grows downwards from memory.
‫So when you push, it grows downwards.
‫So what we've essentially done when we press a single step, we push the value, the media value f f
‫to the stack at the standpoint of position.
‫So we press single step, we see FC.
‫So if if we now go to view memory and just buy here type F, F, C, you can see that our value is there
‫if you do f, e, which is what it used to be.
‫You can see nothing to do FFC, you see that, so that's how it works, the stack goes downwards and
‫we push values to the stack and then if we pop by doing pop X and we do emulate, if you not a single
‫step, once you can see this changes to FFC, if you single step again, this goes back to fee and X
‫contains the value we popped off the stack so we push for to the stack and then we pop the value back
‫off and put it in the register.
‫So hopefully you can see how that allows you to have temporary memory because what you can do, you
‫can set the stack pointer to a position in memory and then you can make use of the stack to store information
‫temporarily, information that you will pop off later like we've done here.
‫So the stack pointer relies on the SS segment.
‫So that's the segment is zero 700, which is why which is why the value is stored in zero seven hundred
‫FFC.
‫Right, because our stack pointer is, is that FFE.
‫So if we was to ever change the stack pointer, the stack would go down from that position instead.
‫So that's an important thing to note.
‫So you can see by using the push and pop instructions, we can easily push temporary data that we plan
‫on keeping temporarily.
‫Now calling subroutines actually uses the stack as well.
‫Now, if we if we did a jump, for example.
‫And I know we have some code here and we want to jump over it, so we use a jump.
‫Now, this doesn't use the stack, this will jump here and it'll never return, but if you use coal,
‫it will go there, but expect come back.
‫So, for example, if we did if we did a label here, we'll go on a test.
‫And by the way, when you reference labels, when it's assembled, it gets converted to addresses,
‫obviously, because the assembler knows the address that this test label should be at based on the origin
‫here.
‫And some other clever, clever position techniques are uses such as counting the size of our instructions
‫and stuff like that.
‫So if we went here, I we'll call this May.
‫Yeah.
‫Uh, main calls test.
‫Yeah.
‫And here do rats as well because we need to return control to the MSDOS operating system.
‫Returns from subroutine coal.
‫Coal's a subroutine.
‫So if we now go here, I'm just going to go move a page.
‫Xerox 20.
‫OK, now in our test, uh, make sure there's a buy there as well.
‫OK, because we because we're calling test, so we need to return from that subroutine.
‫I just wanted to move.
‫I hate zero x ten.
‫OK, so if you if you emulate that now I want you to single step and you can see that Kohl's this address
‫here.
‫OK, and then that moves ten single step again that moves ten and a half and then we call ret to return
‫from the subroutine.
‫So that returns from the subroutine.
‫So it then runs line ten because we call test, we return from the subroutine which puts us on line
‫ten.
‫Yeah.
‫So then if we then step through that then by using single step again you can see that changes the 10
‫H in the H register to 20 and then finally return again to give control back to starts right now the
‫reason I show you this is because Cole uses the stack to store return addresses.
‫So I'm now going to explain how Cole uses the stack to return addresses we emulate again.
‫And this time I want you to go view memory and let's change this to F zero, not FFE.
‫I know the stack point is point to FFE, but we use FFC.
‫We're just so I have more to see so I can show you how it's working.
‫So if you now single step on the call, you can see it's put some memory in here, zero three zero one.
‫So it's actually stored the return address.
‫Right.
‫So then if you single step again and in Rhett's it knows where to jump back to because the return address
‫is stored here, so it jumps to zero one zero three.
‫And you might notice these bytes back to front.
‫Why do we see zero three zero one?
‫Well, this is this is a thing called ending this.
‫Now we're ending this is a spite order.
‫So little endian will flip the bytes.
‫So say we say we push the value, so we push the value zero x four.
‫It'll be stored in memory as FOAF.
‫That's just how they do it.
‫And then big endian means it'll be in order.
‫So if we did four, it'll be stored as four.
‫So that's very important thing to know about intel architectures is they use little endian, little
‫endian this now.
‫So you can see that by calling the call, it pushes the the address that needs to return to to the stack.
‫So then that way then when we come back we call Recht.
‫It knows where to return to.
‫So that is how all that works guys.
‫So zero three zero one is actually zero one zero three.
‫So then one one we call it, the processor will look here.
‫It'll see that this is the value, it'll pop it off the stack and then it will start executing from
‫there.
‫So that is how subroutines work in the process.
‫So this is how we can call things and then be able to get the execution back to us is because Cole uses
‫the stack, stores the address that it needs to run next.
‫So then when you call Rete, it pops the value from the stack, much like we use our position earlier
‫on in this tutorial.
‫It pops it from the stack.
‫It starts running the code from there.
‫So that's how subroutines work.
‫I hope that explains how the stack works, is very handy and is pretty much necessary in all modern
‫processes.
‫When when you write a program in C, for example, and you have loads of low.
‫Variables, they they rely heavily on the stack to be able to store those temporary variables for you
‫because, you know, when you leave a scope and see in the C programming language, all of those primitive
‫variables like your your chars, they all get removed.
‫Right.
‫Well, thanks to the stack.
‫Why that's possible.