1 / 19

Chapter 13, 14 Overview

Chapter 13, 14 Overview. C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation. Recall: Example program. #include <stdio.h> int main() { /* Declare local variables */

amara
Télécharger la présentation

Chapter 13, 14 Overview

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 13, 14 Overview • C programming Environment • C Global Variables • C Local Variables • Memory Map for a C Function • C Activation Records • Example Compilation

  2. Recall: Example program #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 }

  3. 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 Type Name 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

  4. Local 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 <= 0. seconds minutes hours time rate amount R5

  5. Example: “While” Program AND R0, R0, #0 ; clear out R0 STR R0, R5, #0 ; x = 0 ; while (x<10) LOOP: LDR R0, R5, #0 ; 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; } }

  6. Allocating Space for Variables x0000 Vectors x0200 Op Sys • 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, #x • Local: LDR R2, R5, #-y x3000 R6 run-time stack R5 PC instructions R4 global data xFE00 Device Registers xFFFF

  7. Some Activation Record Bookkeeping • Return value • space for value returned by function • allocated even if function does not return a value • Return address • save pointer to next instruction in calling function • convenient location to store R7 in case another function (JSR)is called • Dynamic link • caller’s frame pointer • used to pop this activation record from stack • What else must be in the Activation Record or Context Frame?

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

  9. Example 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; }

  10. Memory Snapshots

  11. Storage

  12. Activation Records

  13. 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 vars, pops R5, pops R7. • Callee returns (RET or JMP R7). • Caller loads return value and pops arguments. • Caller resumes computation…

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

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

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

  17. Resuming the Caller Function ret val q r w dyn link ret addr ret val a 217 25 10 217 • w = Volta(w,10); • JSR Volta; 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

  18. Allocating Space for Variables x0000 Vectors x0200 Op Sys • 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, #x • Local: LDR R2, R5, #-y x3000 R6 run-time stack R5 PC instructions R4 global data xFE00 Device Registers xFFFF

  19. Activation Record Format R6 Function stacked stuff …….. …….. Local Variables Caller’s Frame Pointer (R5) Caller’s Return PC (R7) Function Return Value Function Pass Value n …….. Function Pass Value 1 R5

More Related