Hello World (Assembly Intel x86 NetBSD)

From LiteratePrograms
Jump to: navigation, search
Other implementations: Alice ML | Amiga E | Assembly Intel x86 Linux | Assembly Intel x86 NetBSD | AWK | bash | BASIC | Batch files | C | C, Cairo | C, Xlib | Candle | C++ | C# | Dylan | E | Forth | FORTRAN | Fortress | Go | Groovy | IBM PC bootstrap | Inform 7 | JavaScript | LaTeX | Logo | Lua | MATLAB | OCaml/F Sharp | occam | Oz | Perl | PHP | PIR | PLI | Prolog | Python | Rexx | Ruby | Scheme | sh | SQL | Standard ML | Tcl | Tcl Tk | Visual Basic

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

Contents

[edit] ELF note

NetBSD programs are identified by an ELF .note section.

<<hello_world.asm>>=
section .note.netbsd.ident
	dd	0x07,0x04,0x01
	db	"NetBSD",0x00,0x00
	dd	200000000

[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 last argument is pushed first, so that the first argument is on the top of the stack. Then the system call number (4) is stored in eax and pushed on the stack. System calls are invoked with int 0x80.

<<hello_world.asm>>=
	push	dword len
	push 	dword msg
	push	dword 0x01
	mov	eax,0x04
	push	dword eax
	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>>=

	push	dword 0x00
	mov	eax,0x01
	push	dword eax
	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