Program Compilation and Execution - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Program Compilation and Execution

Description:

Draw block diagram showing order of linker, compiler, assembler, OS, macro preprocessor, loader ... Parses flags and arguments. Invokes gcc script. Performs ... – PowerPoint PPT presentation

Number of Views:152
Avg rating:3.0/5.0
Slides: 19
Provided by: cse54
Category:

less

Transcript and Presenter's Notes

Title: Program Compilation and Execution


1
Program Compilation and Execution

2
Todays Topics
  • Breakdown of CC script
  • Memory Usage during execution

3
Todays Objectives
  • Within context of compiling C program
  • Define actions of linker, compiler, assembler,
    OS, macro preprocessor, loader
  • Draw block diagram showing order of linker,
    compiler, assembler, OS, macro preprocessor,
    loader
  • Explain why runtime stack needed for C
  • Draw logical division of memory into stack, heap
  • Compare and contrast stack and heap
  • For C program, indicate which variables are
    stored in heap in stack in neither

4
Logical Memory Layout
Stack
D Y N A M I C
Currently Not in Use
Not Currently in Use
Heap
Static Space
5
Breaking Down CC
Preprocessed Source
source
ASM
Compiler
Assembler
Preprocessor
.s
O B J E C T
Executable Program In Memory
Executable
Linker
Loader
.o
a.out
6
Breaking Down CC
Preprocessed Source
source
ASM
Compiler
Assembler
Preprocessor
.s
O B J E C T
Executable Program In Memory
Executable
Linker
Loader
.o
a.out
7
C Preprocessor (cpp)
  • Pass over source
  • Insert included files
  • Perform macro substitutions
  • Define macros
  • define NUM 100
  • define xx(v,name,op,metrics) \
    vxxinit(op,name,IR-gtmetrics)
  • gcc E example.c sends preprocessor output to
    stdout

8
Breaking Down CC
Preprocessed Source
source
ASM
Compiler
Assembler
Preprocessor
.s
O B J E C T
Executable Program In Memory
Executable
Linker
Loader
.o
a.out
9
Compiler
  • gcc actually name of a script
  • Compiler translates one language to another (or
    the same???)
  • gcc compiler translates C to assembler
  • gcc S example.c saves example.s
  • Compiler consists of
  • Parser
  • Code generation
  • Mysticism

10
Breaking Down CC
Preprocessed Source
source
ASM
Compiler
Assembler
Preprocessor
.s
O B J E C T
Executable Program In Memory
Executable
Linker
Loader
.o
a.out
11
Assembler
  • Another translator ??? (as example.s)
  • Assembler to (binary) object
  • Why not compile straight to binary?
  • gcc c example.c to save object
  • NM to look at object (nm example.o)

12
Breaking Down CC
Preprocessed Source
source
ASM
Compiler
Assembler
Preprocessor
.s
O B J E C T
Executable Program In Memory
Executable
Linker
Loader
.o
a.out
13
Linker
  • Combine objects, both user .o files and
    libraries, make an executable
  • gcc .o lm yields a.out
  • gcc o myExec .o lm
  • Use nm to look at object

14
Breaking Down CC
Preprocessed Source
source
ASM
Compiler
Assembler
Preprocessor
.s
O B J E C T
Executable Program In Memory
Executable
Linker
Loader
.o
a.out
15
Loader
  • Gets an address to place program
  • Changes necessary addresses (if any)
  • Places code into memory

16
Operating System
  • Oversees whole process
  • Recognizes gcc example.c command
  • Parses flags and arguments
  • Invokes gcc script
  • Performs memory mangagement (malloc)
  • Chooses address to place program

17
Dice.c
  • int NUM 100000
  • main()
  • int i, roll, counts
  • counts (int) malloc (13 sizeof(int))
  • for (i 0 i lt 13 i) countsi 0
  • for ( i 0 i lt NUM i)
  • roll rand() 6 rand() 6 2
  • countsroll
  • for (i 2 i lt 13 i)
  • printf(There were d rolls of d/n,
  • countsi,i)

18
Where Are the Variables?
Stack
D Y N A M I C
i roll counts
Currently Not in Use
Not Currently in Use
Space for 13 ints pointed to by counts ?
Heap
Static Space
Code d throws x\n
Write a Comment
User Comments (0)
About PowerShow.com