Introduction to Assembly Language HelloWorld

 2 minutes to read

HelloWorld in assembly language code show as below:

section .data
msg db "Hello World!",0ah
len equ $-msg
section .text
global _start
_start:
; Write string to standard output
mov eax,4 ;   system call No. 4 (sys_write)
mov ebx,1 ;   ebx sends 1 to indicate standard output file handle(stdout)
mov ecx,msg ; The first address of the string is sent to ecx
mov edx,$len; The length of the string is sent to edx
int 80h;     output string, call the kernel
;quit
mov eax,1;   No. 1 system call (sys_exit)
mov ebx,0; return 0 exit code
int 80h;     end, call the kernel

msg is a string label defined in the data segment (.data), and db stands for define byte. In other words, msg represents a byte array, and 0ah means decimal 10 is the ASCII code value of the newline character.

len is the label of the string length, equ means equivalent, $ is a variable indicating the current position, indicating the position of the byte after the last byte of the len byte array, $ minus msg (the first byte of the string array The position of a byte), which is exactly the length of the byte array, which is calculated by the assembler at compile time.

Compile under linux x86_64 nasm -f elf64 hello.asm connect ld -s -o hello hello.o Execute ./hello