MOP Midterm 2 - Linux/C Programming/Heap operations, MOP Midterm 2 - Assembly Language and ISA, MOP Midterm 2 - Memory Heirarchy/Cache

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/161

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

162 Terms

1
New cards

What is shadowing?

when a local variable has the same name as another variable in the same scope thus hiding the non local variable (should and can be avoided!)

2
New cards

What is the POSIX API?

The portable OS interface standard for maintaining compatibility among Unix OS, whose functions are accessible with unistd.h

3
New cards

What is unistd.h?

A collection of OS functions (system call wrappers) used to access functions in the POSIX API. Some of these functions like brk() and sbrk() can be used to implement your own heap allocator.

4
New cards

What is brk?

"program break" - pointer to end of program and top of heap

5
New cards

What does brk() do?

brk(void *addr) OS function that sets top of heap to passed address.

**OS initially clears new pages of heap memory for security

6
New cards

What is errno?

Set by OS functions to communicate a specific error to program code. Error number can be interpreted with strerror(errno)

7
New cards

What does strerror() do?

strerror(errno) returns a description of the error associated with errno

8
New cards

What will happen if you use the OS functions brk() or sbrk() along with malloc(), calloc(), and free()?

Undefined program behavior

9
New cards

What does sbrk() do?

sbrk(intptr_t incr) OS function that takes an int (that is implicitly converted to intptr_t so it can be added to a pointer) size to (+) or (-) the size of the heap by incr bytes.

10
New cards

What is stdlib.h?

It is the standard library for C programming. It contains functions that allow for conversion, execution flow, math, search, sorting, and randoms

11
New cards

What do atoi() and atol() do?

Converts ascii (string/char array) to an integer/long respectively

12
New cards

What's the difference between atol() and strtol()

atol(char* str) can only convert from base 10 and always returns 0 if the parse is impossible

strtol(char str, char* endptr, int base) can convert any number from base 2 to 36 and on failure can give location where parse ended by changing the value of the passed pointer and an error if the number is too big or small

13
New cards

What is bsearch()?

A standard library function that conducts a binary search

14
New cards

What is qsort()?

A standard library function that implements the quick sort algorithm

15
New cards

What is a *void?

A generic pointer

16
New cards

What is the difference between calloc and malloc?

calloc populates all returned heap space with 0s and takes two arguments (numElements, sizeofElement), whereas malloc doesn't modify contents heap space before allocation and takes one argument (numBytes)

**Note calloc is much slower because it clears the data to 0s

17
New cards

What does realloc() do?

realloc(numElements, elementSize) resizes a previously allocated block of memory and returns a new pointer

18
New cards

What is a heap block?

A chunk of contiguous memory with overhead and payload

19
New cards

What is overhead?

Data/Padding that takes up extra heap space from payload data.

May contain a pointer to the next block if the allocator uses a linked list

20
New cards

What is payload?

The part of a block that holds the data stored there that is used by the process

21
New cards

What is the allocator?

A program that allocates, frees, splits, and coalesces heap blocks

22
New cards

What is the difference between an implicit and explicit allocator?

An implicit allocator uses a "new" operator to determine number of bytes needed, and the garbage collector automatically frees unused heap memory

An explicit allocator requires the user to manually allocate and free heap memory

23
New cards

What are the two goals in allocator design?

Maximize throughput - # of malloc and free requests allocator can satisfy (measured in O(n))

Maximize Memory Utilization - % of memory used for payload (more payload bytes, less overhead)

**INCREASING ONE DECREASES THE OTHER GENERALLY

24
New cards

What are the requirements of a heap allocator?

1. Provide immediate response

2. Handle any sequence of malloc and free requests

3. Can't move allocated blocks

4. Must follow memory alignment requirements

25
New cards

What are the two requirement of double word alignment?

1. Block SIZE and payload ADDRESS must be a multiple of double word length (usually 8)

26
New cards

What is external fragmentation?

total memory space exists to satisfy a request, but it is not contiguous

27
New cards

What is internal fragmentation?

allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used

28
New cards

Why doesn't Java allow primitives on the heap?

Because primitives are very small in size (1-4 bytes), so a lot of padding is required for the memory alignment requirements; Too much internal fragmentation

29
New cards

What is the difference between explicit and implicit free list?

Explicit utilizes a data structure to store the location and size of free blocks.

Implicit store size and status within heap blocks in overhead as headers/footers

30
New cards

What is explicit free list organization?

- When free block location and size are stored in an unordered data structure (like an array of structs with a pointer and int each) by allocator. This list can be separate from or integrated into the heap

- programmer only needs to keep trace of block size (all free blocks are in the data structure)

- More memory is needed to store free list, but time is faster because only free blocks are searched

31
New cards

What is implicit free list organization?

When headers (first word) are used to store size and status of block, and headers are used so allocator can have info about previous block

-Programmer must track block size and status

-Can use less memory than explicit list

-Long search time because every block must be checked

32
New cards

How are the last 3 bits of the size of a block able to be used to store additional information?

Because the block size must be a multiple of 8, so last three bits are always 0.

33
New cards

What are heap placement policies?

Strategies used to determine which free heap block to use to satisfy a request

34
New cards

What are the different placement policies?

First fit - starts at beginning of heap and allocates first block found

Next fit - starts from most recently allocated block and allocates first block found

Best fit - runs through entire heap and allocates the block closest to desired block size

35
New cards

Why does the first fit policy have good memory utilization?

Likely to choose block close to desired size because smaller blocks are at the start of the heap

36
New cards

Pros and cons of block splitting by allocator?

( + ) Better memory utilization, less overhead

( - ) Worse throughput, more blocks = more jumps = slower search

37
New cards

What's the difference between immediate and delayed coalescing?

Immediate coalescing will coalesce any adjacent blocks after freeing a block of at all possible

Delayed coalescing will only coalesce when there is a need for it

38
New cards

Why don't allocated blocks need footers?

Footers are only used for coalescing

39
New cards

What is false fragmentation?

When two contiguous blocks of memory have enough space to satisfy a request but can't because they are separate blocks

40
New cards

How can you integrate an explicit free list into the heap?

By adding extra overhead to free blocks as predecessor and successor pointers. This increases minimum block size.

41
New cards

Does the order of free blocks in the free list need to be the same order as they are found in the address space?

No

42
New cards

What's the difference between EFL address ordering and last-in ordering?

Address ordering orders blocks from low to high address in EFL (better memory utilization malloc() w/ FF policy)

Last-in ordering puts most recently freed block to front of EFL (better free() performance cuz no sorting)

43
New cards

What's the difference between EFL simple segregation and fitted segregation

-Simple segregation has a separate EFL for each block size, so size headers are necessary, just successor pointers.

-Fitted segregation uses size ranges rather than specific sizes.

44
New cards

How does simple EFL segregation work?

-Different EFL for each block size

-headers aren't necessary, block just needs successor pointer

-Fast malloc and free

-If corresponding list is empty, search larger lists or request more memory from OS

***Causes more fragmentation of both types because no splitting or coalescing

45
New cards

What is free list segregation?

When the heap allocator uses an array of free lists segregated by block size to organize free blocks

46
New cards

How does fitted EFL segregation work?

-Free Blocks are segregated into different EFLs for different size ranges (small, medium, large)

-allows for better throughput as only some free blocks need to checked

-Uses FF searching through range, if nothing found go to larger range and split block

47
New cards

Why don't conse

48
New cards

What is ISA?

Instruction Set Architecture, like IA-32

49
New cards

What is machine code?

This is low-level binary code that represents how computer hardware and CPUs understand instructions. It is specific to different machines!

50
New cards

How long is an IA-32 instruction?

1-15 bytes

51
New cards

T/F Memory bits do not distinguish instructions from different data types from pointers

True, the only way the machine knows what kind of info it's getting is from how memory is accessed (instruction fetching or operant loading) and from the operation and operands of the instruction itself

52
New cards

How does word size differ between caching and assembly?

4 bytes in cache, 2 bytes in assembly

53
New cards

How does IA-32 machine code see a C char?

byte (b)

54
New cards

How does IA-32 machine code see a C int?

Double word (l) 4 bytes

55
New cards

How does IA-32 machine code see a C char*? (All ptrs)

Double word (l) 4 bytes

56
New cards

How much data can IA-32 registers store?

1, 2, and 4 bytes of data or a 4 byte address

57
New cards

%eax

Accumulator register for arithmetic operations.

58
New cards

%ecx

Counter - counting and loops

59
New cards

%edx

data - stores data

60
New cards

%ebx

base - stores base value/address

61
New cards

%esi

source index -

62
New cards

%edi

Destination Index -

63
New cards

%esp

Stack pointer register indicating top of stack.

64
New cards

%ebp

Base pointer - points to base of stack

65
New cards

Which two registers used for stack bounds?

esp and ebp, they are only used to store the start and end of the stack

66
New cards

%eip

Program Counter (extended instruction pointer)

67
New cards

What does the program counter do?

Holds the address of the next instruction to be fetched. This address is obtained while the current instruction is executed

68
New cards

What are condition code registers?

(AKA e-flags) 1 bit registers whose bits are automatically set to store status of the most recent ALU operation for conditional execution

69
New cards

What are operand specifiers?

S and D, S specifies the location of value to be used by an instruction and D specifies location where result is to be stored. They come in the form of different ways of formatting operands to show how the machine should interpret them.

70
New cards

What does the $ operand specifier represent?

A constant value rather than a location

71
New cards

How do you use a hex number as a constant in assembly?

$0xBEEFBABE

72
New cards

What does the % operand specifier represent?

Specifies an operand value that is in a register

73
New cards

What is an effective address?

An operand to an instruction that references memory, sometimes given indirectly

74
New cards

What is absolute addressing mode?

Imm - A numerical value with no specifiers. The operand is located at the memory location corresponding to the numerical value

75
New cards

How is the effective address obtained in indirect addressing mode?

- The effective address of the operand is found in the register

76
New cards

How do you know what size specifier to use when you only know the one of the operands?

Since that operand will be a register/constant, use its size as the size specifier

77
New cards

What are all the 8 bit registers in the 32 bit system?

ah-dh & al-dl

78
New cards

What are all the 16 bit registers in the 32 bit system?

ax-dx, si, di, sp, bp

79
New cards

What addressing mode is this: (%Reg)

indirect addressing

80
New cards

How is the effective address obtain in base + offset mode?

The effective address is found by adding the offset to the address in the given register

81
New cards

What addressing mode is this: Imm(%Reg)

base+offset

82
New cards

How is the effective address obtained in indexed addressing mode?

- Effective address is obtained by adding values contained in index and base register

83
New cards

What addressing mode is this: (%BaseReg,%IndexReg)

indexed addressing mode

84
New cards

What addressing mode is this: Imm(%Reg,%Reg)

Indexed + offset addressing mode

85
New cards

How is the effective address obtain in indexed + offset addressing mode?

- Effective address is obtained by adding values contained in index and base register plus the offset

86
New cards

What addressing mode is this: Imm(%Reg,%Reg,s)

Base + scaled index + offset

87
New cards

How is the effective address obtained in Base + scaled index + offset addressing mode

- Effective address is obtained by adding value contained at the base register to the result of scaling the value in the index register, then add the offset

88
New cards

What addressing mode is this:

(%Reg,%Reg,s)

Scaled index no offset

89
New cards

What addressing mode is this:

Imm(,%Reg,s)

Scaled index no base

90
New cards

What addressing mode is this:

(,%Reg,s)

Scaled index no base no offset

91
New cards

What does the % operand specifier represent?

A register

92
New cards

What size can base registers be

Only 32 - bit

93
New cards

What values can scale factor be?

Only 1,2,4, or 8

94
New cards

What do the instruction Mov, PUSH, and POP do?

Copy data from source to destination to allow data to move around memory and registers

95
New cards

How does MOV work?

MOV S,D - Copies S to D

**S and D must be of equal size

96
New cards

How do Movz and Movs work?

MOV[Z/S][b/w/l][b/w/l] S,D allow for copying data when S and D are different sizes. MOVZ fills the remain space with 0s and MOVS sign extends to fill the rest of the space

97
New cards

How does pushl work?

pushl S allows for a double word to be copied onto top of stack.

**Note that storing data on the stack requires it first be grown by subtracting from stack pointer: R[%esp] <- R[%esp] - sizeof(S)

M[R[%esp]] <- S

98
New cards

How does popl work?

popl D allows for double word at the top of the stack to be copied from the stack

**Note that removing data on the stack requires it be shrank by adding to stack pointer after data is copied:

M[R[%esp]] -> D

R[%esp] <- R[%esp] + sizeof(D)

99
New cards

Which way does the stack grow?

Down to lower memory addresses.

100
New cards

Which way does the heap grow?

UP towards the Stack