Hello World (Assembly Intel x86 Linux)

From LiteratePrograms
Jump to: navigation, search
Other implementations: Ada | ALGOL 68 | Alice ML | Amiga E | Applescript | AspectJ | Assembly Intel x86 Linux | Assembly Intel x86 NetBSD | AWK | bash | BASIC | Batch files | C | C, Cairo | C, Xlib | Candle | Clojure | C++ | C# | Delphi | Dylan | E | Eiffel | Erlang | Forth | FORTRAN | Fortress | Go | Groovy | Haskell | Hume | IBM PC bootstrap | Inform 7 | Java | Java, Swing | JavaScript | LaTeX | Lisp | Logo | Lua | Maple | MATLAB | Mercury | OCaml/F Sharp | occam | Oz | Pascal | Perl | Perl6 | PHP | Pic | PIR | PLI | PostScript | Prolog | Python | Rexx | Ruby | Scala | Scheme | Seed7 | sh | Smalltalk | SQL | Standard ML | SVG | Tcl | Tcl Tk | Visual Basic | Visual Basic .NET | XSL

This is the traditional "Hello World!" program in NASM for Linux.

[edit] Data segment

We use the .data section to store the string and name it msg.

<<hello_world.asm>>=

section .data
msg	db	"Hello World!",0x0a

This code calculates the length of msg, and stores it in len.

<<hello_world.asm>>=
len	equ	$-msg

[edit] Text segment

The .text section contains the actual code. _start is the entry point.

<<hello_world.asm>>=

section .text
	global _start

_start:

We use the write system call to output the text string on standard output. This call takes 3 arguments: file descriptor, pointer to buffer, length of buffer. The registers ebx, ecx and edx are used to store these arguments. We store the system call number 4 for write in eax. System calls are invoked with int 0x80.

<<hello_world.asm>>=

	mov	ebx,0x01
	mov	ecx,msg
	mov	edx,len
	mov	eax,0x04
	int	0x80

When write is finished, we call exit, which has number 1, with argument 0. The exit call will never return.

<<hello_world.asm>>=
	mov	ebx,0x00
	mov	eax,0x01
	int	0x80

[edit] Run

This is how to compile and run this code:

nasm -f elf hello_world.asm
ld -s -o hello_world hello_world.o
./hello_world
Download code
hijacker
hijacker
hijacker
hijacker