In this second program we print the string "Hello, world!" to the console. Here is how it works:
%define ENDL 10
global _start
section .text
_start:
mov rax, 1
mov rdi, 1
mov rsi, msg_hello
mov rdx, 14
syscall
mov rax, 60
mov rdi, 0
syscall
section .data
msg_hello: db "Hello, world!", ENDL
Here is everything new we have added:
%define ENDL 10
: Defines a constant value 10 as ENDL (newline character). This is similar to the C/C++ preprocessor.mov rax, 1
: Moves the code for thewrite
syscall intorax
.mov rdi, 1
: The first parameter of the syscall is the file descriptor.1
corresponds to the standard output (stdout).mov rsi, 1
: The second parameter of the syscall is the pointer to the buffer to output. In this case the adress ofmsg_hello
.mov rdx, 14
: The second parameter of the syscall is the number of bytes to write. In this case14
for the string and theENDL
.section .data
: This indicates the beginning of the data section where variables and constants are placed.msg_hello: db "Hello, world", ENDL
: Defines a sequence of data bytes using thedb
directive with the string "Hello, world" followed by a newline character.
Commands that are executed by the assembler during the assembly process. They are not translated into machine code but are used to control the assembly process, organize the code, and define data. Some common directives include defining constants, allocating space for variables, and specifying the start of code or data sections.
%define ENDL 10
: Defines a constant value10
namedENDL
.section .text
: Indicates the beginning of the code section.global _start
: Makes the_start
label globally accessible.db "Hello, world"
: Defines a sequence of bytes (data).
Commands that are translated by the assembler into machine code that the CPU can execute. They perform actions such as moving data between registers, performing arithmetic operations, and controlling the flow of the program. Instructions are the actual operations that the CPU will execute at runtime.
mov rax, 1
: Moves the value1
into therax
register.add rax, rbx
: Adds the value inrbx
to the value inrax
and stores the result inrax
.syscall
: Makes a system call.jmp _label
: Jumps to the instruction at the label_label
.
Try printing "I'm learning assembly" instead.