Quelltextmain.c add.c mul.c
1 Präprozessorgcc -E
2 Compilergcc -S → .s
3 Assemblergcc -c → .o
4 Linker-o exec
Ausführen./exec
main.c
#include <stdio.h>
#include "add.h"
#include "mul.h"
int main(void) {
printf("3 + 4 = %d\n", add(3, 4));
printf("3 * 4 = %d\n", mul(3, 4));
return 0;
}
add.hHeader
int add(int a, int b);
mul.hHeader
int mul(int a, int b);
add.c
#include "add.h"
int add(int a, int b) {
return a + b; }
mul.c
#include "mul.h"
int mul(int a, int b) {
return a * b; }
5 Textdateienint main(… gcc exec01001000 10001001… CPU
Ziel: eine ausführbare Datei aus Nullen und Einsen, mit Befehlen, die die CPU (das „Gehirn“ des Computers) direkt ausführen kann.
main.c10 Zeilen
// Ein kleiner Rechner
#include <stdio.h>570 Zeilen aus stdio.h
typedef signed char __int8_t;
typedef long long __int64_t;
char __mbstate8[128];
typedef int __darwin_wchar_t;
typedef long __darwin_time_t;
typedef __uint32_t __darwin_gid_t;
typedef __uint16_t __darwin_mode_t;
typedef __uint32_t __darwin_useconds_t;
struct __darwin_pthread_handler_rec *__n…
long __sig;
};
long __sig;
};
long __sig;
};
typedef struct _opaque_pthread_mutexattr_…
typedef int __darwin_wctrans_t;
typedef unsigned char u_int8_t;
typedef unsigned long uintptr_t;
typedef int64_t user_off_t;
int renameatx_np(int, const char *, int, …
};
short _flags;
int (* _Nullable _read) (void *, char *,…
unsigned char _ubuf[3];
extern FILE *__stdoutp __attribute__((__s…
int fflush(FILE *);
int fputc(int, FILE *);
int fseek(FILE *, long, int);
__attribute__((__deprecated__("This funct…
int rename (const char *__old, const char…
__attribute__((__deprecated__("This funct…
char * tmpnam(char *);
int vsprintf(char * restrict , const char…
int __srget(FILE *);
return (__swbuf(_c, _p));
int getchar_unlocked(void);
__attribute__((__deprecated__("This funct…
int vfscanf(FILE * restrict __stream, con…
int vdprintf(int, const char * restrict, …
extern const char *const sys_errlist[];
int setlinebuf(FILE *);
int (* _Nullable)(void *));
const char * restrict, ...);
extern int __vsprintf_chk (char * restric…
const char * restrict, va_list);
#include "add.h"int add(int a, int b);
#include "mul.h"int mul(int a, int b);
int main(void) {
printf("3 + 4 = %d\n", add(3, 4));
printf("3 * 4 = %d\n", mul(3, 4));
return 0;
}
darüber: 570 Zeilen aus stdio.h unser Code
stdio.h+ viele weitere Header
typedef signed char __int8_t;
typedef struct __sFILE { … } FILE;
int printf(const char *, ...);
int scanf(const char *, ...);
FILE *fopen(const char *, …);
add.hHeader
int add(int a, int b);
mul.hHeader
int mul(int a, int b);
int printf(…); + 570 Zeilen
int add(int a, int b);
int mul(int a, int b);
main.cnach dem Präprozessor
…
int main(void) {
printf("3 + 4 = %d\n", add(3, 4));
printf("3 * 4 = %d\n", mul(3, 4));
return 0;
}
Compiler
main.sAssembler
main:
mov edi, 3
mov esi, 4
call add
…
call printf
…
call mul
…
main.c
int main(void) {
printf(…, add(3, 4));
Compiler
main.sAssembler
main:
mov edi, 3 … call add …
add.c
int add(int a, int b) {
return a + b; }
Compiler
add.sAssembler
add:
push rbp … add eax, edx … ret
mul.c
int mul(int a, int b) {
return a * b; }
Compiler
mul.sAssembler
mul:
push rbp … imul eax, … ret
main.s
call add …
call printf … call mul
Assembler
main.oMaschinencode
01010101 01001000 10001001 11100101
… 10111111 00000011 … 11101000 …
add.s
add:
… add eax, edx … ret
Assembler
add.oMaschinencode
01010101 01001000 10001001 11100101
… 00000001 11010000 … 11000011
mul.s
mul:
… imul eax, … ret
Assembler
mul.oMaschinencode
01010101 01001000 10001001 11100101
… 00001111 10101111 … 11000011
Achtung: nicht nur 0 und 1, auch Namen wie add und printf
execnoch leer
Linkersetzt alles zusammen
main.o01010101 10111111 …
add.o01010101 01001000 …
mul.o01010101 01001000 …
C-Bibliothekprintf und mehr
execein Programm
01010101 01001000
10001001 11100101
10111111 00000011
main.cadd.cmul.cprintf
CPU
Terminal