Files
assembly-hello-world/hello.asm
2025-10-13 20:47:17 -03:00

41 lines
2.1 KiB
NASM

; nah it's pretty easy.
; take a look in this hello world i made with assembly x86_64 on linux, i recommend to copy and paste
; it in vscode or other code editor to better understand it, use the .asm
; extension and compile it using NASM and GCC using the following commands:
; nasm -f elf64 hello.asm -o hello.o
; gcc hello.o -o hello -no-pie
; to run:
; ./hello
; i highly recommend the "ASM Code Lens" if you're using vscode.
; now here's the code with explanations:
global _start ; declaring the "main function"
section .rodata ; when "variables" is, rodata means that the variables will NOT change
msg: db "hello, assembly", 0xA ; msg is the variable name, "db" is define byte, which in our case is that string,
; 0xA is a hex representation for newline character (/n)
msglen: equ $ - msg ; here we calculate the string "msg" length, as equ being "equate" - that defines a constant,
;'$' being the location counter - is our current address, basically it means "how many bytes have pass since here"
section .text ; this basically says that the code the cpu will execute is above, .text means executable code (instructions)
_start: ; now this is the main function, here we want to make some syscalls to the OS, for print the message,
;in our case is for Linux, that follows the following parameter structure: syscall number (RAX),
;and take up to 6 arguments (RDI, RSI, RDX, R10, R8, R9 - yeah, the register R8 comes before R9),
;it's cool to know, but most of the time you'll only need the arguments required by the syscall
mov rax, 1 ; moving constant 1 to register RAX, it means the sys_write linux's syscall
mov rdi, 1 ; moving 1 to RDI, means that the file descriptor is stdout (default linux output)
;others include stdin (0, input) and stderr (2, error output) - this is the first argument
mov rsi, msg ; moving the string address (buffer) to rsi, second argument
mov rdx, msglen ; moving the string size to rdx, it says how many bytes msg will use
syscall ; finally, we call system.
; here has another syscall to exit program
mov rax, 60 ; 60th syscall: sys_exit
mov rdi, 0 ; exit status, 0 means "no errors"
syscall ; calling again