Inhaltsverzeichnis

Die Syntax kann je nach verwendetem Assembler variieren. Es gibt mehrere verschiedene Assemblersprachen für x86-Prozessoren, darunter NASM (Netwide Assembler), GAS (GNU Assembler) und MASM (Microsoft Macro Assembler).

Obwohl es gewisse Gemeinsamkeiten gibt, unterscheiden sich die Syntaxe dieser Assembler in einigen Aspekten. Zum Beispiel verwenden NASM und GAS ein Intel-ähnliches Syntaxformat, bei dem Operanden üblicherweise in der Reihenfolge „Quelle, Ziel“ angegeben werden. MASM hingegen verwendet eine leicht andere Syntax, bei der die Operanden normalerweise in umgekehrter Reihenfolge angegeben werden, also „Ziel, Quelle“.

section .data
    message db 'Hallo, Welt!', 0
 
section .text
    global _start

Assembly ist im Grunde die erste Programmiersprache nach Bytecode (Maschinencode). inline assembly

Windows Emulatoren: EMU8086 SMSx86

Windows, NASM, GCC

; nasm -f elf code.asm -o code.o
; gcc code.o -o code.exe

global _main

extern _scanf
extern _strcmp
extern _strcat
extern _printf

section .data

stra db "a", 0
strb db "b", 0
fmtc db "%c", 0
fmts db "%s", 0
fmtd db "%d", 0

section .bss

input resb 1000

section .text

    _main:
    
    	push ebp
    	mov ebp, esp
    
    _begin:  
      
        ; scanf("%s", input)
        push input 
        push fmts
        call _scanf
        add esp, 8
        
        ; int i = strcmp(input, stra); printf("%d", i)
    	push stra
        push input
        call _strcmp
    	add esp, 8
        push eax
        push fmtd
        call _printf
        add esp, 8
        
        ; printf("%c", 10)
        push 10
        push fmtc
        call _printf
        add esp, 8
        
        ; strcat(input, strb)        
        push strb
        push input
        call _strcat
        add esp, 8
        
        ; printf("%s", input)        
        push input
        push fmts
        call _printf
        add esp, 8
    
    _end:
        
        ; clean up
    	mov esp, ebp
    	pop ebp
    	ret

Linux, NASM, GCC

; nasm -f elf code.asm -o code.o
; gcc code.o -o code

global main

extern printf
extern scanf
extern strcmp

SECTION .data

stra: db "a", 0
fmtc: db "%c", 0
fmtd: db "%d", 0
fmts: db "%s", 0

SECTION .bss

input resb 100

SECTION .text    

main:				

	; setup
	push ebp
	mov ebp, esp

	; printf("%s", stra)
	push stra
	push dword fmts
	call printf
	add esp, 8

	; printf("%c", 10)
	push 10
	push dword fmtc
	call printf
	add esp, 8

	; scanf("%s", input)
	push input
	push dword fmts
	call scanf
	add esp, 8

	; strcmp(stra, input)
	push input
	push stra
	call strcmp
	add esp, 8

	; int i = strcmp(input, stra); printf("%d", i)
	push eax
	push dword fmtd
	call printf
	add esp, 8

	; cleanup
        mov     esp, ebp	
        pop     ebp		

end:

	mov	eax,0 ; return 0	
	ret

Linux, NASM, LD

Linux mit SysCalls

; Compiling
; nasm -f elf32 code.asm -o code.o
; ld -m elf_i386 code.o -o code

global _start

section .bss

	buffer resb 10	; 10 byte buffer
	bufferlen equ $ - buffer

section .data

        prompt db "prompt> "	; , 0x0a
        promptlen equ $ - prompt

section .text

_start:

loop1:

        mov eax, 4	; sys_write
        mov ebx, 1 	; stdout
        mov ecx, prompt	; prompt
        mov edx, promptlen	; length predefined
        int 0x80

	mov eax, 3	; sys_read
	mov ebx, 0	; stdin
	mov ecx, buffer	; buffer
	mov edx, bufferlen	; length 10 bytes
	int 80h

	cmp eax, 0
	jle lpend1	; if read <= 0

	mov eax, 4	; sys_write
	mov ebx, 1	; stdout
	mov ecx, buffer	; buffer
	mov edx, bufferlen	; length
	int 80h

	jmp loop1

lpend1: 

	mov eax, 1	; sys_exit
	mov ebx, 0	; status
	int 80h

Windows, NASM, LD

Windos mit „system call instructions“

; Compiling
; nasm -f win32 code.asm -o code.o
; ld code.o -lkernel32 -o code.exe

global _start

extern _GetStdHandle@4
extern _WriteConsoleA@20
extern _ExitProcess@4

section .data

        str: db 'hello, world', 0xA, 0x0
        strLen: equ $ - str

section .bss

        numCharsWritten: resd 1

section .text

        _start:

        push    dword -11       ; Arg1: request handle for standard output
        call    _GetStdHandle@4 ; Result: in eax

        push    dword 0         ; Arg5: Unused so just use zero
        push    numCharsWritten ; Arg4: push pointer to numCharsWritten
        push    dword strLen    ; Arg3: push length of output string
        push    str             ; Arg2: push pointer to output string
        push    eax             ; Arg1: push handle returned from _GetStdHandle
        call    _WriteConsoleA@20

        push    dword 0         ; Arg1: push exit code
        call    _ExitProcess@4

https://www.swansontec.com/sregisters.html