Write a 64-bit program in assembly that prints Hello, world in .asm
section .data
hello db 'Hello, world!',0
section .text
global _start
_start:
; write Hello, world! to stdout
mov eax, 4 ; system call number (sys_write)
mov ebx, 1 ; file descriptor (stdout)
mov ecx, hello ; pointer to the message
mov edx, 13 ; message length
int 0x80 ; interrupt to invoke syscall
; exit the program
mov eax, 1 ; system call number (sys_exit)
xor ebx, ebx ; exit code 0
int 0x80 ; interrupt to invoke syscall
This assembly program uses the Linux system call interface to print "Hello, world!" to the standard output and then exits. The explanation for each step is provided as comments in the code:
- Section .data: Declare a data section to store constants.
assembly
section .data
hello db 'Hello, world!',0
hello
is a null-terminated string containing the message to be printed.Section .text: Declare a text section where the main code resides.
assembly
section .text
global _start
_start
is the entry point for the program.Start of the code (_start):
assembly
_start:
- Write to stdout (sys_write):
assembly
mov eax, 4 ; system call number (sys_write)
mov ebx, 1 ; file descriptor (stdout)
mov ecx, hello ; pointer to the message
mov edx, 13 ; message length
int 0x80 ; interrupt to invoke syscall
Set up registers for the
write
system call:eax
contains the system call number (4
forwrite
).ebx
contains the file descriptor (1
forstdout
).ecx
contains the pointer to the message (hello
).edx
contains the message length (13
characters).- Trigger the system call using interrupt
0x80
.
Exit the program (sys_exit):
assembly
mov eax, 1 ; system call number (sys_exit)
xor ebx, ebx ; exit code 0
int 0x80 ; interrupt to invoke syscall
- Set up registers for the
exit
system call:eax
contains the system call number (1
forexit
).ebx
contains the exit code (0
).- Trigger the system call using interrupt
0x80
.