Basic Linux Assembly Tutorial

Pre-requirements: running Linux,have installed NASM and ld. Have a text editor you like

Assembly is the most important language any low level developer can learn. Once you understand assembly you can write higher level ends in other languages but there is no plausible alternative for assembly if you are working outside of an established kernel.

Assembly comments are written as “;this is a comment”. On a line anything after “;” is ignored by the assembler.

Here is a basic program that prints “Hello World!” to the console.

Place the following code into helloworld.asm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
section .data
	message: db 'Hello World!',10	
	messageLength: equ $-message  
 
section .text
	global _start	
 
_start:
	mov eax,4
	mov ebx,1   
	mov ecx,message   
	mov edx,messageLength 
 
	int 80h
 
	mov eax,1
	mov ebx,0 
	int 80h

The “mov” instruction works by taking the parameter on the right and putting it into the variable on the left. “int 80h” is just Linux’s way of telling the CPU you have something important to ask it. Whatever value is in eax tells the CPU what function you want to execute. In this case 1 is the print function. The register ebx tells the kernel to call the write function to the standard output which is the console.

To make the assembly code an executable now, you must first assemble it into an object file which is the format used to translate source code from all languages into a common middle ground.

nasm -f elf helloworld.asm

With an object file created the last step is now to create an executable. The linker takes this object file and checks it to make sure all the references are usable on the system.

ld -s -o helloworld helloworld.o

To run the program

./helloworld

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

*
What is 12 + 7 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human)

WordPress Themes