chapter 11-14 , appendix d c programs

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

Upload: melva

Post on 23-Jan-2016

30 views

Category:

Documents


0 download

DESCRIPTION

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!!. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 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!!

Page 2: Chapter 11-14  ,  Appendix D  C Programs

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

}

Page 3: Chapter 11-14  ,  Appendix D  C Programs

Once More: Pointers - IMPORTANTIMPORTANT• 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?

Page 4: Chapter 11-14  ,  Appendix D  C Programs

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}

Page 5: Chapter 11-14  ,  Appendix D  C Programs

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?

Page 6: Chapter 11-14  ,  Appendix D  C Programs

Memory Map

Page 7: Chapter 11-14  ,  Appendix D  C Programs

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.

secondsminuteshourstimerate

amountR5

Page 8: Chapter 11-14  ,  Appendix D  C Programs

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 + 1BR LOOP ;another

iteration

DONE:

#include <stdio.h>

int main(){ int x = 0;

while (x<10) { printf("%d ", x); x = x + 1; }}

Page 9: Chapter 11-14  ,  Appendix D  C Programs

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}

Page 10: Chapter 11-14  ,  Appendix D  C Programs

Symbol Table & Local variable Storage

Page 11: Chapter 11-14  ,  Appendix D  C Programs

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)

Page 12: Chapter 11-14  ,  Appendix D  C Programs

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)

Page 13: Chapter 11-14  ,  Appendix D  C Programs

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)

Page 14: Chapter 11-14  ,  Appendix D  C Programs

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, #-yOffset = distance from beginning of storage area

instructions

global data

run-timestack

Device Registers

x0200

xFFFF

PC

R4

R6R5

x0000

xFE00

Vectors

Op Sys x3000

Heap

Page 15: Chapter 11-14  ,  Appendix D  C Programs

Example C Program with Function Callsint 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;}

Page 16: Chapter 11-14  ,  Appendix D  C Programs

Snapshots of Stack Before, During, and After Function Calls

Page 17: Chapter 11-14  ,  Appendix D  C Programs

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

Page 18: Chapter 11-14  ,  Appendix D  C Programs

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

Called Program

Called Program

Called Program

“PUSHED” on Stack By:

Page 19: Chapter 11-14  ,  Appendix D  C Programs

Stack Snapshot

Can you tell where we are in the program?

Page 20: Chapter 11-14  ,  Appendix D  C Programs

Activation Records on Stack

(Watt’s R5 )

(Main’s R5 )

Volta’s R5

Now where are we?

Page 21: Chapter 11-14  ,  Appendix D  C Programs

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…

Page 22: Chapter 11-14  ,  Appendix D  C Programs

Calling the Function

w = Volta(w, 10); ; push second arg

AND 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

qrwdyn linkret addrret vala

25 1025

xFD00

new R6

Note: Caller needs to know number and type of arguments,doesn't know about local variables.

R5R6

Page 23: Chapter 11-14  ,  Appendix D  C Programs

Starting the Callee Function

; 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

mkdyn linkret addrret valqrwdyn linkret addrret vala

xFCFBx3100

25 1025

xFD00

new R6new R5

R6

R5

Page 24: Chapter 11-14  ,  Appendix D  C Programs

Ending the Callee Function

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

mkdyn linkret addrret valqrwdyn linkret addrret vala

-43217xFCFBx310021725 1025

xFD00

R6R5

new R6

new R5

Page 25: Chapter 11-14  ,  Appendix D  C Programs

Resuming the Caller Function

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

ret valqrwdyn linkret addrret vala

21725 10217

xFD00

R6

R5new R6