Intro to C Flashcards

1
Q

Machine code

A

binary files (such as .obj, .exe, a.out)

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

Assembly code

A

text files (e.g. .asm)

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

C code

A

text files (.c, .h)

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

Compiler

A

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
5
Q

Assembler

A

converts assembly language to machine language object files (.asm to .obj)

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

Linker

A

combines multiple object files (.obj) into a single executable object file; e.g. user_program.obj + os.obj → my_program.obj

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

Symbol table

A

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.

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

Local variables

A

the “scope” of these variables exists only within the { }.

Variables must be declared at the top of block!

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

Stack

A

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.

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

Frame

A

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.

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

Frame pointer

A

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.”

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

Return address

A

address in program memory to return after function completes

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

Prologue

A

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.

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

Epilogue

A

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

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

Prologue Code

A

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)

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

Epilogue Code

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Big ideas of C

A

A high-level/low-level language: binds the gap between a machine level language and high-level languages.

  • Imperative
  • Procedural
  • File-oriented
  • Portability
18
Q

Imperative

A

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.”

19
Q

Procedural

A

Emphasis on the program’s flow rather than the data operated on by functions

20
Q

File-oriented

A

Files: a simple abstraction for permanent storage and I/O

21
Q

Portability

A

The same C code can be compiled for different ISAs

22
Q

How are arguments passed in C

A

● 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.

23
Q

Why must varialbe be declared at the start of function

A

○ 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!

24
Q

How are arrays stored on the stack

A

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

25
Q

What is a hashtable in C

A
An associative array where the incoming data is mapped to the proper storage location within the array. 
An array (bucket holder) of linked lists with a hashing function to determine where data should fit.
26
Q

From c to obj

A

C -> compile -> .asm

.asm -> assemble -> .obj/machine code

27
Q

Assembly code is not machine specific

A

WRONG

28
Q

Main

A
  • All C programs must start in main
  • Recall C is procedural , I. e. everything is in a function
  • Note that this gets converted to a label in assembly
29
Q

Local Variables

A

Exist only in code block they are defined in
Are NOT initialized to 0 in C
Variables MUST be declared at the top of block. Because when assembling first thing done after prologue is create variables on stack

30
Q

Where in memory do functions build their stack

A

Data memory, it will hold local variables, return values, return address, base pointer and arguments

31
Q

Memory split in LC4 of Data memory

A
  1. Program Memory up to x1FFF
  2. Global Variables up to x3FFF
  3. Dynamic Storage - heap up to x6FFF
  4. Local Variables (STACK) up to x7FFF
32
Q

Structure Frame

A
  1. temporaries, arguments to callees
  2. local variables
  3. Callers frame Pointer
  4. Return Address
  5. Return Value
  6. Arguments to function

1-4 addressed by R5 others by R6

33
Q

Adding 2 Local Variables to stack after prologue complete

A
ADD R6 R6 #-2
CONST R7 #2
STR R7 R5 #-1
CONST R7 #3
STR R7 R5 #-2

->arguments added incremental

34
Q

Where do R5 and R6 point to

A
R5 = "bottom"of the frame, entry, callers frame pointer
R6 = top of stack
35
Q

Adding Arguments to stack

A
One by one, right to left
e.g Pow(2,3)
LDR R7 R5 #-2
ADD R6, R6 #-1
STR R7, R6 #0
LDR R7 R5 #-1
ADD R6, R6 #-1
STR R7, R6 #0
36
Q

Why is C pass-by-value

A

Protects local variables from being modified accidentally

37
Q

How does calling main work in LC4

A

The LC4’s OS (Operating System) that we’ve written calls a function called USER_START, that puts the LC4 in user mode. USER_START then calls the function: main(), it must be called main() as it is hard coded in USER_START

.CODE
.ADDR x8200
.FALIGN
  ;; first job of OS is to return PennSim to x0000 & downgrade privledge
  CONST R7, #0   ; R7 = 0
  RTI            ; PC = R7 ; PSR[15]=0
38
Q

Explain Linking

A

It is useful if you write programs that have many separate files that depend on one another

It creates a pointer from one file to another.

“links” the files together as if they were one long .OBJ file.

39
Q

Function Definition vs Function Declaration

A

A function definition provides the actual body of the function

A function declaration tells the compiler about a function’s name, return type, and parameters.

40
Q

Where is #define PI stored

A

Nowhere, nno space allocated as it is a directive and not a available. When compiling, compiler will substitute PI in code with value

41
Q

The name of the array IS a memory address. The pointer IS a memory address. The array itself does NOT contain memory addresses

CORRECT?

A

NO, pointer IS not a memory address but rather contains a memory address