“Hello World” with assembly language, nasm.
Recently, I’ve gotten interested in assembly language. The purpose is to understand programming more deeply. At this time, I decided to use nasm because it is easy to install. The environment is macOS.
Preparation
$ brew install nasm$ touch assemble.asm
The code
global _mainsection .text
_main: mov rax, 0x02000004
mov rdi, 1
mov rsi, message
mov rdx, 13
syscall
mov rax, 0x02000001
xor rdi, rdi
syscallsection .data
message: db "Hello, World", 10
Output
$ nasm -fmacho64 assemble.asm
$ ld assemble.o -lSystem -o assemble
$ ./assemble
Hello, World
Explanation
・global: this file’s code can be used from other object files.
・main: entry point
・section .text: machine language is written in this section.
・mov: copy content A to content B (ex. mov contentB contentA)
・syscall: make kernel execute the written code
・xor: the exclusive OR. It is written in order to initialize rdi.
・rax: a register for system call number. 0x02000004 is “write”. 0x02000001 is “read”
・rdi: a register for file descriptor. 1 is standard output.
・rsi: a register for reading an address for message.
・rdx: a register for byte of an output object.
・section .data: used for declaring initialized data or constants
・db: 8 bytes
・10: means new line in ASCII