Download presentation
Presentation is loading. Please wait.
1
Chapter 11-12, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3
2
Higher Level Languages High Level Languages give us: Symbolic Names Expressions Libraries of functions/subroutines Abstraction of underlying hardware Readability Structure – help keep bugs out
3
Translating Higher Level Program Interpreters Compilers
4
Compiling C
5
Simple 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 #define STOP 0 /* Function : main */ /* Description : prompt for input, then display countdown */ int main() { /* Variable declarations */ int counter; /* Holds intermediate count values */ int startPoint; /* Starting point for count down */ /* Prompt the user for input */ printf("===== Countdown Program =====\n"); printf("Enter a positive integer: "); scanf("%d", &startPoint); /* Count down from the input number to 0 */ for (counter = startPoint; counter >= STOP; counter--) { printf("%d\n", counter); } return 0 }
6
Terms, Etc. Pre processor directives #define #include Header Files Data Types int char double Scope Local Global Variable Initiation Local – not initialized Global – initialized to 0
7
Scope #include 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 }
8
IMPORTANT Pointers - 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;
9
Precedence Order of Operations See Table 12.5 Page 323
10
Arithmetic Operators SymbolOperationUsage PrecedenceAssoc * multiply x * y 6l-to-r / divide x / y 6l-to-r % modulo x % y 6l-to-r + addition x + y 7l-to-r - subtraction x - y 7l-to-r Evaluate expressions left to right. * / % have higher precedence than + -.
11
Bitwise Operators SymbolOperationUsagePrecedenceAssoc ~ bitwise NOT ~x 4r-to-l << left shift x << y 8l-to-r >> right shift x >> y 8l-to-r & bitwise AND x & y 11l-to-r ^ bitwise XOR x ^ y 12l-to-r | bitwise OR x | y 13l-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.
12
Logical Operators SymbolOperationUsagePrecedenceAssoc ! logical NOT !x 4r-to-l && logical AND x && y 14l-to-r || logical OR x || y 15l-to-r Treats entire variable (or value) as TRUE (non-zero) or FALSE (zero). Result is 1 (TRUE) or 0 (FALSE).
13
Relational Operators SymbolOperationUsagePrecedenceAssoc > greater than x > y 9l-to-r >= greater than or equal x >= y 9l-to-r < less than x < y 9l-to-r <= less than or equal x <= y 9l-to-r == equal x == y 10l-to-r != not equal x != y 10l-to-r Result is 1 (TRUE) or 0 (FALSE). Note: Don't confuse equality (==) with assignment (=).
14
Special Operators: ++ and -- Changes value of variable before (or after) its value is used in an expression. SymbolOperationUsagePrecedenceAssoc ++ postincrement x++ 2r-to-l -- postdecrement x-- 2r-to-l ++ preincrement ++x 3r-to-l - - predecrement --x 3r-to-l Pre: Increment/decrement variable before using its value. Post: Increment/decrement variable after using its value.
15
Special Operators: +=, *=, etc. Arithmetic and bitwise operators can be combined with assignment operator. StatementEquivalent 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 <<= y;x = x << y; x >>= y;x = x >> y; All have same precedence and associativity as = and associate right-to-left.
16
Special Operator: Conditional SymbolOperationUsagePrecedenceAssoc ?: conditional x?y:z 16l-to-r If x is TRUE (non-zero), result is y; else, result is z. Like a MUX, with x as the select signal. x yz 10
17
Example program #include 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); /* Calculate total time in seconds */ time = amount / rate; /* Convert time into hours, minutes, seconds */ hours = time / 3600; /* 3600 seconds in an hour */ minutes = (time % 3600) / 60; /* 60 seconds in a minute */ seconds = ((time % 3600) % 60); /* remainder is seconds */ /* Output results */ printf("Transfer Time : %dh %dm %ds\n", hours, minutes, seconds); return 0 }
18
Symbol 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 0 -3 -4 -5 -2 main
19
Local Variable Storage Local variables are stored in an activation record, also known as a stack frame. Symbol table “offset” gives the distance from the base of the frame. –R5 is the frame pointer – holds address of the base of the current frame. –A new frame is pushed on the run-time stack each time a block is entered. –Because stack grows downward, base is the highest address of the frame, and variable offsets are <= 0. seconds minutes hours time rate amount R5
20
Allocating 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 beginning of storage area –Global: LDR R1, R4, #4 –Local: LDR R2, R5, #-3 instructions global data run-time stack 0x0000 0xFFFF PC R4 R6 R5
21
Another Example /* Include the standard I/O header file */ #include 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); /* Print out results */ printf("outLocalA = %d, outLocalB = %d\n", outLocalA, outLocalB); return 0 }
22
Example: Symbol Table NameTypeOffsetScope inGlobalint0global inLocalint0main outLocalAintmain outLocalBint-2main
23
Example: 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)
24
Example (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)
25
Example (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)
26
Another program #include #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 = pi*r^2 */ circumference = 2 * pi * RADIUS; /* circumference = */ /* 2*pi*r */ printf("Area of a circle with radius %f cm is %f cm^2\n", RADIUS, area); printf("Circumference of the circle is %f cm\n", circumference); return 0 }
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.