1 Introduction
1 Introduction
Assembly language
programming
Requirements
Assembler: Netwide assembler (NASM)
An assembler for the Intel x86 architecture.
It can be used in both Linux and Windows
It can be download from the internet
We will assume the IA-32 instruction set (Intel Architecture, 32-bit)
Parts of an assembly language program
Data section/segment: For declaring variables/constants that are
initialised
Specified using the instruction section .data
BSS section/segment: used for declaring variables that are not
initialised and allocating memory to them
It is specified using the instruction section .bss
text section/segment: contains the actual code.
Specified using the instruction section .text
It must begin with the instruction global _start which tell the
kernel where the program execution begins
The semicolon (;) is used to signal the start of a comment. Comments
can be on their own line or after an instruction
Assembly language statements
Generally, there are three types of statements
Instructions: An instruction is specified by an opcode. Each opcode
generates one machine instruction
Assembler directives (pseudo-ops): These tell the assembler what to
do during the assembly process
Macros: These are a text substitution mechanism. One macro can
result into one or several instructions.
Assembly language statements syntax
Instructions are usually entered one per line
The follow the format
[label] opcode [operands] [;comment]
Examples
INC i ; increments the variable i by one
MOV [total], 48 ; transfer 48 to the variable total
Hello Kenya program
section .text
global _start ;must be declared for linker (ld)
section .data
msg db 'Hello Kenya!', 0xa ;string to be printed
len equ $ - msg ;length of the string
Assembling
To assemble the code, use the command below
> nasm -f elf hello.asm
Elf stands for executable and linkable format
The -f option is used to specify the output file format. Example
formats are elf32(elf), elf64, win32, win64,
After assembling, link using the command below
> ld -m elf_i386 -s hello.o -o hello
Then run the program by typing
>./hello