AbstrakIn the first module of this course, there are nine task
that relating to the compilation of C Language program and
Assembly Language. The software used for the first module is the
GNU C Compiler (GCC), Codeblock, Notepad++, and HexEdit.
The experiments were performed that stages of compiling,
comparing the results of each phase compilation, compilation
techniques with GCC, comparing results compilation is done with
Makefile and batchfile, and the use of stack memory on a
procedure call. At the last of the experiment (the 9th experiment),
the students asked to make Fibonacci program in C Language.
From the first module, the students expected to understand stages
of the compilation of the C Language to be Machine Language,
and be able to compile it with GCC, makefile, and batch file. The
others, the students are also expected to understand and to analyze
Intel X86 Assembly Language and know to use stack memory on
procedure calls..
KeywordAssembly
Language,
Compilation, Stack Memory
GCC,
Optimation,
I. PENDAHULUAN
Preprocessor
Semua perintah preprocessor yang ditulis dalam bahasa
tingkat tinggi akan diproses terlebih dahulu oleh
preprocessor sebelum compiler melaksanakan tugasnya.
Beberapa tugas dari preprocessor ini adalah sebagai
berikut.
o Semua komentar dalam file program diganti degan spasi
satu buah.
o Semua \n (backslash-newline) yang menandakan baris
%~d0
cd "%~dp0"
gcc -O0 -c
gcc -O1 -c
gcc -O2 -c
gcc -O3 -c
gcc -Os -c
gcc -Ofast
objdump -d
objdump -d
objdump -d
objdump -d
objdump -d
objdump -d
pause
code_O0.c
code_O1.c
code_O2.c
code_O3.c
code_Os.c
-c code_Ofast.c
code_O0.o > code_O0.s
code_O1.o > code_O1.s
code_O2.o > code_O2.s
code_O3.o > code_O3.s
code_Os.o > code_Os.s
code_Ofast.o > code_Ofast.s
text.o: text.c
gcc -c text.c
Nilai memory
Nilai memory
G. Tugas 7: Header File
Pada tugas 7 akan belajar untuk membuat dan
memanfaatkan header file dan memahami bagaimana extern
pada header file bekerja. Untuk memulai tugas 7 maka dibuat
file add.c, header file add.h yang akan di-include pada main.c
dan file main.c yang meminta pengguna memasukkan dua buah
bilangan lalu menjumlahkan kedua bilangan tersebut dengan
memanggil fungsi sum. Masing-masing isi file terlampir pada
lampiran 5a-5d.
Proses pemanggilan fungsi yang telah didefinisikan
pada add.c akan mengalami masalah apabila variabel accum
(pada add.c) tidak di-extern terlebih dahulu sehingga variabel
tidak terdefinisi pada main.c Fungsi extern pada header yaitu
agar suatu variabel dalam sebuah file dapat digunakan pada file
.c lainnya. Berikut perintah eksekusi program:
return x*x;
Nilai memory
Nilai memory
return x*x;
Nilai memory
IV. SIMPULAN
return temp1+temp2;
Nilai memory
I. Tugas 9: Program Fibonacci
Pada tugas 9 akan dibuat program penghitung deret Fibonacci
yang sederhana untuk menampilkan deret Fibonacci. Untuk
membuat sebuah program penghitung deret Fibonacci yang
sederhana dibutuhkan prosedur ataupun kode yaitu fibo.c,
fibo.h, inputn.c, inputn.h, dan fibo_main.c, yang terlampir
pada lampiran 6a-6c. Adapun kode untuk header yaitu:
Fibo.h
Inputn.h
REFERENSI
[1] Bryant, Randal, dan David OHallaron. Computer Systems : A Programmers
Perspective 2nd Edition. 2011. Massachusetts : Pearson Education Inc. Patterson,
[2] David, dan John Hennessy. Computer Organization and Design : The
Hardware/Software Interface. 2012. Waltham : Elsevier Inc.
Lampiran
1. Source code untuk tugas I
a. code.c
// Praktikum NWS3103 Arsitektur Sistem Komputer
// Modul
:1
// Percobaan
:1
// Tanggal
: 26 September 2016
// Kelompok
:5
// Rombongan
:1
// Nama (NIM) 1 : Aldo Erwin Manurung (14S14022)
// Nama (NIM) 2 : Grace La Rosa Sihotang (14S14010)
// Nama File
: code.c
// Deskripsi
: Demonstrasi proses kompilasi C
//
Menjumlahkan deret bilangan sebanyak N_LOOP
#define N_LOOP 500
int main(void)
{
int indeks;
int accumulator;
indeks = 0;
accumulator = 0;
while(indeks<N_LOOP)
{
accumulator = accumulator + indeks;
indeks = indeks + 1;
}
return accumulator;
}
b. code.i
# 1 "code.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "code.c"
# 14 "code.c"
int main(void)
{
int indeks;
int accumulator;
indeks = 0;
accumulator = 0;
while(indeks<500)
{
accumulator = accumulator + indeks;
indeks = indeks + 1;
}
return accumulator;
}
c. code.s
.file
.def
.text
.globl _main
.def
_main:
pushl
movl
"code.c"
___main;
.scl
2;
.type
32;
_main; .scl
2;
.type
32;
.endef
%ebp
%esp, %ebp
.endef
subl
andl
movl
movl
movl
call
call
movl
movl
$24, %esp
$-16, %esp
$0, %eax
%eax, -12(%ebp)
-12(%ebp), %eax
__alloca
___main
$0, -4(%ebp)
$0, -8(%ebp)
cmpl
jle
jmp
$499, -4(%ebp)
L4
L3
movl
leal
addl
leal
incl
jmp
-4(%ebp), %eax
-8(%ebp), %edx
%eax, (%edx)
-4(%ebp), %eax
(%eax)
L2
movl
leave
ret
-8(%ebp), %eax
L2:
L4:
L3:
d. code.o
e. code.exe
2. Hasil tugas 3
a. Disasssembly_code_o.asm
code.o: file format pe-i386
Disassembly of section .text:
00000000 <_main>:
0:55 push %ebp
1:89 e5 mov %esp,%ebp
3:83 e4 f0 and $0xfffffff0,%esp
6:83 ec 10 sub $0x10,%esp
9:e8 00 00 00 00 call e <_main+0xe>
e:c7 44 24 0c 00 00 00 movl $0x0,0xc(%esp)
15:00
16:c7 44 24 08 00 00 00 movl $0x0,0x8(%esp)
1d:00
1e:eb 0c jmp 2c <_main+0x2c>
20:8b 44 24 0c mov 0xc(%esp),%eax
24:01 44 24 08 add %eax,0x8(%esp)
28:ff 44 24 0c incl 0xc(%esp)
2c:81 7c 24 0c f3 01 00 cmpl $0x1f3,0xc(%esp)
33:00
34:7e
36:8b
3a:c9
3b:c3
b.
3.
ea jle 20 <_main+0x20>
44 24 08 mov 0x8(%esp),%eax
leave
ret
Hasil tugas 4
a. Code_O0.s
code_O0.o: file format pe-i386
Disassembly of section .text:
00000000 <_main>:
0:55 push %ebp
1:89 e5 mov %esp,%ebp
3:83 e4 f0 and $0xfffffff0,%esp
6:83 ec 10 sub $0x10,%esp
9:e8 00 00 00 00 call e <_main+0xe>
e:c7 44 24 0c 00 00 00 movl $0x0,0xc(%esp)
15:00
16:c7
1d:00
1e:eb
20:8b
24:01
28:ff
2c:81
33:00
34:7e
36:8b
3a:c9
3b:c3
b.
44 24 08 00 00 00 movl $0x0,0x8(%esp)
0c
44
44
44
7c
jmp 2c <_main+0x2c>
24 0c mov 0xc(%esp),%eax
24 08 add %eax,0x8(%esp)
24 0c incl 0xc(%esp)
24 0c f3 01 00 cmpl $0x1f3,0xc(%esp)
ea jle 20 <_main+0x20>
44 24 08 mov 0x8(%esp),%eax
leave
ret
Code_O1.s
code_O1.o: file format pe-i386
Disassembly of section .text:
00000000 <_main>:
0:55 push %ebp
1:89 e5 mov %esp,%ebp
3:83 e4 f0 and $0xfffffff0,%esp
6:e8 00 00 00 00 call b <_main+0xb>
b:b8 00 00 00 00 mov $0x0,%eax
10:40 inc %eax
11:3d f4 01 00 00 cmp $0x1f4,%eax
16:75 f8 jne 10 <_main+0x10>
18:b8 4e e7 01 00 mov $0x1e74e,%eax
1d:c9 leave
1e:c3 ret
1f:90 nop
c.
Code_O2.s
code_O2.o: file format pe-i386
Disassembly of section .text.startup:
00000000 <_main>:
0:55 push %ebp
1:89 e5 mov %esp,%ebp
3:83 e4 f0 and $0xfffffff0,%esp
6:e8 00 00 00 00 call b <_main+0xb>
b:b8 4e e7 01 00 mov $0x1e74e,%eax
10:c9 leave
11:c3 ret
12:90 nop
13:90 nop
d.
Code_O3.s
code_O3.o: file format pe-i386
Disassembly of section .text.startup:
00000000 <_main>:
0:55 push %ebp
1:89 e5 mov %esp,%ebp
3:83 e4 f0 and $0xfffffff0,%esp
6:e8 00 00 00 00 call b <_main+0xb>
b:b8 4e e7 01 00 mov $0x1e74e,%eax
10:c9 leave
11:c3 ret
12:90 nop
13:90 nop
e.
Code_Os.s
code_Os.o: file format pe-i386
Disassembly of section .text.startup:
00000000 <_main>:
0:55 push %ebp
1:89 e5 mov %esp,%ebp
3:83 e4 f0 and $0xfffffff0,%esp
f.
Code_Ofast.s
code_Ofast.o: file format pe-i386
Disassembly of section .text.startup:
00000000 <_main>:
0:55 push %ebp
1:89 e5 mov %esp,%ebp
3:83 e4 f0 and $0xfffffff0,%esp
6:e8 00 00 00 00 call b <_main+0xb>
b:b8 4e e7 01 00 mov $0x1e74e,%eax
10:c9 leave
11:c3 ret
12:90 nop
13:90 nop
4.
Hasil Tugas 5
a. Main_text.c
// Praktikum NWS3103 Arsitektur Sistem Komputer
// Modul
: 1
// Percobaan : 5
// Tanggal
: 26 September 2016
// Kelompok : 5
// Rombongan : 1
// Nama (NIM) 1 : Aldo Erwin Manurung (14S14022)
// Nama (NIM) 2 : Grace La Rosa Sihotang(14S14022)
// Nama File : main_text.c
// Deskripsi : Demonstrasi MakeFile
// Memanggil prosedur test pada text.c
#include "text.h"
void main(void)
{
test();
}
b.
Text.c
// Praktikum NWS3103 Arsitektur Sistem Komputer
// Modul
: 1
// Percobaan : 5
// Tanggal
: 26 September 2016
// Kelompok : 5
// Rombongan : 1
// Nama (NIM) 1 : Aldo Erwin Manurung (14S14022)
// Nama (NIM) 2 : Grace La Rosa Sihotang(14S14022)
// Nama File : text.c
// Deskripsi : Demonstrasi MakeFile, Mencetak string ke layar
#include <stdio.h>
#include "text.h"
void test(void)
{
printf("Arsitektur Sistem Komputer sangat menyenangkan!\n");
}
c.
Text.h
#ifndef TES_H
#define TES_H 100
void test(void);
#endif
5.
Hasil Tugas 7
a. Add.c
// Praktikum NWS3103 Arsitektur Sistem Komputer
// Modul
: 1
// Percobaan : 7
// Tanggal
: 26 September 2016
// Kelompok : 5
// Rombongan : 1
// Nama (NIM) 1 : Aldo Erwin Manurung (14S14022)
// Nama (NIM) 2 : Grace La Rosa Sihotang(14S14022)
// Nama File : add.c
// Deskripsi : Demonstrasi header file
// Menjumlahkan dua bilangan
#define START_VAL 0
int accum = START_VAL;
int sum(int x, int y)
{
int t = x + y;
accum += t;
return t;
}
b.
Main.c
// Praktikum NWS3103 Arsitektur Sistem Komputer
// Modul
: 1
// Percobaan : 7
// Tanggal
: 26 September 2016
// Kelompok : 5
// Rombongan : 1
// Nama (NIM) 1 : Aldo Erwin Manurung (14S14022)
// Nama (NIM) 2 : Grace La Rosa Sihotang(14S14022)
// Nama File : main.c
// Deskripsi : Demonstrasi header file
// Menjumlahkan dua bilangan
#include <stdio.h>
#include "add.h"
int main(void)
{
int x,y,z;
scanf("%d", &x);
scanf("%d", &y);
z = sum(x,y);
printf("%d", z);
return 0;
}
c.
Add.h
#ifndef accum
extern int sum (int x, int y);
#endif
d.
Makefile
all: main.exe
main.exe: main.o add.o
gcc main.o add.o -o main.exe
main.o: main.c
gcc -c main.c
add.o: add.c
gcc -c add.c
6.
b.
Fibo.c
// Praktikum NWS3103 Arsitektur Sistem Komputer
// Modul
: 1
// Percobaan : 9
// Tanggal
: 26 September 2016
// Kelompok : 5
// Rombongan : 1
// Nama (NIM) 1 : Aldo Erwin Manurung (14S14022)
// Nama (NIM) 2 : Grace La Rosa Sihotang(14S14022)
// Nama File : fibo.c
// Deskripsi : Program fibonacci
#include <stdio.h>
int fibo (int n)
{
int i;
int a = 1;
int b = 1;
int c;
printf("1, 1, ");
for (i=1;i<=n-2;i++)
{
c = a+b;
printf("%d, ",c);
a = b;
b = c;
}
return 0;
}
c.
Fibo_main.c
//
//
//
//
//
//
//
//
//