HPC Lecture7
HPC Lecture7
HPC Lecture7
out
% gcc program.c
program.c: File containing program written in
the C programming language
a.out: File containing executable equivalent
program in machine language
2
Steps in gcc These are text files – you can read
or write them with a text editor
program.o
as
ld Library files
e.g., math.o, the math library
These are object files
a.out
(binary) – you can write
programs to read them
3
Steps in gcc
cpp: C pre-processor
Pre-processing of #include, #define, …
Output: an expanded C program
cc1: C compiler
Output: an equivalent assembly language program
Almost like machine language but readable
as: Assembler
Output: an equivalent machine language program
ld: Linkage editor
4
Sample program.c
#include<stdio.h>
#include<math.h>
float a[100];
main() {
int i;
float sum;
for(i=0, sum=0.0; i<100; i++) {
a[i] = sqrt(a[i]);
sum += a[i];
}
printf("sum = %4.2f\n", sum);
}
5
Corresponding program.s
.section .bss, 8, 0x00000003, 0, 8
.bss:
.section .bss
.origin 0x0
.align 0
.globl a
.type a, stt_object
.size a, 400
6
Assembly Representation.
a: # 0x0
.dynsym a sto_default
.space 400
.section .text
8
Example: Function Call and Return
void A() {
Caller
… Parameter
B(5); Function call
… Return address
}
void B (int x) {
Local variables
Callee
int a, b; Return
…
return();
}
9
Example: Function Call and Return.
What must be done on a function call?
10
Example: Function Call and Return..
What must be done on a function call?
11
Example: Function Call and Return…
What must be done on a function call?
12
Aside: What is a Stack?
A data structure; like a stack of books
Operations:
Push: Insert onto top PUSH 47
POP; POP
13
Example: Function Call and Return….
What must be done on a function call?
Pass parameters on stack
Transfer control to start of function
Remember return address
Where? On a stack (in main memory)
Clean up stack
Transfer control back to return address
14
Problem: Separate Compilation
Consider our simple example of compiling a C
program in program.c that calls a math library
function
% gcc program.c
cc1 might use general purpose registers R3-R10 for
the frequently used variables
But, what if these registers are used by the math
function, which was compiled previously?
When the math function is called, the values in R3-
R10 would be over written and therefore lost
Unless we save the values of those registers as part
of the function call
15
Example: Function Call and Return
What must be done on a function call?
Pass parameters on stack
Transfer control to start of function
Remember return address
Where? On a stack (in main memory)
Save register values on stack
Allocate space for local variables on stack
What must be done on a function return?
Pass return value (through stack)
Restore register values from the stack
Clean up stack
Transfer control back to return address
16