Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 11-14, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3 Please return breadboards.

Similar presentations


Presentation on theme: "Chapter 11-14, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3 Please return breadboards."— Presentation transcript:

1 Chapter 11-14, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3 Please return breadboards by next Wednesday: Put usable wires in box Remove chips using chip puller Put parts back in their proper bins Thanks!!

2 C 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 }

3 IMPORTANT Once More: 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 variable''. To declare a pointer to a variable: int *pointer; Note: ip = ip + 1 actually increments ip by 4. Why?

4 First Example program Again #include int main() { /* Declare local variables */ 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 }

5 Symbol Table Like assembler, compiler needs to know information associated with identifiers – in assembler, all identifiers were labels and information is address Compiler keeps more information - Name (identifier) - Type - Location in memory - Scope Where are local variables stored? Why?

6 Memory Map

7 Local Variable Storage Local variables are stored in an stack frame. (also known as a activation record) 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” or scope 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

8 Example: “While” Program AND R0, R0, #0 ; clear out R0 STR R0, R5, #0 ; x = 0 ; while (x<10) LOOP:LDR R0, R5, # ; perform the test ADD R0, R0, #-10 BRpz DONE ; loop body ; LDR R0, R5, #0 ; R0 <= x ADD R0, R0, #1 ; x + 1 STR R0, R5, #0 ; x = x + 1 BR LOOP ;another iteration DONE: #include int main() { int x = 0; while (x<10) { printf("%d ", x); x = x + 1; }

9 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 }

10 Symbol Table & Local variable Storage

11 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)

12 Example (continued) ; first statement: ; outLocalA = inLocal++ & ~inGlobal; LDR R0, R5, #0 ; get inLocal ADD R1, R0, #1 ; inLocal++ STR R1, R5, #0 ; store inLocal 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)

13 Example (continued) ; next statement: ; outLocalB = (inLocal + inGlobal) ; - (inLocal - inGlobal); LDR R0, R5, #0 ; inLocal LDR R1, R4, #0 ; inGlocal ADD R0, R0, R1 ; R0 is inLocal + inGlobal LDR R2, R5, #0 ; inLocal LDR R3, R4, #0 ; inGlobal NOT R3, R3 ADD R3, R3, #1 ; - inGlobal ADD R2, R2, R3 ; inLocal - inGlobal NOT R2, R2 ; negate ADD R2, R2, #1 ; - (inLocal – inGlobal) ADD R0, R0, R2 ; outLocalB STR R0, R5, #-2 ; store outLocalB (offset = -2)

14 Allocating Space for Variables Global data section –All global variables stored here (actually all static variables) –R4 points to Global Variables Run-time stack –Used for local variables (among other things) –R6 points to top of stack –R5 points to top frame on stack –New frame created for each “block” or scope (goes away when block exited) Accessing a variable: –Global: LDR R1, R4, #x –Local: LDR R2, R5, #-y Offset = distance from beginning of storage area instructions global data run-time stack Device Registers x0200 xFFFF PC R4 R6 R5 x0000 xFE00 Vectors Op Sys x3000 Heap

15 Example C Program with Function Calls int main () { int a = 23; int b = 14;... b = Watt(a); /* main calls both */ b = Volta(a,b);... } int Watt(int c); { int w = 5;... w = Volta(w,10); /* Watt calls Volta */... return w; } int Volta(int q, int r) { int k = 3; int m = 6;... /* Volta calls no one */ return k+m; }

16 Snapshots of Stack Before, During, and After Function Calls

17 Context Frame or Activation Record Format Function stacked stuff …….. Local Variables Caller’s Frame Pointer (R5) Caller’s R7 (contains ITS caller’s PC) Function Return Value Function Pass Value 1 …….. Function Pass Value n R6 R5

18 Context Frame or Activation Record Format Function stacked stuff …….. Local Variables Caller’s Frame Pointer (R5) Caller’s R7 (contains ITS caller’s PC) Function Return Value Function Pass Value 1 …….. Function Pass Value n R6 R5 (Stack PTR) (Frame PTR) Calling program Called Program “PUSHED” on Stack By:

19 Stack Snapshot Can you tell where we are in the program?

20 Activation Records on Stack (Watt’s R5  ) (Main’s R5  ) Volta’s R5 Now where are we?

21 Summary of LC-3 Function Call Implementation 1.Caller pushes arguments (last to first). 2.Caller invokes subroutine (JSR). 3.Callee allocates return value, pushes R7 and R5. 4.Callee allocates space for local variables. 5.Callee executes function code. 6.Callee stores result into return value slot. 7.Callee pops local variables, pops R5, pops R7. 8.Callee returns RET (or JMP R7). 9.Caller loads return value and pops arguments. 10.Caller resumes computation…

22 Calling the Function w = Volta(w, 10); ; push second arg AND R0, R0, #0 ADD R0, R0, #10 ADD R6, R6, #-1 STR R0, R6, #0 ; push first argument LDR R0, R5, #0 ADD R6, R6, #-1 STR R0, R6, #0 ; call subroutine JSR Volta q r w dyn link ret addr ret val a 25 10 25 xFD00 new R6 Note: Caller needs to know number and type of arguments, doesn't know about local variables. R5 R6

23 Starting the Callee Function ; leave space for return value ADD R6, R6, #-1 ; push return address ADD R6, R6, #-1 STR R7, R6, #0 ; push dyn link (caller’s frame ptr) ADD R6, R6, #-1 STR R5, R6, #0 ; set new frame pointer ADD R5, R6, #-1 ; allocate space for locals ADD R6, R6, #-2 m k dyn link ret addr ret val q r w dyn link ret addr ret val a xFCFB x3100 25 10 25 xFD00 new R6 new R5 R6 R5

24 Ending the Callee Function return k; ; copy k into return value LDR R0, R5, #0 STR R0, R5, #3 ; pop local variables ADD R6, R5, #1 ; pop dynamic link (into R5) LDR R5, R6, #0 ADD R6, R6, #1 ; pop return addr (into R7) LDR R7, R6, #0 ADD R6, R6, #1 ; return control to caller RET m k dyn link ret addr ret val q r w dyn link ret addr ret val a -43 217 xFCFB x3100 217 25 10 25 xFD00 R6 R5 new R6 new R5

25 Resuming the Caller Function w = Volta(w,10); ; load return value (top of stack) LDR R0, R6, #0 ; perform assignment STR R0, R5, #0 ; pop return value ADD R6, R6, #1 ; pop arguments ADD R6, R6, #2 ret val q r w dyn link ret addr ret val a 217 25 10 217 xFD00 R6 R5 new R6


Download ppt "Chapter 11-14, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3 Please return breadboards."

Similar presentations


Ads by Google