Last active
May 18, 2025 16:18
-
-
Save danielsource/1f204c5e338be404425cf02c30ec0071 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; How to compile this file: | |
; $ nasm -felf64 -o hello.o hello-linux64.asm | |
; $ ld -o hello.out hello.o | |
; $ ./hello.out | |
; | |
; Elements of a NASM program: | |
; * labels | |
; * instructions | |
; * operands | |
; * directives | |
; * sections | |
; | |
; Some instructions: | |
; mov x,y (x <- y) | |
; and x,y (x <- x & y) | |
; or x,y (x <- x | y) | |
; xor x,y (x <- x ^ y) | |
; add x,y (x <- x + y) | |
; sub x,y (x <- x - y) | |
; inc x (x <- x + 1) | |
; dec x (x <- x - 1) | |
; syscall (trigger OS call: rax = syscall number, rdi/rsi/rdx/... = args) | |
; db (define bytes) | |
; equ (similar to a C '#define') | |
; | |
; Kinds of operands: | |
; * register (rdx, r2d, r2b, dh, ...) | |
; * memory ([750], [rcx + rsi*4], [rbx - 8], ...) | |
; * immediate (200, 0xc8, 0b1100_1000, ...) | |
; | |
; 64-bit reg | low 32-bits | low 16-bits | low 8-bits | 16-bit high half | |
; -----------|-------------|-------------|-------------|----------------- | |
; r0 aka rax | r0d aka eax | r0w aka ax | r0b aka al | ah | |
; r1 aka rcx | r1d aka ecx | ... | r1b aka cl | ch | |
; r2 aka rdx | ... | | r2b aka dl | dh | |
; r3 aka rbx | | | r3b aka bl | bh | |
; r4 aka rsp | | | r4b aka spl | | |
; r5 aka rbp | | | r5b aka bpl | | |
; r6 aka rsi | | | r6b aka sil | | |
; r7 aka rdi | r7d aka edi | r7w aka di | r7b aka dil | | |
; ... | ... | ... | ... | | |
; r15 | r15d | r15w | r15b | | |
; | |
; other regs | |
; ------------- | |
; rflags/eflags | |
; xmm{0-15} | |
; mmx... | |
; ymm... | |
; zmm... | |
; ... | |
global _start | |
STDIN equ 0 | |
STDOUT equ 1 | |
STDERR equ 2 | |
SYS_WRITE equ 1 | |
SYS_EXIT equ 60 | |
section .data | |
message db 'hello, world!',10 | |
message_len equ $-message | |
section .text | |
_start: | |
mov rax, SYS_WRITE | |
mov rdi, STDOUT | |
mov rsi, message | |
mov rdx, message_len | |
syscall | |
mov rdi, rax | |
mov rax, SYS_EXIT | |
syscall | |
; vi:ft=nasm: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; nasm -felf64 triangle.asm && ld triangle.o && ./a.out | |
global _start | |
STDOUT equ 1 | |
SYS_WRITE equ 1 | |
SYS_EXIT equ 60 | |
N equ 8 | |
section .data | |
chars db '*',10 | |
section .text | |
_start: | |
mov r9b, 1 | |
mov r8b, 0 | |
mov rdi, STDOUT | |
print_triangle: | |
mov rax, SYS_WRITE | |
mov rsi, chars | |
mov rdx, 1 | |
syscall | |
inc r8b | |
cmp r8b, r9b | |
jne print_triangle | |
mov rax, SYS_WRITE | |
mov rsi, chars+1 | |
syscall | |
mov r8b, 0 | |
inc r9b | |
cmp r9b, N | |
jng print_triangle | |
mov rax, SYS_EXIT | |
mov rdi, 0 | |
syscall |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment