A complete assembly language program is presented to demonstrate the appropriate program formatting.
A properly formatted assembly source file consists of several main parts:
Data section where initialized data is declared and defined.
BSS section where uninitialized data is declared.
Text section where code is placed
Comments
The semicolon (;) is used to note program comments. Comments (using the ;) may be placed anywhere, including after an instruction.
Any characters after the; are ignored by the assembler
Data Section
The initialized data must be declared in the "section .data" section. All initialized variables and constants are placed in this section.
Variable names must start with a letter, followed by letters or numbers, including some special characters (such as the underscore, "_"). variable definitions must include the name, the data type, and the initial value for the value for the variable.
The general format is:
<variableName> <dataType> <initialValue>
Refer to the following sections for a series of examples using various data types:
Declaration | |
db | 8-bit variable(s) |
dw | 16-bit ‘’ |
dq | 64-bit ‘’ |
ddq | 128bit ‘’ int |
dt | 128-bit ‘’ float |
BSS Section
Uninitialized data is declared in the "section .bss" section. All uninitialized variables are declared in this section.
Variable names start with a letter followed by letters or numbers including some special characters (such as the underscore, "_").
Variable definitions must include the name, the data type, and the count.
The general format is:
<variableName> <resType> <count>
The supported data types are as follows:
Declaration | |
resb | 8bit |
resw | 16bit |
resd | 32bit |
resq | 64bit |
resdq | 128bit |
Text Section
The code is placed in the "section .text" section. The instructions are specified one per line and each must be valid instruction with the appropriate required operands.
The text sections will include some headers or labels that define the initial program entry point. For, example, assuming a basic program using the standard system linker, the following declarations must be included.
global _start
_start:
No special label or directives are required to terminate the program.
Example Program
A very simple assembly language program is presented to demonstrate the appropriate program formatting:
; Simple example demonstrating basic program format and layout.
; Ed Jorgensen
; July 18, 2014
; ************************************************************
; Some basic data declarations
section .data
; -----
; Define constants
EXIT_SUCCESS equ 0
SYS_exit equ 60
; successful operation
; call code for terminate
; -----
; Byte (8-bit) variable declarations
bVar1 db 17
bVar2 db 9
bResult db 0
; -----
; Word (16-bit) variable declarations
wVar1 dw 17000
wVar2 dw 9000
wResult dw 0
; -----
; Double-word (32-bit) variable declarations
dVar1 dd 17000000
dVar2 dd 9000000
dResult dd 0
; -----
; Quadword (64-bit) variable declarations
qVar1 dq 170000000
qVar2 dq 90000000
qResult dq 0
; ************************************************************
; Code Section
section .text
global _start
_start:
;
;
; Performs a series of very basic addition operations
; to demonstrate basic program format.
;
; ----------
; Byte example
;
; bResult = bVar1 + bVar2
mov al, byte [bVar1]
add al, byte [bVar2]
mov byte [bResult], al
; ----------
; Word example
;
; wResult = wVar1 + wVar2
mov ax, word [wVar1]
add ax, word [wVar2]
mov word [wResult], ax
; ----------
; Double-word example
;
; dResult = dVar1 + dVar2
mov eax, dword [dVar1]
add eax, dword [dVar2]
mov dword [dResult], eax
Credits:
- x86-64 Assembly Language Programming with Ubuntu by Ed Jorgensen, Ph.D. May 2022