A (saner, IMO) flavor of assembly.
section .data
str: db 'Hello world!', 0Ah ; message string with new-line char at the end (10 decimal)
str_len: equ $ - str ; calcs length of string (bytes) by subtracting the str's start address
section .text
global _start
_start:
;; mov a, b <===> a = b
mov ax, 1234h ; copies the value 0x1234 (4660) into register AX
mov bx, ax ; copies the value of the AX register into the BX register
add esp, 4 ; esp += 4
call func ; push next instruction address onto stack and jump to a function
; return value is stored in eax
push eax ; push the value of eax onto the stack; increments esp (by the size of eax)
pop eax ; pop a value off the stack into eax; decrements esp (by the size of eax)
;; arithmetic operations inside brackets
mov eax, [ebx + ecx*4] ; resolves to an address and then accessed
lea eax, [ebx + ecx*4] ; interpreted as a value and then stored into eax
test eax, eax ; performs eax & eax, and sets FLAGS accordingly
jmp 0x0000411e ; jump to 0x0000411e (I made up this address)
je 0x0000411e ; jump to 0x0000411e if eax = eax
jne 0x0000411e ; jump to 0x0000411e if eax != eax (won't ever happen)
;; example of Linux syscalls from wikipedia
mov eax, 4 ; specify the sys_write function code (from OS vector table)
mov ebx, 1 ; specify file descriptor stdout --in gnu/linux, everything's treated as a file,
; even hardware devices
mov ecx, str ; move start _address_ of string message to ecx register
mov edx, str_len ; move length of message (in bytes)
int 80h ; interrupt kernel to perform the system call we just set up -
; in gnu/linux services are requested through the kernel
mov eax, 1 ; specify sys_exit function code (from OS vector table)
mov ebx, 0 ; specify return code for OS (zero tells OS everything went fine)
int 80h ; interrupt kernel to perform system call (to exit)
func:
;; pop the return address off the stack and jump to that address
ret