Intro to C Flashcards
Machine code
binary files (such as .obj, .exe, a.out)
Assembly code
text files (e.g. .asm)
C code
text files (.c, .h)
Compiler
converts a high-level language (like C) to low-level language (assembly), for example .c to .asm
Assembler
converts assembly language to machine language object files (.asm to .obj)
Linker
combines multiple object files (.obj) into a single executable object file; e.g. user_program.obj + os.obj → my_program.obj
Symbol table
created by a compiler to keep track of where data should be stored, the type of data stored, and scope of the data being stored by the program it compiled
Instead of recording exact addresses for variables, offset is used relative to the starting location (e.g. R5). WHY?
This was function could use any starting place in data memory and build its stack. Fct can be loaded at different places at different times.
Local variables
the “scope” of these variables exists only within the { }.
Variables must be declared at the top of block!
Stack
in the context of the C language, the region in data memory where data required to support functions in the C language will be stored (functions such as
arguments, local variables, and return values). It is organized like a physical stack: imagine piling one book on top of the next one in a stack of books. This is
why it grows “backwards” in memory.
Frame
a logical grouping of the data on the stack that belongs to a single function. Examples of data in the stack frame are arguments, local variables, and return values. As one function calls another, each function will have its own frame.
Frame pointer
a register that holds a starting address for the frame or , in the case of the LC4, a register holding the location of where the calling function’s frame pointer was stored in the active function’s stack frame. Sometimes called
the “base pointer.”
Return address
address in program memory to return after function completes
Prologue
assembly code created by the compiler to construct the frame for any given function. It is the first code executed when a function is called. It is also responsible for “pushing” the stack frame for the active function onto the stack.
Epilogue
assembly code created by the compiler to “pop” the stack from the active function off of the stack. It copies the return value into the proper location
on the stack so that the calling function has access to the returning data (if any) from the active function. It is executed every time a function exits
Prologue Code
Prologue: same for every function; remember this!
;; prologue
STR R7, R6, #-2 ; save caller’s return address
STR R5, R6, #-3 ; save caller’s frame pointer
ADD R6, R6, #-3 ; updates stack pointer
ADD R5, R6, #0 ; creates/updates frame pointer
○ Arguments are pushed onto stack from right to left.
○ This lets the first argument from the left be the one closest to the callee.
○ That is called “C convention” (while left-to-right is called “PASCAL”).
○ It is needed for functions with variable argument counts e.g. printf.
○ It can also lead to the order of evaluation being right-to-left.
■ Example: pow (a, ++a) may become pow (3, 3)
Epilogue Code
Before enter epilogue store return value in R7 ;;epilogue ADD R6, R5, #0 ; pop local variables ADD R6, R6, #3 ; decrease stack STR R7, R6, #-1 ;update return value LDR R5, R6, #-3 ; restore base ptr LDR R7, R6 #-2 ; restore R7 for RET
Big ideas of C
A high-level/low-level language: binds the gap between a machine level language and high-level languages.
- Imperative
- Procedural
- File-oriented
- Portability
Imperative
The imperative is similar to how you do something (step by step) - assignment statements that explicitly change memory values, while declarative is more similar to what you do, describe function
An imperative approach (HOW): “I see that table located under the Gone Fishin’ sign is empty. My husband and I are going to walk over there and sit down.”
A declarative approach (WHAT): “Table for two, please.”
Procedural
Emphasis on the program’s flow rather than the data operated on by functions
File-oriented
Files: a simple abstraction for permanent storage and I/O
Portability
The same C code can be compiled for different ISAs
How are arguments passed in C
● C is pass-by-value (not pass-by-reference)
○ Functions receive “copies” of local variables.
■ Recall that arguments to functions were copies of local vars.
○ Protects local variables from being modified accidentally.
Why must varialbe be declared at the start of function
○ Size of static/automatic variables are known at compile time:
ADD R6, R6, #-1 ; allocate space for local vars
Also, compiler may compile line-by-line, hence right upfront!
How are arrays stored on the stack
Like one giant variable. Address increases with array index
○ The order of arrays on the stack is consistent with the memory address
i.e. in increasing order.
○ Example:
arr[0] - x7FF5
arr[1] - x7FF6
arr[2] - x7FF7