//ECE 209 Notes
Building blocks
Program
Maipulate input data to create new data... information
Data = Numbers, Text
Source Code File
-Unit of compilation
Function
-Unit of modularity
Statement
-Unit of work
Expression
Literals
Decimal
Hex
Binary
->
Integer
Float
Ascii Characters
's' single qutoe is a character
->
String
"Double qutoes is a string"
Special Characters
'\n' newline
'\t' tab
'\0' null
->
Booleans
True, False
Floating point literals
1e6
3e-2
1.5e6
String Literals
"First line. \nSecond line"
Variables
All variables have a type,
which will tell you what data
can be stored in it and how it
can be manipulated
int signed_integer = 42;
double floating_point = 42.5;
char single_letter = 'a';
Variables are stored diffrently depending on cpu type
x86 int = 4B, double = 8B, char = 1B
LC3 int = 1 word, double = 2 words, char = 1 word
don't assume zero value on variable creation
Variable name
User defined
Letters, Digits, Underscores
Can't start with a digit
Case sensitive
Variable creation
type, name;
int count;
double sum;
char mode;
Initialization
int count = 0;
double sum = 15.5;
char mode = 'a';
Can declare/init multiple vars of the same type in a declaration
int count = 0, sum = 5, i;
Variable assignment
Change variable value
count = 2;
Copy from other variable of same type
i = count;
Copy from variable of diffrent type
int a = 10;
double x = 3.5;
a = x;
"a" now equals 3
smaller to larger type ok
int -> double -> float
larger to smaller type loses data
float -> double -> int
Scope
Local, global
local variables declared inside a function
global variables declared outside a function
(bad practice)
Operators
Bitwise
AND (&), OR (|), NOT (^), Shift (a>>b, a<<b)
Arithmetic
(<,>,+,-,=,%)
Logical
&&, ||, ==
Math
type in = type out
int9/int5 = 1
modulus % used with only ints
Cast
force data into other type
double a = 5.2;
int c;
c = (int) a / b;
a now has value of 5, trunkates
ScanF
scanf("$d.%d", &dollars, &cents);
value is variable name preceded by $
values separated by decimal
all input types %d, $x, %i except %c will skip leading white space
if scanf sees input that doesnt match it ends
fn: scanf("%d", &var)
input: "400\nabc\n123"
result: var = 400
fn: scanf("%d%d", &a, &b)
input: 1234/n
result: a = 1234 //wait for more input
fn: scanf("%d%d", &a, &b)
input: 1234/n5678
result: a = 1234, b = 5672
fn: scanf("%d%d", &a, &b)
input: 1234x
result: a = 1234, b = (exit)
scanf leaves unusable values in the input stream, this can effect subsequent
scanfs
sizeof(double, int, float)
tells you the size of a value on current platform (not neccecarry most of the time)
int a = 10, b = 10;
scanf("%d%d", &a, &b);
printf("%d%d\n", a , b);
return 0;
Statement in an expression followed by ;
Compound Statement
collects multiple statements into one
If statements
if (condition) {
//statement1;
else
//statement2;
else if(statement){
//statement3;
}
}
Translating C code to LC3 instructions
all C instructions must be completed using only ADD AND and NOT
Only use R0 - R3 for temporary values, R4 - R7 hold program information
Use LDR to read variable values, R5 - variable offset
Variables have no labels, don't use LD, LDI, ST, STI, only the register offset functions
LDR STR
Variables are declared going down, stack offset goes into negitives from R5
STR R0, R5, #-3
Dont need to clear registers before assigning values, new value will overwrite old