Memakai nasm Di Linux
02 November 2011 at 5:41 PM Tinggalkan Komentar
Pada tulisan sebelumnya yang berjudul Assembly di Linux Dengan GAS, saya memperlihatkan penggunaan GNU Assembler di Linux. GNU Assembler (GAS) hampir tersedia di kebanyakan instalasi Linux, sehingga programmer tinggal memakainya saja. Tapi untuk memakai GAS, seseorang harus mempelajari assembly dengan syntax AT&T. Bagi yang terbiasa dengan syntax Intel, pada awalnya mungkin akan sering mengalami sindrom “operand tertukar” (hal ini karena letak operand di syntax AT&T terbalik dengan yang ada di dokumentasi Intel). Sebagai contoh, perhatikan syntax Intel berikut:
mov al, bl
Baris di atas akan memintahkan isi register BL ke register AL. Untuk melakukan hal yang sama pada syntax AT&T, programmer harus memberikan perintah seperti:
movb %bl, %al
Urutan operand yang terbalik seperti ini bisa jadi membingungkan bagi programmer yang sudah terbiasa memakai syntax Intel.
Salah satu solusinya adalah dengan meng-install Netwide Assembler (NASM) yang sangat populer di Linux. Source NASM yang terbaru pada tulisan ini dibuat dapat di-download di situs resmi NASM. Setelah men-download dan men-extract source NASM, kerjakan script configure. Berikan perintah make dan make install untuk meng-install NASM pada lokasi default.
Untuk melihat dukungan format NASM, berikan perintah seperti berikut:
$ nasm -hf ... valid output formats for -f are (`*' denotes default): * bin flat-form binary files (e.g. DOS .COM, .SYS) ith Intel hex srec Motorola S-records aout Linux a.out object files aoutb NetBSD/FreeBSD a.out object files coff COFF (i386) object files (e.g. DJGPP for DOS) elf32 ELF32 (i386) object files (e.g. Linux) elf64 ELF64 (x86_64) object files (e.g. Linux) as86 Linux as86 (bin86 version 0.3) object files obj MS-DOS 16-bit/32-bit OMF object files win32 Microsoft Win32 (i386) object files win64 Microsoft Win64 (x86-64) object files rdf Relocatable Dynamic Object File Format v2.0 ieee IEEE-695 (LADsoft variant) object file format macho32 NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files macho64 NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) object files dbg Trace of all info passed to output stage elf ELF (short name for ELF32) macho MACHO (short name for MACHO32) win WIN (short name for WIN32)
Pada Linux, format yang dipakai adalah elf. Perhatikan bahwa NASM mendukung format bin yang akan menghasilkan flat-form binary file (mirip seperti file COM di zaman DOS). Format bin seperti ini dapat dipakai untuk menghasilkan kode untuk bootloader dan berbagai keperluan lain dalam membuat sebuah sistem operasi baru.
Berikut ini adalah program yang sama seperti pada tulisan Assembly di Linux Dengan GAS, hanya saja kali ini memakai Intel syntax dan ditujukan untuk NASM:
section .data output_vendor: db `Vendor ID Prosesor adalah 'xxxxxxxxxxxx'\n` output_vendor_length equ $-output_vendor output_brand: db `Prosesor Brand String adalah 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'\n` output_brand_length equ $-output_brand output_no_brand: db `Tidak ada informasi Processor Brand String\n` output_no_brand_length equ $-output_no_brand section .text global _start _start: mov eax, 0 cpuid mov edi, output_vendor mov [edi+27], ebx mov [edi+31], edx mov [edi+35], ecx mov eax, 4 mov ebx, 1 mov ecx, output_vendor mov edx, output_vendor_length int 0x80 mov eax, 0x80000000 cpuid cmp eax, 0x80000004 jl no_brand mov eax, 0x80000002 cpuid mov edi, output_brand mov [edi+30], eax mov [edi+34], ebx mov [edi+38], ecx mov [edi+42], edx mov eax, 0x80000003 cpuid mov [edi+46], eax mov [edi+50], ebx mov [edi+54], ecx mov [edi+58], edx mov eax, 0x80000004 cpuid mov [edi+62], eax mov [edi+66], ebx mov [edi+70], ecx mov [edi+74], edx mov [edi+77], byte 0x20 mov eax, 4 mov ebx, 1 mov ecx, output_brand mov edx, output_brand_length int 0x80 jmp selesai no_brand: mov eax, 4 mov ebx, 1 mov ecx, output_no_brand mov edx, output_no_brand_length int 0x80 selesai: mov eax, 1 mov ebx, 0 int 0x80
Untuk menjalankan program tersebut, berikan perintah seperti berikut ini:
$ nasm -f elf cpuinfo.asm $ ld -o cpuinfo cpuinfo.o $ ./cpuinfo Vendor ID Prosesor adalah 'GenuineIntel' Prosesor Brand String adalah 'Pentium(R) Dual-Core CPU T4400 @ 2.20GHz '
Nilai option “-f elf” menunjukkan bahwa NASM akan menghasilkan output dalam format ELF yang dipakai oleh Linux. Pembuat program assembler yang terbiasa menggunakan IA-32 pun dapat tetap memakai Intel syntax di platform Linux.
Trackback this post | Subscribe to the comments via RSS Feed