phiral.net
Home

|=---------------------------=[ Assembly Examples ]=--------------------------=|
|=----------------------------------------------------------------------------=|

---[ Assembly examples for Windows, Linux and *BSD.

 
---[ Linux AT&T Syntax

1: .section .data
2: helloWorld:
3:   .ascii "Hello, World!", "\n"
4: .section .text
5: .globl _start
6: _start:
7:    nop
8:    movl $4, %eax
9:    movl $1, %ebx,
10:   movl $helloWorld, %ecx
11:   movl $14, %edx
12:   int $0x80
13:   movl $1, %eax
14:   movl $0, %ebx
15:   int $0x80

Assemble with:

as -o hello_att_linux.o hello_att_linux.s
ld -o hello_att_linux hello_att_linux.o

./hello_att_linux

---[ Linux Intel Syntax

1:  section .data
2:  helloWorld db "Hello, World!", 0xa
3:  section .text
4:  globl _start
5:  _start: 
6:     nop
7:     mov eax, 4
8:     mov ebx, 1
9:     mov ecx, helloWorld
10:    mov edx, 14
11:    int 0x80
12:    mov eax, 1
13:    mov ebx, 0
14:    int 0x80

Assemble with:

nasm -f elf hello_intel_linux.asm
ld -s -o hello_intel_linux hello_intel_linux.o
./hello_intel_linux

---[ BSD Intel Syntax

1: section .data
2: helloWorld db "Hello, World!", 0xa
3: section .text
4: global _start
5: _start:
6:    push dword 14
7:    push dword helloWorld
8:    push dword 1
9:    mov eax, 4
10:   push dword eax
11:   int 80h
12:   add esp, 16
13:   push dword 0
14:   mov eax, 1
15:   push dword eax
16:   int 80h

Assemble with:

nasm -f elf hello_intel_bsd.asm
ld -s -o hello_intel_bsd hello_intel_bsd.o
./hello_intel_bsd

Note on the seemingly extra "push dword 0" [10], [15]. Since we are not using
a "call" instruction and we are using the 'C' calling convention we must push 
an extra register/dword [%eax] onto the stack to compensate for the missing pushed return 
address that would have been there had we used "call".


---[ BSD AT&T Syntax

1: .section .data
2: helloWorld:
3:   .ascii "Hello, World!", "\n"
4: .section .text
5: .global _start
6: _start:
7:    pushl $14
8:    pushl $helloWorld
9:    pushl $1
10:   movl $4, %eax
11:   pushl %eax
12:   int $0x80
13:   addl $16, %esp
14:   pushl $0
15:   movl $1, %eax
16:   pushl %eax
17:   int $0x80

Assemble with:

as -o hello_att_bsd.o hello_att_bsd.s
ld -o hello_att_bsd hello_att_bsd.o

./hello_att_bsd

Note on seemingly extra "pushl %eax" [11], [16]. Since we are not using a 
"call" instruction and we are using the 'C' calling convention we must 
push an extra register/dword [%eax] onto the stack to compensate for the missing pushed 
return address that would have been there had we used "call".

---[ Windows 16Bit Intel Syntax

1:  .model small
2:  .stack 100h
3:  .data
4:  helloWorld db 'Hello, World!', 0dh, 0ah, '$'
5:  .code
6:  main proc
7:     mov ax, @data
8:     mov ds, ax
9:     mov ah, 09h
10:    mov dx, offset helloWorld
11:    int 21h
12:    mov ax, 4c00h
13:    int 21h
14: main endp
15: end main

Compile with:

tasm hello_win16
tlink hello_win16
hello_win16