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