[TUTORIAL] Cara mengubah (Konversi) Bahasa C ke Assembly dengan GCC (Menggunakan VS Code)
Assalamu‘alaikum wr. wb.
Hello guys, Kembali lagi bersama Inzaghi's Blog! Jika kalian ingin mempelajari Bahasa Pemrograman Tingkat Rendah seperti Assembly tapi kesulitan untuk memahami Sintaks kodenya, Nah solusinya adalah dengan membuatnya menggunakan Bahasa C kemudian dikonversikan ke Bahasa Rakitan/Assembly. Pada Postingan ini, saya akan memberikan Tutorial Konversi dari Bahasa C ke Assembly dengan GCC di VS Code.
Sumber Artikel : Situstarget.com dan Geeksforgeeks.org
Memang sih, Bahasa Assembly sulit dipelajari terutama bagi Pemula karena merupakan Bahasa Tingkat Rendah yang digunakan dalam Pemrograman Komputer, Mikroprosesor, Pengendali Mikro, dan perangkat lainnya yang dapat diprogram. Bahasa Assembly juga merupakan Bahasa yang lebih mudah dipahami oleh Komputer karena mendekati Bahasa Mesin.
Sedangkan, Bahasa Pemrograman C merupakan salah satu Bahasa Pemrograman Komputer. Dibuat pada Tahun 1972 oleh Dennis Ritchie untuk Sistem Operasi Unix di Bell Telephone Laboratories. Meskipun C dibuat untuk memprogram sistem dan jaringan komputer namun bahasa ini juga sering digunakan dalam mengembangkan software aplikasi.
Sintaks untuk mengubah ke Assembly :
$ gcc -S filename.c
Tergantung Nama File C yang Anda namai, makanya ditulis sebagai "filename.c".
Mari kita lihat contohnya di bawah ini.
1. Program Sederhana (Hello World)
Pertama, kita buat terlebih dahulu Program dalam Bahasa C yang berjudul "hello.c". Anda bisa membuatnya dengan menggunakan Visual Studio Code (VS Code).
#include <stdio.h>int main() {printf("Hello world!");return 0;}
Kemudian, kita ketik :
gcc -S hello.c
Maka, File Assembly akan dibuat secara Otomatis oleh Kompailer (Compiler) GCC. Inilah Gambar-nya :
.file "main.c".text.def __main; .scl 2; .type 32; .endef.globl main.def main; .scl 2; .type 32; .endef.seh_proc mainmain:pushq %rbp.seh_pushreg %rbpmovq %rsp, %rbp.seh_setframe %rbp, 0subq $48, %rsp.seh_stackalloc 48.seh_endprologuecall __mainmovl $6, -4(%rbp)movl $4, -8(%rbp)movl -8(%rbp), %edxmovl -4(%rbp), %eaxmovl %eax, %ecxcall addmovl %eax, -12(%rbp)movl $0, %eaxaddq $48, %rsppopq %rbpret.seh_endproc.ident "GCC: (Rev5, Built by MSYS2 project) 10.3.0".def add; .scl 2; .type 32; .endef
Memang, Baris kodenya lebih panjang dan lebih banyak jika diubah ke Bahasa Assembly. Karena setiap Baris indentasi dalam kode di atas sesuai dengan instruksi mesin tunggal. Misalnya, instruksi
pushq
menunjukkan bahwa isi register %rbp
harus didorong ke tumpukan program. Semua informasi tentang nama variabel lokal atau tipe data telah dihapus.Mari kita ambil contoh lain yang ada di bawah ini :
2. Penjumlahan Sederhana
Sama seperti diatas, kita buat terlebih dahulu Program dalam Bahasa C yang berjudul "addition.c".
#include <stdio.h>int main(){int a, b, c;a = 1;b = 2;c = a+b;printf("a = %d\n", a);printf("b = %d\n", b);printf("a + b = %d", c);}
Kemudian, kita ketik :
gcc -S addition.c
Maka, File Assembly akan dibuat secara Otomatis oleh Kompailer (Compiler) GCC. Inilah Gambar-nya :
.file "addition.c".text.def printf; .scl 3; .type 32; .endef.seh_proc printfprintf:pushq %rbp.seh_pushreg %rbppushq %rbx.seh_pushreg %rbxsubq $56, %rsp.seh_stackalloc 56leaq 48(%rsp), %rbp.seh_setframe %rbp, 48.seh_endprologuemovq %rcx, 32(%rbp)movq %rdx, 40(%rbp)movq %r8, 48(%rbp)movq %r9, 56(%rbp)leaq 40(%rbp), %raxmovq %rax, -16(%rbp)movq -16(%rbp), %rbxmovl $1, %ecxmovq __imp___acrt_iob_func(%rip), %raxcall *%raxmovq %rbx, %r8movq 32(%rbp), %rdxmovq %rax, %rcxcall __mingw_vfprintfmovl %eax, -4(%rbp)movl -4(%rbp), %eaxaddq $56, %rsppopq %rbxpopq %rbpret.seh_endproc.def __main; .scl 2; .type 32; .endef.section .rdata,"dr".LC0:.ascii "a = %d\12\0".LC1:.ascii "b = %d\12\0".LC2:.ascii "a + b = %d\0".text.globl main.def main; .scl 2; .type 32; .endef.seh_proc mainmain:pushq %rbp.seh_pushreg %rbpmovq %rsp, %rbp.seh_setframe %rbp, 0subq $48, %rsp.seh_stackalloc 48.seh_endprologuecall __mainmovl $1, -4(%rbp)movl $2, -8(%rbp)movl -4(%rbp), %edxmovl -8(%rbp), %eaxaddl %edx, %eaxmovl %eax, -12(%rbp)movl -4(%rbp), %eaxmovl %eax, %edxleaq .LC0(%rip), %rcxcall printfmovl -8(%rbp), %eaxmovl %eax, %edxleaq .LC1(%rip), %rcxcall printfmovl -12(%rbp), %eaxmovl %eax, %edxleaq .LC2(%rip), %rcxcall printfmovl $0, %eaxaddq $48, %rsppopq %rbpret.seh_endproc.ident "GCC: (Rev5, Built by MSYS2 project) 10.3.0".def __mingw_vfprintf; .scl 2; .type 32; .endef
3. Fungsi Operasi Hitung
Nama File : aritmethic_function.c
#include <stdio.h>float tambah(float x, float y){return (x+y);}float kurang(float x, float y){return (x-y);}float kali(float x, float y){return (x*y);}float bagi(float x, float y){return (x/y);}int main(){int add, sub, mul;float dvd;int a = 9;int b = 4;add = tambah(a, b);sub = kurang(a, b);mul = kali(a, b);dvd = bagi(a, b);printf("a = %d, b = %d\n", a, b);printf("a + b = %d\n", add);printf("a - b = %d\n", sub);printf("a * b = %d\n", mul);printf("a / b = %.2f\n", dvd);}
Ketik :
gcc -S aritmethic_function.c
Kode dalam Bahasa Assembly GCC (aritmethic_function.s) :
.file "aritmethic_function.c".text.def printf; .scl 3; .type 32; .endef.seh_proc printfprintf:pushq %rbp.seh_pushreg %rbppushq %rbx.seh_pushreg %rbxsubq $56, %rsp.seh_stackalloc 56leaq 48(%rsp), %rbp.seh_setframe %rbp, 48.seh_endprologuemovq %rcx, 32(%rbp)movq %rdx, 40(%rbp)movq %r8, 48(%rbp)movq %r9, 56(%rbp)leaq 40(%rbp), %raxmovq %rax, -16(%rbp)movq -16(%rbp), %rbxmovl $1, %ecxmovq __imp___acrt_iob_func(%rip), %raxcall *%raxmovq %rbx, %r8movq 32(%rbp), %rdxmovq %rax, %rcxcall __mingw_vfprintfmovl %eax, -4(%rbp)movl -4(%rbp), %eaxaddq $56, %rsppopq %rbxpopq %rbpret.seh_endproc.globl tambah.def tambah; .scl 2; .type 32; .endef.seh_proc tambahtambah:pushq %rbp.seh_pushreg %rbpmovq %rsp, %rbp.seh_setframe %rbp, 0.seh_endprologuemovss %xmm0, 16(%rbp)movss %xmm1, 24(%rbp)movss 16(%rbp), %xmm0addss 24(%rbp), %xmm0popq %rbpret.seh_endproc.globl kurang.def kurang; .scl 2; .type 32; .endef.seh_proc kurangkurang:pushq %rbp.seh_pushreg %rbpmovq %rsp, %rbp.seh_setframe %rbp, 0.seh_endprologuemovss %xmm0, 16(%rbp)movss %xmm1, 24(%rbp)movss 16(%rbp), %xmm0subss 24(%rbp), %xmm0popq %rbpret.seh_endproc.globl kali.def kali; .scl 2; .type 32; .endef.seh_proc kalikali:pushq %rbp.seh_pushreg %rbpmovq %rsp, %rbp.seh_setframe %rbp, 0.seh_endprologuemovss %xmm0, 16(%rbp)movss %xmm1, 24(%rbp)movss 16(%rbp), %xmm0mulss 24(%rbp), %xmm0popq %rbpret.seh_endproc.globl bagi.def bagi; .scl 2; .type 32; .endef.seh_proc bagibagi:pushq %rbp.seh_pushreg %rbpmovq %rsp, %rbp.seh_setframe %rbp, 0.seh_endprologuemovss %xmm0, 16(%rbp)movss %xmm1, 24(%rbp)movss 16(%rbp), %xmm0divss 24(%rbp), %xmm0popq %rbpret.seh_endproc.def __main; .scl 2; .type 32; .endef.section .rdata,"dr".LC0:.ascii "a = %d, b = %d\12\0".LC1:.ascii "a + b = %d\12\0".LC2:.ascii "a - b = %d\12\0".LC3:.ascii "a * b = %d\12\0".LC4:.ascii "a / b = %.2f\12\0".text.globl main.def main; .scl 2; .type 32; .endef.seh_proc mainmain:pushq %rbp.seh_pushreg %rbpmovq %rsp, %rbp.seh_setframe %rbp, 0subq $64, %rsp.seh_stackalloc 64.seh_endprologuecall __mainmovl $9, -4(%rbp)movl $4, -8(%rbp)pxor %xmm0, %xmm0cvtsi2ssl -8(%rbp), %xmm0pxor %xmm2, %xmm2cvtsi2ssl -4(%rbp), %xmm2movd %xmm2, %eaxmovaps %xmm0, %xmm1movd %eax, %xmm0call tambahcvttss2sil %xmm0, %eaxmovl %eax, -12(%rbp)pxor %xmm0, %xmm0cvtsi2ssl -8(%rbp), %xmm0pxor %xmm3, %xmm3cvtsi2ssl -4(%rbp), %xmm3movd %xmm3, %eaxmovaps %xmm0, %xmm1movd %eax, %xmm0call kurangcvttss2sil %xmm0, %eaxmovl %eax, -16(%rbp)pxor %xmm0, %xmm0cvtsi2ssl -8(%rbp), %xmm0pxor %xmm4, %xmm4cvtsi2ssl -4(%rbp), %xmm4movd %xmm4, %eaxmovaps %xmm0, %xmm1movd %eax, %xmm0call kalicvttss2sil %xmm0, %eaxmovl %eax, -20(%rbp)pxor %xmm0, %xmm0cvtsi2ssl -8(%rbp), %xmm0pxor %xmm5, %xmm5cvtsi2ssl -4(%rbp), %xmm5movd %xmm5, %eaxmovaps %xmm0, %xmm1movd %eax, %xmm0call bagimovd %xmm0, %eaxmovl %eax, -24(%rbp)movl -8(%rbp), %edxmovl -4(%rbp), %eaxmovl %edx, %r8dmovl %eax, %edxleaq .LC0(%rip), %rcxcall printfmovl -12(%rbp), %eaxmovl %eax, %edxleaq .LC1(%rip), %rcxcall printfmovl -16(%rbp), %eaxmovl %eax, %edxleaq .LC2(%rip), %rcxcall printfmovl -20(%rbp), %eaxmovl %eax, %edxleaq .LC3(%rip), %rcxcall printfpxor %xmm0, %xmm0cvtss2sd -24(%rbp), %xmm0movq %xmm0, %raxmovq %rax, %rdxmovq %rdx, %xmm0movapd %xmm0, %xmm1movq %rax, %rdxleaq .LC4(%rip), %rcxcall printfmovl $0, %eaxaddq $64, %rsppopq %rbpret.seh_endproc.ident "GCC: (Rev5, Built by MSYS2 project) 10.3.0".def __mingw_vfprintf; .scl 2; .type 32; .endef
3. Array Sederhana
Nama File : array.c
#include <stdio.h>int main() {int x[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};x[0] = 5;printf("%d\n", x[0]);printf("%d\n", x[6]);int length = sizeof(x)/sizeof(x[0]);for (int i = 0; i < length; i++) {printf("%d ", x[i]);}}
Ketik :
gcc -S array.c
Kode dalam Bahasa Assembly GCC (array.s) :
.file "array.c".text.def printf; .scl 3; .type 32; .endef.seh_proc printfprintf:pushq %rbp.seh_pushreg %rbppushq %rbx.seh_pushreg %rbxsubq $56, %rsp.seh_stackalloc 56leaq 48(%rsp), %rbp.seh_setframe %rbp, 48.seh_endprologuemovq %rcx, 32(%rbp)movq %rdx, 40(%rbp)movq %r8, 48(%rbp)movq %r9, 56(%rbp)leaq 40(%rbp), %raxmovq %rax, -16(%rbp)movq -16(%rbp), %rbxmovl $1, %ecxmovq __imp___acrt_iob_func(%rip), %raxcall *%raxmovq %rbx, %r8movq 32(%rbp), %rdxmovq %rax, %rcxcall __mingw_vfprintfmovl %eax, -4(%rbp)movl -4(%rbp), %eaxaddq $56, %rsppopq %rbxpopq %rbpret.seh_endproc.def __main; .scl 2; .type 32; .endef.section .rdata,"dr".LC0:.ascii "%d\12\0".LC1:.ascii "%d \0".text.globl main.def main; .scl 2; .type 32; .endef.seh_proc mainmain:pushq %rbp.seh_pushreg %rbpmovq %rsp, %rbp.seh_setframe %rbp, 0subq $80, %rsp.seh_stackalloc 80.seh_endprologuecall __mainmovl $1, -48(%rbp)movl $2, -44(%rbp)movl $3, -40(%rbp)movl $4, -36(%rbp)movl $5, -32(%rbp)movl $6, -28(%rbp)movl $7, -24(%rbp)movl $8, -20(%rbp)movl $9, -16(%rbp)movl $5, -48(%rbp)movl -48(%rbp), %eaxmovl %eax, %edxleaq .LC0(%rip), %rcxcall printfmovl -24(%rbp), %eaxmovl %eax, %edxleaq .LC0(%rip), %rcxcall printfmovl $9, -8(%rbp)movl $0, -4(%rbp)jmp .L4.L5:movl -4(%rbp), %eaxcltqmovl -48(%rbp,%rax,4), %eaxmovl %eax, %edxleaq .LC1(%rip), %rcxcall printfaddl $1, -4(%rbp).L4:movl -4(%rbp), %eaxcmpl -8(%rbp), %eaxjl .L5movl $0, %eaxaddq $80, %rsppopq %rbpret.seh_endproc.ident "GCC: (Rev5, Built by MSYS2 project) 10.3.0".def __mingw_vfprintf; .scl 2; .type 32; .endef
4. Pointer Sederhana
Nama File : pointer.c
#include <stdio.h>int main() {int var1;char var2[10];printf("Alamat dari variabel var1 adalah : %x\n", &var1);printf("Alamat dari variabel var2 adalah : %x\n", &var2);return 0;}
Ketik :
gcc -S pointer.c
Kode dalam Bahasa Assembly GCC (pointer.s) :
.file "pointer.c".text.def printf; .scl 3; .type 32; .endef.seh_proc printfprintf:pushq %rbp.seh_pushreg %rbppushq %rbx.seh_pushreg %rbxsubq $56, %rsp.seh_stackalloc 56leaq 48(%rsp), %rbp.seh_setframe %rbp, 48.seh_endprologuemovq %rcx, 32(%rbp)movq %rdx, 40(%rbp)movq %r8, 48(%rbp)movq %r9, 56(%rbp)leaq 40(%rbp), %raxmovq %rax, -16(%rbp)movq -16(%rbp), %rbxmovl $1, %ecxmovq __imp___acrt_iob_func(%rip), %raxcall *%raxmovq %rbx, %r8movq 32(%rbp), %rdxmovq %rax, %rcxcall __mingw_vfprintfmovl %eax, -4(%rbp)movl -4(%rbp), %eaxaddq $56, %rsppopq %rbxpopq %rbpret.seh_endproc.def __main; .scl 2; .type 32; .endef.section .rdata,"dr".align 8.LC0:.ascii "Alamat dari variabel var1 adalah : %x\12\0".align 8.LC1:.ascii "Alamat dari variabel var2 adalah : %x\12\0".text.globl main.def main; .scl 2; .type 32; .endef.seh_proc mainmain:pushq %rbp.seh_pushreg %rbpmovq %rsp, %rbp.seh_setframe %rbp, 0subq $48, %rsp.seh_stackalloc 48.seh_endprologuecall __mainleaq -4(%rbp), %raxmovq %rax, %rdxleaq .LC0(%rip), %rcxcall printfleaq -14(%rbp), %raxmovq %rax, %rdxleaq .LC1(%rip), %rcxcall printfmovl $0, %eaxaddq $48, %rsppopq %rbpret.seh_endproc.ident "GCC: (Rev5, Built by MSYS2 project) 10.3.0".def __mingw_vfprintf; .scl 2; .type 32; .endef
5. Swap Program Sederhana
Nama File : swap.c
#include <stdio.h>int swap(int *x, int *y) {int temp = *x;*x = *y;*y = temp;}int main() {int x = 5;int y = 10;printf("%d", swap(&x, &y));return 0;}
Ketik :
gcc -S swap.c
Kode dalam Bahasa Assembly GCC (swap.s) :
.file "swap.c".text.def printf; .scl 3; .type 32; .endef.seh_proc printfprintf:pushq %rbp.seh_pushreg %rbppushq %rbx.seh_pushreg %rbxsubq $56, %rsp.seh_stackalloc 56leaq 48(%rsp), %rbp.seh_setframe %rbp, 48.seh_endprologuemovq %rcx, 32(%rbp)movq %rdx, 40(%rbp)movq %r8, 48(%rbp)movq %r9, 56(%rbp)leaq 40(%rbp), %raxmovq %rax, -16(%rbp)movq -16(%rbp), %rbxmovl $1, %ecxmovq __imp___acrt_iob_func(%rip), %raxcall *%raxmovq %rbx, %r8movq 32(%rbp), %rdxmovq %rax, %rcxcall __mingw_vfprintfmovl %eax, -4(%rbp)movl -4(%rbp), %eaxaddq $56, %rsppopq %rbxpopq %rbpret.seh_endproc.globl swap.def swap; .scl 2; .type 32; .endef.seh_proc swapswap:pushq %rbp.seh_pushreg %rbpmovq %rsp, %rbp.seh_setframe %rbp, 0subq $16, %rsp.seh_stackalloc 16.seh_endprologuemovq %rcx, 16(%rbp)movq %rdx, 24(%rbp)movq 16(%rbp), %raxmovl (%rax), %eaxmovl %eax, -4(%rbp)movq 24(%rbp), %raxmovl (%rax), %edxmovq 16(%rbp), %raxmovl %edx, (%rax)movq 24(%rbp), %raxmovl -4(%rbp), %edxmovl %edx, (%rax)nopaddq $16, %rsppopq %rbpret.seh_endproc.def __main; .scl 2; .type 32; .endef.section .rdata,"dr".LC0:.ascii "%d\0".text.globl main.def main; .scl 2; .type 32; .endef.seh_proc mainmain:pushq %rbp.seh_pushreg %rbpmovq %rsp, %rbp.seh_setframe %rbp, 0subq $48, %rsp.seh_stackalloc 48.seh_endprologuecall __mainmovl $5, -4(%rbp)movl $10, -8(%rbp)leaq -8(%rbp), %rdxleaq -4(%rbp), %raxmovq %rax, %rcxcall swapmovl %eax, %edxleaq .LC0(%rip), %rcxcall printfmovl $0, %eaxaddq $48, %rsppopq %rbpret.seh_endproc.ident "GCC: (Rev5, Built by MSYS2 project) 10.3.0".def __mingw_vfprintf; .scl 2; .type 32; .endef
6. Program Fungsi Ackermann
Nama File : ackermann.c
#include <stdio.h>int ack(int m, int n){if (m == 0){return n + 1;}else if((m > 0) && (n == 0)){return ack(m - 1, 1);}else if((m > 0) && (n > 0)){return ack(m - 1, ack(m, n - 1));}}int main(){int x, y, A;printf("Ackermann Function A(x, y)\n");printf("Insert value of x : ");scanf("%d", &x);printf("Insert value of y : ");scanf("%d", &y);A = ack(x, y);printf("A(%d, %d) = %d", x, y, A);return 0;}
Ketik :
gcc -S ackermann.c
Kode dalam Bahasa Assembly GCC (ackermann.s) :
.file "ackermann.c".text.def scanf; .scl 3; .type 32; .endef.seh_proc scanfscanf:pushq %rbp.seh_pushreg %rbppushq %rbx.seh_pushreg %rbxsubq $56, %rsp.seh_stackalloc 56leaq 48(%rsp), %rbp.seh_setframe %rbp, 48.seh_endprologuemovq %rcx, 32(%rbp)movq %rdx, 40(%rbp)movq %r8, 48(%rbp)movq %r9, 56(%rbp)leaq 40(%rbp), %raxmovq %rax, -16(%rbp)movq -16(%rbp), %rbxmovl $0, %ecxmovq __imp___acrt_iob_func(%rip), %raxcall *%raxmovq %rbx, %r8movq 32(%rbp), %rdxmovq %rax, %rcxcall __mingw_vfscanfmovl %eax, -4(%rbp)movl -4(%rbp), %eaxaddq $56, %rsppopq %rbxpopq %rbpret.seh_endproc.def printf; .scl 3; .type 32; .endef.seh_proc printfprintf:pushq %rbp.seh_pushreg %rbppushq %rbx.seh_pushreg %rbxsubq $56, %rsp.seh_stackalloc 56leaq 48(%rsp), %rbp.seh_setframe %rbp, 48.seh_endprologuemovq %rcx, 32(%rbp)movq %rdx, 40(%rbp)movq %r8, 48(%rbp)movq %r9, 56(%rbp)leaq 40(%rbp), %raxmovq %rax, -16(%rbp)movq -16(%rbp), %rbxmovl $1, %ecxmovq __imp___acrt_iob_func(%rip), %raxcall *%raxmovq %rbx, %r8movq 32(%rbp), %rdxmovq %rax, %rcxcall __mingw_vfprintfmovl %eax, -4(%rbp)movl -4(%rbp), %eaxaddq $56, %rsppopq %rbxpopq %rbpret.seh_endproc.globl ack.def ack; .scl 2; .type 32; .endef.seh_proc ackack:pushq %rbp.seh_pushreg %rbpmovq %rsp, %rbp.seh_setframe %rbp, 0subq $32, %rsp.seh_stackalloc 32.seh_endprologuemovl %ecx, 16(%rbp)movl %edx, 24(%rbp)cmpl $0, 16(%rbp)jne .L6movl 24(%rbp), %eaxaddl $1, %eaxjmp .L5.L6:cmpl $0, 16(%rbp)jle .L8cmpl $0, 24(%rbp)jne .L8movl 16(%rbp), %eaxsubl $1, %eaxmovl $1, %edxmovl %eax, %ecxcall ackjmp .L5.L8:cmpl $0, 16(%rbp)jle .L9cmpl $0, 24(%rbp)jle .L9movl 24(%rbp), %eaxsubl $1, %eaxmovl %eax, %edxmovl 16(%rbp), %ecxcall ackmovl %eax, %edxmovl 16(%rbp), %eaxsubl $1, %eaxmovl %eax, %ecxcall ackjmp .L5.L9:.L5:addq $32, %rsppopq %rbpret.seh_endproc.def __main; .scl 2; .type 32; .endef.section .rdata,"dr".LC0:.ascii "Ackermann Function A(x, y)\12\0".LC1:.ascii "Insert value of x : \0".LC2:.ascii "%d\0".LC3:.ascii "Insert value of y : \0".LC4:.ascii "A(%d, %d) = %d\0".text.globl main.def main; .scl 2; .type 32; .endef.seh_proc mainmain:pushq %rbp.seh_pushreg %rbpmovq %rsp, %rbp.seh_setframe %rbp, 0subq $48, %rsp.seh_stackalloc 48.seh_endprologuecall __mainleaq .LC0(%rip), %rcxcall printfleaq .LC1(%rip), %rcxcall printfleaq -8(%rbp), %raxmovq %rax, %rdxleaq .LC2(%rip), %rcxcall scanfleaq .LC3(%rip), %rcxcall printfleaq -12(%rbp), %raxmovq %rax, %rdxleaq .LC2(%rip), %rcxcall scanfmovl -12(%rbp), %edxmovl -8(%rbp), %eaxmovl %eax, %ecxcall ackmovl %eax, -4(%rbp)movl -12(%rbp), %edxmovl -8(%rbp), %eaxmovl -4(%rbp), %ecxmovl %ecx, %r9dmovl %edx, %r8dmovl %eax, %edxleaq .LC4(%rip), %rcxcall printfmovl $0, %eaxaddq $48, %rsppopq %rbpret.seh_endproc.ident "GCC: (Rev5, Built by MSYS2 project) 10.3.0".def __mingw_vfscanf; .scl 2; .type 32; .endef.def __mingw_vfprintf; .scl 2; .type 32; .endef
7. Program Format Waktu (Nama Hari dan Bulan)
Nama File : day-and-month-name.c
#include <time.h>#include <stdio.h>int main() {char bufDay[32];struct tm *ts1;size_t last1;time_t nowDy = time(NULL);ts1 = localtime(&nowDy);last1 = strftime(bufDay, 32, "%A", ts1);bufDay[last1] = '\0';char bufMon[32];struct tm *ts2;size_t last2;time_t nowMn = time(NULL);ts2 = localtime(&nowMn);last2 = strftime(bufMon, 32, "%B", ts2);bufMon[last2] = '\0';printf("%s\n", bufDay);printf("%s\n", bufMon);return 0;}
Ketik :
gcc -S day-and-month-name.c
Kode dalam Bahasa Assembly GCC (day-and-month-name.s) :
.file "day-and-month-name.c".text.def time; .scl 3; .type 32; .endef.seh_proc timetime:pushq %rbp.seh_pushreg %rbpmovq %rsp, %rbp.seh_setframe %rbp, 0subq $32, %rsp.seh_stackalloc 32.seh_endprologuemovq %rcx, 16(%rbp)movq 16(%rbp), %rcxmovq __imp__time64(%rip), %raxcall *%raxaddq $32, %rsppopq %rbpret.seh_endproc.def localtime; .scl 3; .type 32; .endef.seh_proc localtimelocaltime:pushq %rbp.seh_pushreg %rbpmovq %rsp, %rbp.seh_setframe %rbp, 0subq $32, %rsp.seh_stackalloc 32.seh_endprologuemovq %rcx, 16(%rbp)movq 16(%rbp), %rcxmovq __imp__localtime64(%rip), %raxcall *%raxaddq $32, %rsppopq %rbpret.seh_endproc.def printf; .scl 3; .type 32; .endef.seh_proc printfprintf:pushq %rbp.seh_pushreg %rbppushq %rbx.seh_pushreg %rbxsubq $56, %rsp.seh_stackalloc 56leaq 48(%rsp), %rbp.seh_setframe %rbp, 48.seh_endprologuemovq %rcx, 32(%rbp)movq %rdx, 40(%rbp)movq %r8, 48(%rbp)movq %r9, 56(%rbp)leaq 40(%rbp), %raxmovq %rax, -16(%rbp)movq -16(%rbp), %rbxmovl $1, %ecxmovq __imp___acrt_iob_func(%rip), %raxcall *%raxmovq %rbx, %r8movq 32(%rbp), %rdxmovq %rax, %rcxcall __mingw_vfprintfmovl %eax, -4(%rbp)movl -4(%rbp), %eaxaddq $56, %rsppopq %rbxpopq %rbpret.seh_endproc.def __main; .scl 2; .type 32; .endef.section .rdata,"dr".LC0:.ascii "%A\0".LC1:.ascii "%B\0".LC2:.ascii "%s\12\0".text.globl main.def main; .scl 2; .type 32; .endef.seh_proc mainmain:pushq %rbp.seh_pushreg %rbpmovq %rsp, %rbp.seh_setframe %rbp, 0subq $160, %rsp.seh_stackalloc 160.seh_endprologuecall __mainmovl $0, %ecxcall timemovq %rax, -72(%rbp)leaq -72(%rbp), %raxmovq %rax, %rcxcall localtimemovq %rax, -8(%rbp)movq -8(%rbp), %rdxleaq -64(%rbp), %raxmovq %rdx, %r9leaq .LC0(%rip), %r8movl $32, %edxmovq %rax, %rcxcall strftimemovq %rax, -16(%rbp)leaq -64(%rbp), %rdxmovq -16(%rbp), %raxaddq %rdx, %raxmovb $0, (%rax)movl $0, %ecxcall timemovq %rax, -120(%rbp)leaq -120(%rbp), %raxmovq %rax, %rcxcall localtimemovq %rax, -24(%rbp)movq -24(%rbp), %rdxleaq -112(%rbp), %raxmovq %rdx, %r9leaq .LC1(%rip), %r8movl $32, %edxmovq %rax, %rcxcall strftimemovq %rax, -32(%rbp)leaq -112(%rbp), %rdxmovq -32(%rbp), %raxaddq %rdx, %raxmovb $0, (%rax)leaq -64(%rbp), %raxmovq %rax, %rdxleaq .LC2(%rip), %rcxcall printfleaq -112(%rbp), %raxmovq %rax, %rdxleaq .LC2(%rip), %rcxcall printfmovl $0, %eaxaddq $160, %rsppopq %rbpret.seh_endproc.ident "GCC: (Rev5, Built by MSYS2 project) 10.3.0".def __mingw_vfprintf; .scl 2; .type 32; .endef.def strftime; .scl 2; .type 32; .endef
8. Program Sistem Penanggalan
Nama File : penanggalan.c
#include <stdio.h>#include <stdlib.h>#include <time.h>#include <string.h>int main(){int leap, thn, bln, tgl, har;int harke, harmun;int jam, mnt, dtk;double dateCal, persen, permun;char bufMon[32];struct tm *ts1;size_t last1;time_t nowMn = time(NULL);ts1 = localtime(&nowMn);last1 = strftime(bufMon, 32, "%B", ts1);bufMon[last1] = '\0';char bufDay[32];struct tm *ts2;size_t last2;time_t nowDy = time(NULL);ts2 = localtime(&nowDy);last2 = strftime(bufDay, 32, "%A", ts2);bufDay[last2] = '\0';char bufDate[32];struct tm *ts3;size_t last3;time_t nowDt = time(NULL);ts3 = localtime(&nowDt);last3 = strftime(bufDate, 32, "%A, %d %B %G", ts3);char bufTime[32];struct tm *ts4;size_t last4;time_t nowTm = time(NULL);ts4 = localtime(&nowTm);last4 = strftime(bufTime, 32, "%H:%M:%S", ts4);bufTime[last4] = '\0';time_t now;time(&now);struct tm *local_time = localtime(&now);thn = 1900 + local_time->tm_year;bln = 1 + local_time->tm_mon;tgl = local_time->tm_mday;har = local_time->tm_wday;harke = 1 + local_time->tm_yday;jam = local_time->tm_hour;mnt = local_time->tm_min;dtk = local_time->tm_sec;if (thn % 400 == 0){leap = 366;}else if (thn % 100 == 0){leap = 365;}else if (thn % 4 == 0){leap = 366;}else {leap = 365;}dateCal = (harke-1)+((double)jam/24)+((double)mnt/1440)+((double)dtk/86400);persen = (double)(100*dateCal/leap);harmun = 1+leap-harke;permun = 100-persen;printf("Tahun : %d\n", thn);printf("Bulan : %d (%s)\n", bln, bufMon);printf("Tanggal : %d\n", tgl);printf("Hari : %d (%s)\n", har, bufDay);printf("Hari ke (dalam Setahun) : %d\n", harke);printf("Sisa Hari (Tahun depan) : %d\n", harmun);printf("Persentase dalam Setahun : %f%\n", persen);printf("Persentase Hitung Mundur : %f%\n", permun);printf("Jam : %d\n", jam);printf("Menit : %d\n", mnt);printf("Detik : %d\n", dtk);printf("\n");printf("%s, %02d %s %d\n", bufDay, tgl, bufMon, thn);printf("Day-%d (%.4f%)\n", harke, persen);printf("%s", bufTime);return 0;}
Ketik :
gcc -S penanggalan.c
Kode dalam Bahasa Assembly GCC (penanggalan.s) :
.file "penanggalan.c".text.def printf; .scl 3; .type 32; .endef.seh_proc printfprintf:pushq %rbp.seh_pushreg %rbppushq %rbx.seh_pushreg %rbxsubq $56, %rsp.seh_stackalloc 56leaq 48(%rsp), %rbp.seh_setframe %rbp, 48.seh_endprologuemovq %rcx, 32(%rbp)movq %rdx, 40(%rbp)movq %r8, 48(%rbp)movq %r9, 56(%rbp)leaq 40(%rbp), %raxmovq %rax, -16(%rbp)movq -16(%rbp), %rbxmovl $1, %ecxmovq __imp___acrt_iob_func(%rip), %raxcall *%raxmovq %rbx, %r8movq 32(%rbp), %rdxmovq %rax, %rcxcall __mingw_vfprintfmovl %eax, -4(%rbp)movl -4(%rbp), %eaxaddq $56, %rsppopq %rbxpopq %rbpret.seh_endproc.def time; .scl 3; .type 32; .endef.seh_proc timetime:pushq %rbp.seh_pushreg %rbpmovq %rsp, %rbp.seh_setframe %rbp, 0subq $32, %rsp.seh_stackalloc 32.seh_endprologuemovq %rcx, 16(%rbp)movq 16(%rbp), %rcxmovq __imp__time64(%rip), %raxcall *%raxaddq $32, %rsppopq %rbpret.seh_endproc.def localtime; .scl 3; .type 32; .endef.seh_proc localtimelocaltime:pushq %rbp.seh_pushreg %rbpmovq %rsp, %rbp.seh_setframe %rbp, 0subq $32, %rsp.seh_stackalloc 32.seh_endprologuemovq %rcx, 16(%rbp)movq 16(%rbp), %rcxmovq __imp__localtime64(%rip), %raxcall *%raxaddq $32, %rsppopq %rbpret.seh_endproc.def __main; .scl 2; .type 32; .endef.section .rdata,"dr".LC0:.ascii "%B\0".LC1:.ascii "%A\0".LC2:.ascii "%A, %d %B %G\0".LC3:.ascii "%H:%M:%S\0".align 8.LC8:.ascii "Tahun : %d\12\0".align 8.LC9:.ascii "Bulan : %d (%s)\12\0".align 8.LC10:.ascii "Tanggal : %d\12\0".align 8.LC11:.ascii "Hari : %d (%s)\12\0".align 8.LC12:.ascii "Hari ke (dalam Setahun) : %d\12\0".align 8.LC13:.ascii "Sisa Hari (Tahun depan) : %d\12\0".align 8.LC14:.ascii "Persentase dalam Setahun : %f%\12\0".align 8.LC15:.ascii "Persentase Hitung Mundur : %f%\12\0".align 8.LC16:.ascii "Jam : %d\12\0".align 8.LC17:.ascii "Menit : %d\12\0".align 8.LC18:.ascii "Detik : %d\12\0".LC19:.ascii "\12\0".LC20:.ascii "%s, %02d %s %d\12\0".LC21:.ascii "Day-%d (%.4f%)\12\0".LC22:.ascii "%s\0".text.globl main.def main; .scl 2; .type 32; .endef.seh_proc mainmain:pushq %rbp.seh_pushreg %rbpsubq $384, %rsp.seh_stackalloc 384leaq 128(%rsp), %rbp.seh_setframe %rbp, 128.seh_endprologuecall __mainmovl $0, %ecxcall timemovq %rax, 72(%rbp)leaq 72(%rbp), %raxmovq %rax, %rcxcall localtimemovq %rax, 240(%rbp)movq 240(%rbp), %rdxleaq 80(%rbp), %raxmovq %rdx, %r9leaq .LC0(%rip), %r8movl $32, %edxmovq %rax, %rcxcall strftimemovq %rax, 232(%rbp)leaq 80(%rbp), %rdxmovq 232(%rbp), %raxaddq %rdx, %raxmovb $0, (%rax)movl $0, %ecxcall timemovq %rax, 24(%rbp)leaq 24(%rbp), %raxmovq %rax, %rcxcall localtimemovq %rax, 224(%rbp)movq 224(%rbp), %rdxleaq 32(%rbp), %raxmovq %rdx, %r9leaq .LC1(%rip), %r8movl $32, %edxmovq %rax, %rcxcall strftimemovq %rax, 216(%rbp)leaq 32(%rbp), %rdxmovq 216(%rbp), %raxaddq %rdx, %raxmovb $0, (%rax)movl $0, %ecxcall timemovq %rax, -24(%rbp)leaq -24(%rbp), %raxmovq %rax, %rcxcall localtimemovq %rax, 208(%rbp)movq 208(%rbp), %rdxleaq -16(%rbp), %raxmovq %rdx, %r9leaq .LC2(%rip), %r8movl $32, %edxmovq %rax, %rcxcall strftimemovq %rax, 200(%rbp)movl $0, %ecxcall timemovq %rax, -72(%rbp)leaq -72(%rbp), %raxmovq %rax, %rcxcall localtimemovq %rax, 192(%rbp)movq 192(%rbp), %rdxleaq -64(%rbp), %raxmovq %rdx, %r9leaq .LC3(%rip), %r8movl $32, %edxmovq %rax, %rcxcall strftimemovq %rax, 184(%rbp)leaq -64(%rbp), %rdxmovq 184(%rbp), %raxaddq %rdx, %raxmovb $0, (%rax)leaq -80(%rbp), %raxmovq %rax, %rcxcall timeleaq -80(%rbp), %raxmovq %rax, %rcxcall localtimemovq %rax, 176(%rbp)movq 176(%rbp), %raxmovl 20(%rax), %eaxaddl $1900, %eaxmovl %eax, 172(%rbp)movq 176(%rbp), %raxmovl 16(%rax), %eaxaddl $1, %eaxmovl %eax, 168(%rbp)movq 176(%rbp), %raxmovl 12(%rax), %eaxmovl %eax, 164(%rbp)movq 176(%rbp), %raxmovl 24(%rax), %eaxmovl %eax, 160(%rbp)movq 176(%rbp), %raxmovl 28(%rax), %eaxaddl $1, %eaxmovl %eax, 156(%rbp)movq 176(%rbp), %raxmovl 8(%rax), %eaxmovl %eax, 152(%rbp)movq 176(%rbp), %raxmovl 4(%rax), %eaxmovl %eax, 148(%rbp)movq 176(%rbp), %raxmovl (%rax), %eaxmovl %eax, 144(%rbp)movl 172(%rbp), %edxmovslq %edx, %raximulq $1374389535, %rax, %raxshrq $32, %raxsarl $7, %eaxmovl %edx, %ecxsarl $31, %ecxsubl %ecx, %eaximull $400, %eax, %ecxmovl %edx, %eaxsubl %ecx, %eaxtestl %eax, %eaxjne .L8movl $366, 252(%rbp)jmp .L9.L8:movl 172(%rbp), %edxmovslq %edx, %raximulq $1374389535, %rax, %raxshrq $32, %raxsarl $5, %eaxmovl %edx, %ecxsarl $31, %ecxsubl %ecx, %eaximull $100, %eax, %ecxmovl %edx, %eaxsubl %ecx, %eaxtestl %eax, %eaxjne .L10movl $365, 252(%rbp)jmp .L9.L10:movl 172(%rbp), %eaxandl $3, %eaxtestl %eax, %eaxjne .L11movl $366, 252(%rbp)jmp .L9.L11:movl $365, 252(%rbp).L9:movl 156(%rbp), %eaxsubl $1, %eaxpxor %xmm1, %xmm1cvtsi2sdl %eax, %xmm1pxor %xmm0, %xmm0cvtsi2sdl 152(%rbp), %xmm0movsd .LC4(%rip), %xmm2divsd %xmm2, %xmm0addsd %xmm0, %xmm1pxor %xmm0, %xmm0cvtsi2sdl 148(%rbp), %xmm0movsd .LC5(%rip), %xmm2divsd %xmm2, %xmm0addsd %xmm0, %xmm1pxor %xmm0, %xmm0cvtsi2sdl 144(%rbp), %xmm0movsd .LC6(%rip), %xmm2divsd %xmm2, %xmm0addsd %xmm1, %xmm0movsd %xmm0, 136(%rbp)movsd 136(%rbp), %xmm1movsd .LC7(%rip), %xmm0mulsd %xmm1, %xmm0pxor %xmm1, %xmm1cvtsi2sdl 252(%rbp), %xmm1divsd %xmm1, %xmm0movsd %xmm0, 128(%rbp)movl 252(%rbp), %eaxaddl $1, %eaxsubl 156(%rbp), %eaxmovl %eax, 124(%rbp)movsd .LC7(%rip), %xmm0subsd 128(%rbp), %xmm0movsd %xmm0, 112(%rbp)movl 172(%rbp), %eaxmovl %eax, %edxleaq .LC8(%rip), %rcxcall printfleaq 80(%rbp), %rdxmovl 168(%rbp), %eaxmovq %rdx, %r8movl %eax, %edxleaq .LC9(%rip), %rcxcall printfmovl 164(%rbp), %eaxmovl %eax, %edxleaq .LC10(%rip), %rcxcall printfleaq 32(%rbp), %rdxmovl 160(%rbp), %eaxmovq %rdx, %r8movl %eax, %edxleaq .LC11(%rip), %rcxcall printfmovl 156(%rbp), %eaxmovl %eax, %edxleaq .LC12(%rip), %rcxcall printfmovl 124(%rbp), %eaxmovl %eax, %edxleaq .LC13(%rip), %rcxcall printfmovsd 128(%rbp), %xmm0movq 128(%rbp), %raxmovapd %xmm0, %xmm1movq %rax, %rdxleaq .LC14(%rip), %rcxcall printfmovsd 112(%rbp), %xmm0movq 112(%rbp), %raxmovapd %xmm0, %xmm1movq %rax, %rdxleaq .LC15(%rip), %rcxcall printfmovl 152(%rbp), %eaxmovl %eax, %edxleaq .LC16(%rip), %rcxcall printfmovl 148(%rbp), %eaxmovl %eax, %edxleaq .LC17(%rip), %rcxcall printfmovl 144(%rbp), %eaxmovl %eax, %edxleaq .LC18(%rip), %rcxcall printfleaq .LC19(%rip), %rcxcall printfleaq 80(%rbp), %r8movl 164(%rbp), %ecxleaq 32(%rbp), %raxmovl 172(%rbp), %edxmovl %edx, 32(%rsp)movq %r8, %r9movl %ecx, %r8dmovq %rax, %rdxleaq .LC20(%rip), %rcxcall printfmovsd 128(%rbp), %xmm0movq 128(%rbp), %rdxmovl 156(%rbp), %eaxmovapd %xmm0, %xmm2movq %rdx, %r8movl %eax, %edxleaq .LC21(%rip), %rcxcall printfleaq -64(%rbp), %raxmovq %rax, %rdxleaq .LC22(%rip), %rcxcall printfmovl $0, %eaxaddq $384, %rsppopq %rbpret.seh_endproc.section .rdata,"dr".align 8.LC4:.long 0.long 1077411840.align 8.LC5:.long 0.long 1083604992.align 8.LC6:.long 0.long 1089804288.align 8.LC7:.long 0.long 1079574528.ident "GCC: (Rev5, Built by MSYS2 project) 10.3.0".def __mingw_vfprintf; .scl 2; .type 32; .endef.def strftime; .scl 2; .type 32; .endef
Dan masih banyak lagi Program lainnya dalam Bahasa C yang dapat diubah ke Bahasa Assembly.
Itulah Tutorial Konversi dari Bahasa C ke Assembly dengan GCC yang telah kita bahas sekarang. Mohon maaf apabila ada sedikit kesalahan dalam penulisan Kode Program sedikitpun. Dan Untuk mengubah dari Bahasa C ke Assembly, silakan klik di sini (Ctoassembly.com).
Terima Kasih šššš :)
Wassalamu‘alaikum wr. wb.