Title: Chapter 11-12 , Appendix D C Programs
1Chapter 11-12 , Appendix D C Programs
- Higher Level languages
- Compilers
- C programming
- Converting C to Machine Code
- C Compiler for LC-3
2Higher Level Languages
- High Level Languages give us
- Symbolic Names
- Expressions
- Libraries of functions/subroutines
- Abstraction of underlying hardware
- Readability
- Structure help keep bugs out
3Translating Higher Level Program
4Compiling C
5Simple Example C Program
- /
- Program Name countdown, our first C program
-
- Description This program prompts the user
to type in - a positive number and counts down from that
number to 0, - displaying each number along the way.
- /
- / The next two lines are preprocessor directives
/ - include ltstdio.hgt
- define STOP 0
- / Function main
/ - / Description prompt for input, then display
countdown / - int main()
-
- / Variable declarations /
6Terms, Etc.
- Pre processor directives
- define
- include
- Header Files
- ltstdio.hgt
- Data Types
- int
- char
- double
- Scope
- Local
- Global
7Scope
- include ltstdio.hgt
- int globalVar 2 / This variable is
global / - int main()
-
- int localVar 3 / This variable is local
to main / -
- printf("Global d Local d\n", globalVar,
localVar) -
-
- int localVar 4 / Local to this
sub-block / -
- printf("Global d Local d\n", globalVar,
localVar) -
-
- printf("Global d Local d\n", globalVar,
localVar) - return 0
8Pointers - IMPORTANT
- A pointer is a variable which contains the
address in memory of another variable. - We can have a pointer to any variable type.
- The unary or monadic operator gives the
address of a variable''. - The indirection or dereference operator gives
the contents of an object pointed - to by a pointer''.
- To declare a pointer to a variable do
- int pointer
9Precedence Order of Operations
10Arithmetic Operators
- Symbol Operation Usage Precedence Assoc
- multiply x y 6 l-to-r
- / divide x / y 6 l-to-r
- modulo x y 6 l-to-r
- addition x y 7 l-to-r
- - subtraction x - y 7 l-to-r
- Evaluate expressions left to right.
- / have higher precedence than -.
11Bitwise Operators
- Symbol Operation Usage Precedence Assoc
- bitwise NOT x 4 r-to-l
- ltlt left shift x ltlt y 8 l-to-r
- gtgt right shift x gtgt y 8 l-to-r
- bitwise AND x y 11 l-to-r
- bitwise XOR x y 12 l-to-r
- bitwise OR x y 13 l-to-r
- Operate on variables bit-by-bit.
- Like LC-3 AND and NOT instructions.
- Shift operations are logical (not arithmetic).
Operate on values -- neither operand is
changed.
12Logical Operators
- Symbol Operation Usage Precedence Assoc
- ! logical NOT !x 4 r-to-l
- logical AND x y 14 l-to-r
- logical OR x y 15 l-to-r
- Treats entire variable (or value) as TRUE
(non-zero) or FALSE (zero). - Result is 1 (TRUE) or 0 (FALSE).
13Relational Operators
- Symbol Operation Usage Precedence Assoc
- gt greater than x gt y 9 l-to-r
- gt greater than or equal x gt y 9 l-to-r
- lt less than x lt y 9 l-to-r
- lt less than or equal x lt y 9 l-to-r
- equal x y 10 l-to-r
- ! not equal x ! y 10 l-to-r
- Result is 1 (TRUE) or 0 (FALSE).
- Note Don't confuse equality () with
assignment ().
14Special Operators and --
- Changes value of variable before (or after) its
value is used in an expression. - Symbol Operation Usage Precedence Assoc
- postincrement x 2 r-to-l
- -- postdecrement x-- 2 r-to-l
- preincrement x 3 r-to-l
- - - predecrement --x 3 r-to-l
- Pre Increment/decrement variable before using
its value. - Post Increment/decrement variable after using
its value.
15Special Operators , , etc.
- Arithmetic and bitwise operators can be
combinedwith assignment operator. - Statement Equivalent assignment
- x y x x y
- x - y x x - y
- x y x x y
- x / y x x / y
- x y x x y
- x y x x y
- x y x x y
- x y x x y
- x ltlt y x x ltlt y
- x gtgt y x x gtgt y
All have sameprecedence and associativity as
and associate right-to-left.
16Special Operator Conditional
- Symbol Operation Usage Precedence Assoc
- ? conditional x?yz 16 l-to-r
- If x is TRUE (non-zero), result is y else,
result is z. - Like a MUX, with x as the select signal.
y
z
1
0
x
17Example program
- include ltstdio.hgt
- int main()
-
- int amount / The number of bytes to be
transferred / - int rate / The average network transfer
rate / - int time / The time, in seconds, for the
transfer / -
- int hours / The number of hours for the
transfer / - int minutes / The number of mins for the
transfer / - int seconds / The number of secs for the
transfer / - / Get input number of bytes and network
transfer rate / - printf("How many bytes of data to be
transferred? ") - scanf("d", amount)
-
- printf("What is the transfer rate (in
bytes/sec)? ") - scanf("d", rate)
18Symbol Table
- Like assembler, compiler needs to know
information associated with identifier - Compiler keeps more information
- Name (identifier)
- Type
- Location in memory
- Scope
Name
Type
Offset
Scope
amount hours minutes rate seconds time
int int int int int int
0 -3 -4 -1 -5 -2
main main main main main main
19Local Variable Storage
- Local variables are stored in an activation
record, also known as a stack frame. - Symbol table offset gives thedistance from the
base of the frame. - R5 is the frame pointer holds addressof the
base of the current frame. - A new frame is pushed on therun-time stack each
time a block is entered. - Because stack grows downward,base is the highest
address of the frame,and variable offsets are lt
0.
seconds minutes hours time rate amount
R5
20Allocating Space for Variables
- Global data section
- All global variables stored here(actually all
static variables) - R4 points to beginning
- Run-time stack
- Used for local variables
- R6 points to top of stack
- R5 points to top frame on stack
- New frame for each block(goes away when block
exited) - Offset distance from beginningof storage area
- Global LDR R1, R4, 4
- Local LDR R2, R5, -3
0x0000
instructions
PC
R4
global data
R6
run-time stack
R5
0xFFFF
21Another Example
- / Include the standard I/O header file /
- include ltstdio.hgt
- int inGlobal / inGlobal is a global variable
because / - / it is declared outside of all
blocks / -
- int main()
-
- int inLocal / inLocal, outLocalA,
outLocalB are all / - int outLocalA / local to main
/ - int outLocalB
-
- / Initialize /
- inLocal 5
- inGlobal 3
-
- / Perform calculations /
- outLocalA inLocal inGlobal
- outLocalB (inLocal inGlobal) - (inLocal -
inGlobal)
22Example Symbol Table
Name Type Offset Scope
inGlobal int 0 global
inLocal int 0 main
outLocalA int -1 main
outLocalB int -2 main
23Example Code Generation
- main
- initialize variables
- AND R0, R0, 0 ADD R0, R0, 5
inLocal 5 STR R0, R5, 0 (offset
0) AND R0, R0, 0 ADD R0, R0, 3
inGlobal 3 STR R0, R4, 0 (offset 0)
24Example (continued)
- first statement
- outLocalA inLocal inGlobal
- LDR R0, R5, 0 get inLocal ADD
R1, R0, 1 increment STR R1, R5, 0
store LDR R1, R4, 0 get inGlobal
NOT R1, R1 inGlobal AND R2, R0, R1
inLocal inGlobal STR R2, R5, -1
store in outLocalA
(offset -1)
25Example (continued)
- next statement
- outLocalB (inLocal inGlobal)
- (inLocal - inGlobal) - LDR R0, R5, 0 inLocal LDR R1, R4,
0 inGlobal ADD R0, R0, R1 R0 is sum
LDR R2, R5, 0 inLocal LDR R3, R4,
0 inGlobal NOT R3, R3 ADD R3, R3,
1 ADD R2, R2, R3 R2 is difference
NOT R2, R2 negate ADD R2, R2, 1
ADD R0, R0, R2 R0 R0 - R2 STR R0,
R5, -2 outLocalB (offset -2)
26Another program
- include ltstdio.hgt
- define RADIUS 15.0 / This value is in
centimeters / - int main()
-
- const double pi 3.14159
- double area
- double circumference
-
- / Calculations /
- area pi RADIUS RADIUS / area
pir2 / -
- circumference 2 pi RADIUS /
circumference / - /
2pir / -
- printf("Area of a circle with radius f cm is
f cm2\n", - RADIUS, area)
-
- printf("Circumference of the circle is f
cm\n",