Final Exam Flashcards

1
Q

Describe the “heap” region of computer memory used by the C programming language and its relation to dynamic memory allocation

A

Heap memory: Starting address x4000 - x6FFF. When you define a variable, the starting address of that variable is the starting address. Like Assembly Labels.

a large pool of memory that can be used dynamically. This is memory that is not automatically managed – you have to explicitly allocate (using functions such as malloc), and deallocate (e.g. free) the memory. This is why a programmer has control over.

Accessible at runtime and in the user data memory

Dynamic means at a run time. Not at compile time.

limited by the physical memory available and must use pointers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Describe the basic functions that interact with the heap in the standard C library

A

Uses malloc() and free()

void* malloc(size_t size) ; // request memory allocation from heap

Returns a pointer to a region on the heap of size size bytes

void* is “generic pointer” (C for “just an address”).

Can pass it but cannot dereference

Making dinner reservations. This secures you reservation

void free(void* ptr) ;
Clears reservation for address pointed to be ptr
Manage by user-level C runtime library called libc 

Interface function declarations found in stdlib.h

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Discuss memory leaks and other dynamic memory errors

A

If you do not free the memory you used in heap when you are finished with it will result in what is known as a memory leak – memory that is still “being used”, and not available to other processes. Variables created on the heap are accessible anywhere in the program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Summarize the two basic file types in C: binary and ASCII

A

ASCII: Also known as the test files. Encoded in ASCII.
Can’t open an ASCII file in its true binary form with a text editor. Text editor needs to convert it to ASCII.
Example of ASCII files: .c, .txt, .JAVA, .ASM
Read by human
PennSIm takes in an ASCII file and converts to .OBJ file

Binary files: Files are not in ascii code (1s and 0s) created by a programmer. It is
Example of binary files: .OBJ / .DOC / .PPT files.
Cannot read by humans

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Discuss the connection between file access (read/write) and I/O in assembly language

A

Specially files that are opened automatically for all C programs. Not files but I/O devices. These represent the keyboard and the ASCII display.

stdin: standard input (console) ->
stdout: standard output (console, for output)
stderr: standard error (console, for error message)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Differentiate between a character array and a string in C

A

In C, there are not an official string data type. There is a character type. If you want a string, then you need to add a null at the end of your char string.

No need to add null when you want an array of string.

Character array: Does not have the NULL character terminator.
Char name[ ] = {‘S’, ‘a’, ‘r’, ‘a’}; == [S, a, r, a]

String: Must end with a null character terminator ‘\0’

Char name[ ] = {‘S’, ‘a’, ‘r’, ‘a’, ‘\0’}; == Sara

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Describe basic data types in C and how they are related to computer memory

A

char* my_char_ptr == 1 byte == %s
int* my_int_ptr == 4 bytes == %d
float* my_float_ptr == 4 bytes == %f
double* my_double ptr == 8 bytes == %lf

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Describe arrays in C

A

Any data can be made in an array

Ex: char name[4] ; int my_array [5] ; float my_array[5] ; double my_array[5];

Name of the array is the starting address where the element is stored and follows sequential order. .

Like label
uses Index operator [ ]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Describe the notion of a pointer in C

A

A pointer is a variable that can only contain the memory address of another variable

Pointers are typed meaning they can be dereferenced.

Help pass address to functions instead of data

& gets the address of a pointer or other variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Describe the connection between pointers and arrays in C

A

Difference:
Pointers are variables that contains a memory address of a variable that contains data
Arrays are labels for an address in daa memory that contains data
You can do pointer arithmetic on a pointer but you cannot do pointer arithmetic on the array.
You can change the address held onto by a pointer.
You cannot change the address of an array because it is a label.

Connection: :
Use the index operator [ ] as a dereferencing operator on a pointer if pointer points to an array
Use the ++ operator on a pointer to have it point to the next element it points to in memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Discuss storage classes in the C programming language

A
The stack (local variables, arguments and returns) 
Global regions (global vars and static vars) 
The heap - dynamic space.

There are 2 storage classes: automatic and static

Automatic variables: create a new each time when program’s execution enters in the function and destroys when leaves. lose their values when their block terminates (since they are on the stack)
Types: arguments, return types and local variables

Static variables: retain values between invocations. remains in memory while the program is running.
Types: global variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Define the high-level language of C

A

High - level language means that the program language is user friendly.

Procedural which means C focuses on the program’s flow. Runs the program in main

File oriented

Portability which means same C code can be compiled for different ISAs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Explain how high-level languages are connected to assembly and machine languages

A

C code files I write are “compiled” using the compiler into assembly code. Then assembly code is “assembled” into machine code.

C Code(text files like .c or .h) -> ASM code (text files .asm) -> Machine code (binary files .obj)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain what a compiler is and what it does

A

Compiler converts a high-level language (like C) to low-level language (assembly), for example .c to .asm

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Discuss how high level languages use the “stack” to arrange memory in an organized way

A

C uses stack to store data in data memory.
Holds local variables, return values and arguments (automatic variables )

Stacks work like a pile of dishes. First dish is the last and the last dish is the first dish you are going to take out. FILO (First in last out) So first value has the last address (high address) and last value is the first address(low address). Stack grows backwards in memory

C uses stack because the register file is too small. If more than 8 variables or if we need to call other subroutines.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Explain how LC4 C uses a stack and the implications of the stack mechanism

A

The stack will live in User data memory: x7000 - x7FFF
FILO (First in Last out) Stack grows backwards in memory meaning the last value you added leaves first.

Stack mechanism makes it easy to have functions that call other functions like a recursive functions

Implications: stack space is finite; we cannot call too many variables. In LC4 this can cause an overwrite heap with unpredictable results.

Temporaries, arguments = R6
Local variables 
Caller’s frame pointer = R5
Return address - where to return after function completes 
Return value 
Arguments
17
Q

Discuss alternatives to using the stack

A

LC4 uses memory based calling conventions.

Arguments and return values passed via stack

18
Q

Describe the generic architecture of I/O devices connected to a CPU

A

For input devices,
the CPU checks the status register if input is available. Then reads input from the data register. Data register waits if no input is available.

For output devices:
CPU checks status register to see if it can write. Then writes output to data register.

19
Q

Recall and describe the four basic I/O devices on the LC4

A

Keyboard (input)

Ascii console (output)

128 x 124 16 bit RGB pixel display (output)

Timer (not really an I/O device but looks like one to software)

20
Q

Discuss assembly language and its role in a CPU

A

Assembly language: Symbolic representation of machine language. Instructions are mnemonic ASCII strings.
Only readable by humans.

The CPU can understand the assembly language we write because it translates it to machine language in which the CPU can understand. Assembler is used to produce an object file from the assembly file that you wrote.

21
Q

Define an ISA and its purpose

A

Instruction Set Architecture - an instruction set architecture. All of the information needed to create a program for CPU. A contract between programmer and CPU designer.

22
Q

Describe the purpose and implementation of a program counter in a CPU

A

Purpose of a program counter: the purpose is to store our instruction. PC + 1. Once one instruction is done, it can go to the next instruction.

23
Q

Compare sequential logic to combinational logic

A

Sequential logic: Past and present inputs matter to determine output.

Combinational logic: Past input does not matter for output.

24
Q

Define the three basic pieces of sequential logic: RS latch, D ­latch, and D flip­flop

A

RS Latch: a bi-stable state circuit which means it can ether have 2 stable states. Store last output. Latch because it can “latch” onto incoming data.

D Latch: a logic level triggered device, this means device is sensitive to the WE’s logic.

When WE = 1, D latch opens up so it can write. Q follows D.
WE = 0, D latch closes so Read only. Q does not follow D. Q holds the last value of Q.

D Flip Flop: Edge trigger device which means stores output at the edge of the clock.
Positive edge triggered: Clock goes from 0 to 1. Then Q follows D.
Besides this time, Q holds onto the last value of D.

25
Q

Understand finite­ state machines and their relationship to sequential memory

A

a machine that can, at any point in time, be in a specific state from a finite set of possible states. It can move (transition) to another state by accepting an input. If the machine allows for outputs, it can produce an output.

26
Q

Describe the basic structure and operation of computer memory

A

Structure:

  • Has an address and each address has information(data).
  • Address space: number of locations in power of 2. (number of drawers)
  • Addressability: Number of bits in each location

Operation of memory:
- Read and write

27
Q

Define a transistor, how it functions, and different types

A

Transistor: an electrical device (tiny like a hair pin) that acts as an electrical switch.

Functions: control the flow of current in a circuit.
nMOS: opens when 0 no current is present and closes at 1 (when there is a flow of current).
pMOS: opens when 1 (current flowing) and closes 0 (current is not flowing).

Types: nMOS (Pull Down) and pMOS (Pull UP)