Final Exam Flashcards
Describe the “heap” region of computer memory used by the C programming language and its relation to dynamic memory allocation
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.
Describe the basic functions that interact with the heap in the standard C library
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
Discuss memory leaks and other dynamic memory errors
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
Summarize the two basic file types in C: binary and ASCII
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
Discuss the connection between file access (read/write) and I/O in assembly language
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)
Differentiate between a character array and a string in C
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
Describe basic data types in C and how they are related to computer memory
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
Describe arrays in C
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 [ ]
Describe the notion of a pointer in C
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
Describe the connection between pointers and arrays in C
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.
Discuss storage classes in the C programming language
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
Define the high-level language of C
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
Explain how high-level languages are connected to assembly and machine languages
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)
Explain what a compiler is and what it does
Compiler converts a high-level language (like C) to low-level language (assembly), for example .c to .asm
Discuss how high level languages use the “stack” to arrange memory in an organized way
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.
Explain how LC4 C uses a stack and the implications of the stack mechanism
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
Discuss alternatives to using the stack
LC4 uses memory based calling conventions.
Arguments and return values passed via stack
Describe the generic architecture of I/O devices connected to a CPU
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.
Recall and describe the four basic I/O devices on the LC4
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)
Discuss assembly language and its role in a CPU
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.
Define an ISA and its purpose
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.
Describe the purpose and implementation of a program counter in a CPU
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.
Compare sequential logic to combinational logic
Sequential logic: Past and present inputs matter to determine output.
Combinational logic: Past input does not matter for output.
Define the three basic pieces of sequential logic: RS latch, D latch, and D flipflop
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.
Understand finite state machines and their relationship to sequential memory
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.
Describe the basic structure and operation of computer memory
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
Define a transistor, how it functions, and different types
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)