Laboratorium Dasar Teknik Elektro - Sekolah Teknik Elektro dan Informatika ITB
Abstrak
Buat program kode seperti pada
modul. Simpan file dengna
code.c.
1.
PENDAHULUAN
stack
Buka file code.o dan
code.exe pada teks editor.
Bandingkan hasilnya.
Tugas 1 : Proses
Menggunakan GCC
Kompilasi
Bahasa
Klik batch.bat.
Jalankan prgram
main_text.exe.
2.
Simpan dengan nama add.c.
LANDASAN TEORITIS
2.1
Preprocessor
Contohnya,
pada
perintah
#define
MAX_ROWS
10,
pre-processor
akan
mengganti semua kata MAX_ROWS dengan 10.
Pada perintah #include <stdio.h>, preprocessor akan mengganti baris tersebut
dengan isi file stdio.h.
Compiler
Compiler akan menerjemahkan bahasa tingkat
tinggi C menjadi kode assembly. Kode assembly
ini berisi instruksi-instruksi yang sesuai
dengan instruction set yang dimiliki oleh mesin.
File yang dihasilkan pada tahap ini masih
berupa file teks (.s).
Assembler
Assembler akan menerjemahkan bahasa
assembly menjadifile objek. File objek ini
merupakan file biner (.o).
Linker
Linker akan menggabungkan file biner yang
diperoleh pada tahap sebelumnya dengan file
biner lain yang merupakan dependencydari
program yang dibuat, contohnya library untuk
menjalankan fungsi printf. Hasil dari linker
berupa file biner executable(dalam platform
Microsoft Windows, file ini memiliki
akhiran .exe).
2.2
2.3
2.4
2.5
2.6
3.
3.1
3.2
3.4
Diperoleh beberapa file assembly untuk masingmasing optimisasi kompilasi sebagai berikut.
code_O0.o:
3.3
00000000 <_main>:
0: 55
1: 89 e5
%esp,%ebp
3: 83 e4 f0
$0xfffffff0,%esp
6: 83 ec 10
$0x10,%esp
9: e8 00 00 00
<_main+0xe>
e: c7 44 24 0c
$0x0,0xc(%esp)
15: 00
16: c7 44 24 08
$0x0,0x8(%esp)
1d: 00
1e: eb 0c
<_main+0x2c>
20: 8b 44 24 0c
0xc(%esp),%eax
24: 01 44 24 08
%eax,0x8(%esp)
28: ff 44 24 0c
0xc(%esp)
2c: 81 7c 24 0c
$0x1f3,0xc(%esp)
33: 00
34: 7e ea
<_main+0x20>
36: 8b 44 24 08
0x8(%esp),%eax
3a: c9
3b: c3
push
mov
%ebp
and
sub
00
call
00 00 00
movl
00 00 00
movl
jmp
2c
mov
add
incl
f3 01 00
cmpl
jle
20
mov
leave
ret
code_O3.o:
code_O1.o:
push
mov
%ebp
and
00
call
00
mov
inc
cmp
%eax
00
jne
10
00
leave
ret
nop
%ebp
and
call
mov
leave
ret
nop
nop
code_Ofast.o:
%ebp
and
call
00000000 <_main>:
0: 55
1: 89 e5
%esp,%ebp
3: 83 e4 f0
$0xfffffff0,%esp
6: e8 00 00 00 00
<_main+0xb>
b: b8 4e e7 01 00
$0x1e74e,%eax
10: c9
11: c3
12: 90
13: 90
push
mov
%ebp
and
call
mov
leave
ret
nop
nop
mov
leave
ret
nop
nop
00000000 <_main>:
0: 55
1: 89 e5
%esp,%ebp
3: 83 e4 f0
$0xfffffff0,%esp
6: e8 00 00 00 00
<_main+0xb>
b: b8 4e e7 01 00
$0x1e74e,%eax
10: c9
11: c3
12: 90
13: 90
push
mov
mov
code_O2.o:
00000000 <_main>:
0: 55
1: 89 e5
%esp,%ebp
3: 83 e4 f0
$0xfffffff0,%esp
6: e8 00 00 00 00
<_main+0xb>
b: b8 4e e7 01 00
$0x1e74e,%eax
10: c9
11: c3
12: 90
13: 90
code_Os.o:
push
mov
%ebp
and
call
mov
leave
ret
nop
nop
3.5
3.7
3.8
3.6
1
0
4.
KESIMPULAN
3.9
DAFTAR PUSTAKA
[1]
[2]
[3]
11
%~d0
cd "%~dp0"
gcc -O0 -c code_O0.c
gcc -O1 -c code_O1.c
gcc -O2 -c code_O2.c
gcc -O3 -c code_O3.c
gcc -Os -c code_Os.c
gcc -Ofast -c code_Ofast.c
objdump -d code_O0.o > code_O0.s
objdump -d code_O1.o > code_O1.s
objdump -d code_O2.o > code_O2.s
objdump -d code_O3.o > code_O3.s
objdump -d code_Os.o > code_Os.s
objdump -d code_Ofast.o >
code_Ofast.s
pause
12
all: main_text.exe
main_text.exe: main_text.o text.o
gcc main_text.o text.o -o
main_text.exe
main_text.o: main_text.c
gcc -c main_text.c
text.o: text.c
gcc -c text.c
13
#ifndef START_VAL
#define START_VAL 0
extern int accum;
int sum(int x, int y);
#endif
#include<stdio.h>
#include "add.h"
void main(void)
{
int x;
int y;
int z;
printf ("Masukkan bilangan
pertama :");
scanf("%d", &x);
printf ("Masukkan bilangan
kedua :");
scanf ("%d", &y);
z = sum(x,y);
printf ("Jumlahnya : %d\n",
z);
printf ("Masukkan bilangan
pertama :");
scanf("%d", &x);
printf ("Masukkan bilangan
kedua :");
scanf ("%d", &y);
z = sum(x,y);
printf ("Jumlahnya : %d\n",
z);
printf("akumulasi: %d\n",
accum);
}
14
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include "fibo.h"
}
int squaresum (int y, int z)
{
int temp1 = square(y);
int temp2 = square(z);
return temp1+temp2;
}
int main (void)
{
int a = 5; int b = 9;
return squaresum(a,b);
}
15
#include <stdio.h>
#include "inputn.h"
void main(void)
{
int i;
int z;
int batas;
#include <stdio.h>
#include "fibo.h"
#include "inputn.h"
inputn(&batas);
int a = 1;
int b = 1;
}
printf ("%d ", a);
printf ("%d ", b);
if (batas>2)
{
for (i=3;i<=batas;i++)
{
z = fibo (a,b);
printf ("%d ", z);
a=b;
b=z;
}
}
16
17