Assemly trên Linux

Standard

Assembly programming is a little different. There are many assembly languages, but for Linux, NASM or GAS is usually used. Use this to install both:

sudo aptitude install nasm build-essential

I will use NASM for this page. To write an assembly program, you will first need to write the code, then assemble and link it. To begin, enter this code into a file names “hw.asm”:

strData: db "Hello World!", 10

global _start

_start:

   mov eax, 4
   mov ebx, 1
   mov ecx, strData
   mov edx, 13
   int 0x80

   mov eax, 1
   int 0x80

To compile, open a terminal, and use these commands:

nasm hw.asm -f elf

This will assemble hw.asm and name the ouput “hw.o”.

To link, use:

ld hw.o -o hw

To run the program, type its name in the terminal:

./hw

The words “Hello World!” will be put to the screen.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s