Micro işlemci dersine gittiğimde bu işin linuxta bu kadar zor olduğunu düşünmemiştim ve aldığımız micro dersinin de yetersiz olduğun gördüm günün ilerleyen saatlerinde, idelerimde de böyle birşey olmayınca bende biraz araştırmayla konsoldan assembly derlemek için kodları araştırdım.
Başlamadan önce assembly derlemek için nasm paketini kumamız gerekiyor:
sudo apt-get install nasm
hello world yazan bir assembly örneğini derleyeceğiz. Öncelikle hello.asm adında bir belge oluşturalım:

Daha sonra içerisine assembly kodlarımızı yerleştirelim
section .data
hello: db 'Hello world!',10 ; 'Hello world!' plus a linefeed character
helloLen: equ $-hello ; Length of the 'Hello world!' string
; (I'll explain soon)
section .text
global _start
_start:
mov eax,4 ; The system call for write (sys_write)
mov ebx,1 ; File descriptor 1 - standard output
mov ecx,hello ; Put the offset of hello in ecx
mov edx,helloLen ; helloLen is a constant, so we don't need to say
; mov edx,[helloLen] to get it's actual value
int 80h ; Call the kernel
mov eax,1 ; The system call for exit (sys_exit)
mov ebx,0 ; Exit with return code of 0 (no error)
int 80h
Bu kısmdan sonra
nasm -f elf hello.asm
ikinci olarak
ld -s -o hello hello.o
bu komutu kullanıdğımda şu hata ile karşılaştım galiba nasm paketi uyumlu değil
ld: i386 architecture of input file `hello.o' is incompatible with i386:x86-64 output
bu hatayıda şu komutla çözdüm:
ld -m elf_i386 -s -o hello hello.o
oluşturdu dosyalar bu şekilde olacak:

Bu komuttan sonra bize hello adında bir çalıştıralabilir dosya oluşturacak konsoldan
./hello
Dediğinizde programınız çalışacaktır.

One thought on “Linux konsoldan assembly derleme”
Comments are closed.