Memory Segments Code code contains user program Data

  • Slides: 5
Download presentation
Memory Segments • Code (. code) • contains user program • Data (. data)

Memory Segments • Code (. code) • contains user program • Data (. data) • contains fixed program data • Stack • maintains parameters, local variables, return code for functions • Heap • program data • storage is allocated at runtime Note: Heap and Stack grow towards each other 1

Dynamic Memory Allocation • Reserving memory at runtime for objects • aka heap allocation

Dynamic Memory Allocation • Reserving memory at runtime for objects • aka heap allocation • standard in high-level languages (C++, Java) • HLL’s have built-in “heap managers” • Heap manager • allocates large blocks of memory • maintains free list of pointers to smaller blocks • manages requests by programs for storage C++ example - allocate an integer array at run time: int size; cin >> size; int array[] = new int[size]; Irvine, Kip R. Assembly Language for x 86 Processors 7/e, 2015. 2

Windows Heap-Related Functions Irvine, Kip R. Assembly Language for x 86 Processors 7/e, 2015.

Windows Heap-Related Functions Irvine, Kip R. Assembly Language for x 86 Processors 7/e, 2015. 3

Sample Code • Allocate block of memory from existing heap: . data h. Heap

Sample Code • Allocate block of memory from existing heap: . data h. Heap HANDLE ? p. Array DWORD ? ; heap handle ; pointer to array . code push 1000 push HEAP_ZERO_MEMORY push h. Heap call Heap. Alloc cmp eax, 0 jne OK jmp DONE OK: mov p. Array, eax DONE: Irvine, Kip R. Assembly Language for x 86 Processors 7/e, 2015. ; number of bytes to allocate ; initialize to all zeros ; heap handle ; allocate from current heap ; NULL on a failure ; successful, save the pointer 4

Sample Code • Free a block of memory previously created by calling Heap. Alloc:

Sample Code • Free a block of memory previously created by calling Heap. Alloc: . data h. Heap HANDLE ? p. Array DWORD ? ; heap handle ; pointer to array . code push p. Array push 0 push h. Heap call Heap. Free ; pointer to memory to free ; flags, generally zero ; the targeted heap ; eax will be non-zero if successful! Example : Heaptest Irvine, Kip R. Assembly Language for x 86 Processors 7/e, 2015. 5