the ; eax register lea rdi, [message] ; Load the address of [message] ; into the rdi register push rcx ; push the value in the rcx ; register onto the top of the stack pop rcx ; pop the value on the top of the ; stack into the rcx register Data Transfer
the rax register mov rcx, 5 ; Load the value 5 into the rcx register add rax, rcx ; Add the value in rcx to the value in ; rax, and store the result in rax (17) mov rax, 12 ; Load the value 5 into the rax register mov rcx, 5 ; Load the value 12 into the rcx register sub rax, rcx ; Subtract the value in rcx from the value ; in rax, and store the result in rax (7) mov rax, 7 ; Load the value 7 into the rax register inc rax ; Increment the value in rax by 1 (8) mov rax, 7 ; Load the value 7 into the rax register dec rax ; Decrement the value in rax by 1 (6) Arithmetic
with the value 5 je label ; Jump to label if equal (==) jne label ; Jump to label if not equal (!=) jg label ; Jump to label if greater than (>) jng label ; Jump to label if not greater than (!>) jge label ; Jump to label if greater than or equal (>=) jnge label ; Jump to label if not greater than or equal (!>=) jl label ; Jump to label if less than (<) jnl label ; Jump to label if not less than (!<) jle label ; Jump to label if less than or equal (<=) jnle label ; Jump to label if not less than or equal (!<=) jz label ; Jump to label if zero (== 0) jnz label ; Jump to label if not zero (!= 0) Conditions
only system calls. Runs on 64-bit macOS only. ; To assemble and run: ; ; nasm -fmacho64 hello.asm && ld hello.o -static -o hello && ./hello ; ---------------------------------------------------------------------------------------- global start section .text start: mov rax, 0x02000004 ; system call for write mov rdi, 1 ; file handle 1 is stdout mov rsi, message ; address of string to output mov rdx, 13 ; number of bytes syscall ; invoke operating system to do the write mov rax, 0x02000001 ; system call for exit mov rdi, 0 ; exit code 0 syscall ; invoke operating system to exit section .data message: db "Hello, World", 10 ; note the newline at the end
library printf function. Runs on 64-bit macOS only. ; To assemble and run: ; ; nasm -fmacho64 numbers.asm ; ld -L /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -lc -o numbers numbers.o ; ./numbers ; ---------------------------------------------------------------------------------------- global _main extern _printf default rel section .text _main: mov rcx, 1 ; initialise the counter to 1 print_loop: ; Print the counter push rcx ; caller - save register lea rdi, [format] ; set 1st parameter (format) mov rsi, rcx ; set 2nd parameter (current number), call _printf ; call the C library printf function pop rcx ; restore caller-saved register ; Increment the counter and loop while <= 10 inc rcx ; increment the counter cmp rcx, 11 ; compare the counter value to 11 jne print_loop ; jump to print_loop if not equal ; Exit the program mov rax, 0 ; set the exit code ret section .data format: db "%d", 10, 0 ; note the newline at and NULL characters at the end