1 / 25

Chapter 11-14 , Appendix D C Programs

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

melva
Télécharger la présentation

Chapter 11-14 , Appendix D C Programs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 <stdio.h> #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. 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 <stdio.h> 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 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” 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 ;<code for calling the function printf> 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 <stdio.h> 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 <stdio.h> 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 ; storeinLocal 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 x0000 Vectors • 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 x0200 Op Sys x3000 instructions PC R4 global data Heap R6 run-time stack R5 xFE00 Device Registers xFFFF

  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 R6 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 R5

  18. Context Frame or Activation Record Format (Stack PTR) R6 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 Called Program Called Program (Frame PTR) R5 Called Program Called Program Calling program “PUSHED” on Stack By:

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

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

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

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

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

  24. Ending the Callee Function 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 R6 R5 return k; ; copy k into return valueLDR R0, R5, #0STR R0, R5, #3; pop local variablesADD R6, R5, #1; pop dynamic link (into R5)LDR R5, R6, #0ADD R6, R6, #1; pop return addr (into R7)LDR R7, R6, #0ADD R6, R6, #1; return control to callerRET new R6 new R5 xFD00

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

More Related