330 likes | 453 Views
Intro to Computer Org. Assembly Programming – An In-Depth Look At Memory. Layout Of Memory. Layout Of Memory. Text segment Holds all instructions of the program. Layout Of Memory. Data Segment Static part
E N D
Intro to Computer Org. Assembly Programming – An In-Depth Look At Memory
Layout Of Memory • Text segment • Holds all instructions of the program
Layout Of Memory • Data Segment • Static part • Holds all globally accessible data, as well as certain constants known at run time. (Ex: pre-initialized arrays.) • Dynamic part • Gives allocation space for data whose memory size is not known until runtime. • This memory isn’t tied to scope like memory in the stack segment is.
Layout Of Memory • Stack segment • Provides the necessary facilities for proper function usage. • Stack pointer is mobile and not guaranteed to be at a set address at any frame. • However, any frame’s contents are guaranteed to be at a set offset from the frame/stack pointer, when maintained properly.
Referencing Memory • What parts of memory may be referenced by a pointer? • Static data • Dynamic data (the heap) • The stack • Instructions • a+b • a+b+c • all of the above • none of the above • other
Referencing Memory • What parts of memory may be referenced by a pointer? • Static data • Dynamic data (the heap) • The stack • Instructions • a+b • a+b+c • all of the above • none of the above • other
Referencing Memory • In some programming languages (like C/C++), it is possible to have a pointer to a function – and use it! • Consider the MIPS instructions jr and jal. • jr allows us to jump to the address in a register. (Address = pointer) • jal allows us to jump (to a label) and saves a return value.
Referencing Memory • In some programming languages (like C/C++), it is possible to have a pointer to a function – and use it! • Consider the MIPS instructions jr and jal. • If there were a combined instruction (say, jalr) that did both, that would allow use of a function pointer. • jalr is an actual instruction, but we won’t use it in this class. (jump and link register)
Referencing Memory • What parts of memory may store pointers? • Static data • Dynamic data (the heap) • The stack • Instructions • a+b • a+b+c • all of the above • none of the above • other
Referencing Memory • What parts of memory may store pointers? • Static data • Dynamic data (the heap) • The stack • Instructions • a+b • a+b+c • all of the above • none of the above • other
Referencing Memory • Can a pointer reference another pointer? • Yes! In C/C++, an example would be as follows: int i = 2; int* j = &i; int** k = &j; • There is no fundamental limit on what pointers may reference.
Referencing Memory • Remember, a pointer is still a pattern of bits. • Think of an address as an unsigned integer – that integer could be an index into a vast array called “memory.” • Since pointers reference memory, which stores another pattern of bits, pointers to pointers can exist.
Memory Segments • Why is it necessary to have each of the different memory segments?
Memory Segments • Why is it necessary to have each of the different memory segments? • Another way of wording this question– what do we lose by eliminating one?
Memory Segments • Why is it necessary to have each of the different memory segments? • Another way of wording this question– what do we lose by eliminating one? • First, what happens if we eliminate static memory?
Memory Segments • If we eliminate static memory, we don’t lose the following: • The ability to store data in memory, regardless of size • But we have to keep all pointers alive within registers! • Function calls are pretty much unaffected. • The ability to have compile-time data
Memory Segments • If we eliminate static memory, we lose the following: • static + global variables • (cleanly) predefined strings • pre-initialized data structures/arrays • organization of pre-known fixed-size memory
Memory Segments • Why is it necessary to have each of the different memory segments? • Another way of wording this question– what do we lose by eliminating one? • What happens if we eliminate the dynamic memory segment – the heap?
Memory Segments • As covered in a previous class, it is possible to allocate an amount of memory dynamically at run-time within the stack.
Memory Segments • If we eliminate the heap, we lose the following: • Cleanly organized stack space • Consider a method with two different local variables, each of which has a size that is unknown until run-time. • Scope-free pointers
Memory Segments • If we eliminate the heap, we don’t lose the following: • The ability to dynamically allocate data • Stack-frame handling code can be adjusted to hold variable-sized variables and data structures, as seen in a previous lecture.
Memory Segments • If we eliminate the heap, we lose the following: • Cleanly organized stack space • Consider a method with two different local variables, each of which has a size that is unknown until run-time. • Scope-free pointers
Memory Segments • Why is it necessary to have each of the different memory segments? • Another way of wording this question– what do we lose by eliminating one? • What happens if we eliminate the stack?
Memory Segments • If we eliminate the stack, we don’t lose the following: • The ability to call nested functions • The ability to save register contents across function calls • How is this the case?
Memory Segments • We know at compile-time which registers hold contents that we wish to save. • As a result, we can allocate enough static memory for each function’s register contents. • On a function call, the register contents can then be written out to static memory.
Memory Segments • Keep in mind that variable-sized data can always be referenced by static memory and actually stored in dynamic memory.
Memory Segments • We know at compile-time which registers hold contents that we wish to save. • As a result, we can allocate enough static memory for each function’s register contents. • But… what happens if the function wants to call itself?
Memory Segments • We know at compile-time which registers hold contents that we wish to save. • As a result, we can allocate enough static memory for each function’s register contents. • But… what happens if the function wants to call itself? • It will try to use the same locations!
Memory Segments • If we eliminate the stack, we lose the following: • Recursive methods (Whether the recursion has to pass through other methods first or not)
Allocating Memory – An Aside • When allocating dynamic memory, note that it is always of a fixed size – just one that is fixed at run-time instead of compile-time. • Just like in Java, to “expand” an array, you must make a new array and copy the elements over.