1 / 32

Intro to Computer Org.

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

montana
Download Presentation

Intro to Computer Org.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Intro to Computer Org. Assembly Programming – An In-Depth Look At Memory

  2. Layout Of Memory

  3. Layout Of Memory • Text segment • Holds all instructions of the program

  4. 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.

  5. 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.

  6. 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

  7. 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

  8. 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.

  9. 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)

  10. 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

  11. 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

  12. 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.

  13. 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.

  14. Memory Segments • Why is it necessary to have each of the different memory segments?

  15. 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?

  16. 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?

  17. 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

  18. 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

  19. 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?

  20. Memory Segments • As covered in a previous class, it is possible to allocate an amount of memory dynamically at run-time within the stack.

  21. 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

  22. 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.

  23. 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

  24. 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?

  25. 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?

  26. 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.

  27. Memory Segments • Keep in mind that variable-sized data can always be referenced by static memory and actually stored in dynamic memory.

  28. 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?

  29. 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!

  30. 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)

  31. 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.

More Related