01 Stacks in Memory and Stack Operations


[MUSIC] . Welcome back, it's time for the fifth section of the course Procedures and Stacks. What we're going to talk about here is another aspect of Assembly Language. Namely how we define procedures or methods in Assembly Language. And how do we keep track of where we are in the execution of a set of nested procedure calls. So, this this section will cover quite a few topics. We're going to start with talking about the concept of a stack. A special memory construct that we're going to use to keep track of this calling history through our programs. then we're going to talk about return addresses and return values, namely when we return from a method or procedure. where do we return to and how do we remember that and what value do we return? how is that stored? we'll then talk briefly about stack based languages and the Linux stack frame. A particular type of stack that we'll be using throughout the rest of the course. Then we'll get into some other aspects of stacks. Namely how we pass arguments to procedures on the stack and allocate local variables for a procedure. and some of the conventions we have to follow to make sure we keep all of this stuff straight. we'll also talk about the differences between the 32 bit and 64 bit architectures. So, let's start, this video with, the first topic which is basically what is a stack in memory and what are some basic stack operations. Okay, so, if we look at memory, the way we ordganise it for, our programs. Is that we'll have an area where we will keep all of our instructions all these assembly language instructions you've been learning about. they will be usually stored in the area starting at the low end of the memory here shown at the bottom. Then aboce that we'll, store some literals in the program, by literals we mean data that doesn't really ever change. Things like a string that I might want to print, as an example. we'll also do the same thing with static data, variables that we know we're going to need, maybe big arrays of values. that will know that we always will have that storage available to them. And allocate those in memory in a static location meaning there'll always be in the same place. This is the case, for example, with global variables in C. that's where they're placed, then, we have some more interesting areas of memory. We have a dynamic data area, also referred to as the heap. Often and the heap holds data that we create just for a place to store some values temporarily. While we might be in the middle of a procedure or in a phase of our program. but we'll probably reclaim this memory later, because we won't need it for very long during the execution of our program. these variables, this space is allocated using things like New and Java or Mallock in C basic memory allocation procedures that we can call. To get us a new space for the particular data type we need. And this region grows as we ask for more and more dynamic data space and it grows up in the memory to higher addresses. The stack is yet another area of memory where we're going to store this procedure context as we do these procedure calls. And it's going to start at the high end of memory and grow down in the other direction. and we'll see in a second what's what's going to be in that stack to you know, what is, what do we mean by procedure context. For now the important thing to see, is that in fact the stack grows down. The dynamic data grows up, in addresses, and the, these two could meet, and that would be a really bad thing. either that we're putting to much requirements on our call stack, we're going to deeply nested into procedures. Or we're just asking for too much data at one time. And this is the kind of error we'll see in our program sometimes when we, when the operating system says its out of memory. Meaning that its overfilled this area and those two regions, the stack and the dynamic data are running into each other. and potentially override it. Okay, another thing to keep in mind about these memory areas is that the instructions, the literals. And the static data are initialized when the process starts up. When the program starts up. The dynamic data is managed by the program, meaning we're writing in the program when to allocate more memory, when to create a new object. while the stack is managed automatically by the compiler. It's there's instructions inserted by the compiler to add things to the stack or remove things from the stack, okay. Another aspect of these areas is that you'll notice that the instructions are read-only and executable. Meaning, we interpret the bits there as the instructions for the CPU. The literals are also read only because they're fixed, we're not changing those strings ever. But they are not executable. Okay? We're not going to try to execute our our data strings. So, that's really important because that keeps things from getting confused. should we accidentally jump to this area to try to execute instructions. The static data is, of course, writable. We have to be able to change it but it is also not executable. The dynamic data, the same way and the stack the same way. this, this are important limitations that the stack is actually just data values. And addresses that were used but that are not instructions. So, its not executable and we will see this is an important aspect of security in modern computer systems. Okay, we'll see that in one of our assignments. Okay, so let's take a look at the call stack for the IA32 architecture. as I mentioned we'll put the bottom of the stack at the top of the memory And the bottom[LAUGH], of the stack or the top of the stack at the bottom so the stack rose in a downward direction. And this is just a convention that we use it of course is kind of counter intuitive. given that we're going to talk about the top of the stack[LAUGH], being at the bottom. But bear with us for that, this is typically how it's drawn upside down. The important thing to remember is that as we add things to the stack, the addresses get lower. And as we take things off the stack, the addresses get higher, as we move up. Okay. There's a special register in the I30, IA32 architecture called ESP for the Extended Stack Pointer. That always points to the top element of the stack. the last thing that was placed on the stack. Okay. So, the first stack operation we're going to look at is the push instruction and here I'm showing a push long meaning a 32-bit value. And it's given a source register to or memory location to push from. And what basically that does is it takes the value at that source, whether it's a register or a memory location and adds it to the top of the stack. It also decrements the stack pointer by four. And why four? Because it's a push long, remember? Right? Four byte's a 32-bit word. So, the stack pointer is decremented, and now points to the new location in memory, where we've added a or copied that value. into the memory, okay. So, that's pushing something onto the stack, placing it on the top of course here at the bottom, as we've drawn it upside down. All right as you'd expect, there's a corresponding pop instruction, taking things off of the stack. And in this case we also give it a destination to go take that value we pop from the stack. and where to go put it? We need the address of that. And that again can be a memory location or a register in the, in the CPU. So, the way that works is we remove that remove in quotes for now. we remove that value from the top of the stack and adjust the stack pointer for up. Again for four bytes of that 32-bit word. Now, when we say remove this word, we're not really removing it. it's still there in the memory we're just not referencing it anymore. We've adjusted our stack pointer to point to the next value down the stack. but that, those bits if you will, are still in the memory at that location. It's just that we don't we, we acknowledge that they're not to be interpreted anymore. We've effectively removed them in that sense and that we can reclaim this space and push something new onto the stack. And overwrite those bits, because we don't care about them anymore. We've popped them off, and gone and saved them away in our destination, okay? So, they are now safely stored there and we can use the stack for other things.

Wyszukiwarka

Podobne podstrony:
01 [ABSTRACT] Development of poplar coppices in Central and Eastern Europe
Visual Resolution in Coherent and Incoherent Light
Natural Variability in Phenolic and Sesquiterpene Constituents Among Burdock
Cranberries Songs in red and gray
Injuries and overuse syndromes in competitive and elite bodybuilding PubMed NCBI
Human resources in science and technology
2003 Huntington in health and dis JCI
HIM In Love And Lonely
In Love and War
Aristotle On Memory And Reminiscence
20 Seasonal differentation of maximum and minimum air temperature in Cracow and Prague in the period

więcej podobnych podstron