# COMP 2003: Assembly Language and Digital Logic - PowerPoint PPT Presentation

1 / 9

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

COMP 2003: Assembly Language and Digital Logic

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

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

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

pList

pNext

datum

Note: These ranges could be anywhere in memory (except overlapping)

DWORD datum;

};

pNext

datum

pNext

datum

pNext

datum

NULL

stack frame

subesp,4

esp+8

datum

esp

esp+4

callAllocateMemory

AllocateMemoryparam

movdwordptr[eax],NULL

movecx,[esp+8]

address of the memory range returned in eax

mov[eax+4],ecx

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

stack frame

subesp,4

esp+12

movecx,[esp+8]

esp+8

datum

mov[esp],ecx

esp

esp+4

movecx,[esp+12]

mov[eax+4],ecx

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

stack frame

moveax,[esp+4]

esp+4

esp

movecx,[eax]

Alternatively, since NULL==0

cmpecx,NULL

test ecx,ecx

je Done

jzDone

moveax,ecx

Done:

ret

• Need to eventually return from any function