Hello World (Assembly Intel x86 NetBSD)

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 | 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 NetBSD.

Contents

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

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

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

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
Views
Personal tools