1 / 9

COMP 2003: Assembly Language and Digital Logic

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

farica
Télécharger la présentation

COMP 2003: Assembly Language and Digital Logic

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. COMP 2003:Assembly Language and Digital Logic Chapter 4: Using Memory Notes by Neil Dickson

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

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

  4. Simple Structures • Sequence of named offsets with types Offsets Without Automatic Padding Offsets With Automatic Padding struct ByteVector { 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++

  5. Simple Structures struct ByteVector { 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

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

  7. 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 movdword ptr[eax],NULL mov ecx,[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

  8. Functions of a Linked-List Link* addBefore(DWORD datum,Link* pLink) stack frame subesp,4 esp+12 pLink mov ecx,[esp+8] esp+8 datum mov[esp],ecx esp esp+4 ret address callnewLink newLinkparam mov ecx,[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

  9. Functions of a Linked-List Link* findLast(Link* pLink) stack frame mov eax,[esp+4] esp+4 pLink NextLink: esp ret address mov ecx,[eax] Alternatively, since NULL==0 cmp ecx,NULL test ecx,ecx je Done jz Done 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

More Related