90 likes | 205 Views
COMP 2003: Assembly Language and Digital Logic. Chapter 4: Using Memory Notes by Neil Dickson. Memory Allocation. Can already “get” ( allocate ) memory to use (with static memory allocation ) Can just make a global variable to do this
E N D
COMP 2003:Assembly Language and Digital Logic Chapter 4: Using Memory Notes by Neil Dickson
Memory Allocation • Can already “get” (allocate) memory to use (with static memory allocation) • Can just make a global variable to do this • Need a way to also “give back” (free) memory for reuse (with dynamic memory allocation) • Take a piece of memory from the heap; given back when you say you’re done with it • Make a local variable on the stack; given back upon return from the function
Dynamic Memory Allocation • On the heap (a big chunk of memory) • Done by calling functions that manage the heap • Slow: 1,000 to 10,000 times slower than an operation like addition • No limits on when you can allocate or free • Big: can be up to GBs of space • On the stack • Done by subtracting/adding to esp, so very fast • Awkward to reallocate data after start of function • Small: usually just 1MB
Simple Structures • Sequence of named offsets with types Offsets Without Automatic Padding Offsets With Automatic Padding structByteVector { WORD type; DWORD length; DWORD capacity; BYTE isLocked; BYTE* pContent; }; +0 bytes +0 bytes +2 bytes +4 bytes +6 bytes +8 bytes +10 bytes +12 bytes +11 bytes +16 bytes 15 bytes total size 20 bytes total size 19 bytes total size for 64-bit code 24 bytes total size for 64-bit code Usually the default in assembly and C Usually the default in C++
Simple Structures structByteVector { WORD type; DWORD length; DWORD capacity; BYTE isLocked; BYTE* pContent; }; 0 1 2 3 4 5 6 7 8 9 A B C D E type length capacity isLocked pContent
Data Structures • One or more organized ranges of memory • e.g. linked-list: pList pNext datum Note: These ranges could be anywhere in memory (except overlapping) struct Link { Link* pNext; DWORD datum; }; pNext datum pNext datum pNext datum NULL
Functions of a Linked-List Link* newLink(DWORD datum) stack frame subesp,4 esp+8 datum mov[esp],sizeofLink esp esp+4 ret address callAllocateMemory AllocateMemoryparam movdwordptr[eax],NULL movecx,[esp+8] address of the memory range returned in eax mov[eax+4],ecx addesp,4 ret • Need to eventually return from any function • We will need to call void* AllocateMemory(DWORD size), • so reserve stack space for its parameter and remember to give it back • Put the parameter value in before calling AllocateMemory • Fill in the pNext member of Link • Get the value of datum into a register to copy it into the Link • Return the address of the new link in eax
Functions of a Linked-List Link* addBefore(DWORD datum,Link* pLink) stack frame subesp,4 esp+12 pLink movecx,[esp+8] esp+8 datum mov[esp],ecx esp esp+4 ret address callnewLink newLinkparam movecx,[esp+12] address of new link returned in eax mov[eax+4],ecx addesp,4 ret • Need to eventually return from any function • We will need to call void* newLink(DWORD datum), • so reserve stack space for its parameter and remember to give it back • Get the value of datum into a register to pass it as a parameter • Fill in the pNext member of the new Link with the given pLink • Return the address of the new Link in eax
Functions of a Linked-List Link* findLast(Link* pLink) stack frame moveax,[esp+4] esp+4 pLink NextLink: esp ret address movecx,[eax] Alternatively, since NULL==0 cmpecx,NULL test ecx,ecx je Done jzDone moveax,ecx jmpNextLink Done: ret • Need to eventually return from any function • Get the address of the first Link (pLink) • Get its pNext member, • so that we can check whether it’s NULL (the end) • If it is NULL, we’ve found the last Link, so return its address in eax • If not, move to the next link and repeat