Assembly - Basic Syntax
Like other programming languages, assembly language also has its own way of writing. To learn assembly on nasm assembler, the code must be written as per the proper syntax, otherwise the code will not run.
Basic Syntax format -
section.bss
section .text
global _start
_start: mov rax, 1 ; system call for write
mov rdi, 1 ; file handle 1 is stdout
section .data
message: db "Hello, World", 10 ; note the newline at the end
Syntax of assembly language statements-
[label] mnemonic [operands] [;comment]
Assembly Program Section
An assembly program can be divided into three sections – data section, bss section and text section.
- Data Section – The data section is used to declare initialized data or constants. This data does not change at runtime. You can declare various constant values, file names, or buffer sizes, etc. in this section.
The syntax for declaring a data section is - section.data -
BSS section - The BSS section is used to declare variables.
The syntax for declaring a bss section is - section.bss -
Text section - The text section is used to hold the actual code. This section must begin with the global _start declaration, which tells the kernel where to begin program execution.
The syntax for declaring a text section is - section.text
Comments - Assembly language comments begin with a semicolon (;). It can contain any printable character, including spaces. As - ; This program displays a message on the screen
Assembly language statement
There are three types of statements in an assembly language program: - Executable, pseudo-ops, Macros
- Executable Instructions - Executable instructions or simply instructions tell the processor what to do. Each instruction has an operation code (opcode). Each executable instruction generates one machine language instruction.
- Assembler instructions or pseudo-ops - Assembler instructions or pseudo-ops tell the assembler about various aspects of the assembly process. These are non-executable and do not generate machine language instructions.
- Macros - Macros are basically a text replacement mechanism.
Syntax of assembly language statements - [label] mnemonic [operands] [;comment]
A basic instruction has two parts, the first being the name of the instruction (or mnemonic) to be executed, and the second being the operands or parameters of the command.
Some examples of assembly language statements-
INC COUNT ; Increment the memory variable COUNT
MOV TOTAL, 54 ; Transfer the value 54 in the
; memory variable TOTAL
ADD AH, BH ; Add the content of the
; BH register into the AH register
AND MASK1, 114 ; Perform AND operation on the
; variable MASK1 and 114
ADD MARKS, 15 ; Add 15 to the variable MARKS
MOV AL, 12 ; Transfer the value 12 to the AL register
Hello world program in assembly
The following assembly language code displays the string 'hello world' on the screen −
section .text
global _start ;must be declared for linker (ld)
_start: ;tells linker entry point
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db 'Hello, world!', 0xa ;string to be printed
len equ $ - msg ;length of the string
When the above code is compiled and executed, it produces the following result −
Hello, world!
Compile, link, and run assembly programs in NASM.
Set the path to the nasm and ld binaries in your PATH environment variable. Then follow these steps to compile and link the above program −
- Type the above code using text editor and save it as - hello.asm.
- Make sure you are in the same directory where you saved hello.asm.
- To assemble the program,type nasm -f elf hello.asm.
- If there is any error, you will be prompted about that at this stage. Otherwise, an object file of your program named hello.o. will be created.
- To link the object file and create an executable file named hello, ld -m elf_i386 -s -o hello hello.o.
- Execute the program by typing ./hello.
If you have done everything correctly, it will display 'Hello, world!'. on the screen.