View
272
Download
0
Category
Preview:
Citation preview
8/2/2019 Assembly Language Programming in NASM
1/23
ASSEMBLY LANGUAGEPROGRAMMING INNASM
8/2/2019 Assembly Language Programming in NASM
2/23
Why assembly?- Assembly is widely used in industry:
- Embedded systems.
- Real time systems.
- Low level and direct access tohardware
8/2/2019 Assembly Language Programming in NASM
3/23
Our First NASM Program
8/2/2019 Assembly Language Programming in NASM
4/23
Running Our Code in NASM
To assemble a file, you issue a command of the form
> nasm -f
:Example
> nasm -f elf hello.asm
It would create hello.o file that has elf format (executable and linkable format).
To crreate an executable file hello from hello.o
> gcc hello.o -o hello
In order to run it you should write its name on the command line:
> ./hello
Which shows you the output of the program:
> Hello world!
8/2/2019 Assembly Language Programming in NASM
5/23
Basic Data Types
word
7 015 8
doublewordlow word
31 16 15 0
high word
quadwordlow doubleword
63 32 31 0
high doubleword
byte
7 0
8/2/2019 Assembly Language Programming in NASM
6/23
The Parts of an Assembly Program The .data section
This section is for "declaring initialized data", in other words defining "variables" that
already contain stuff. However this data does not change at runtime so they're not really
variables. The .data section is used for things like filenames and buffer sizes, and you can
also define constants using the EQU instruction. Here you can use the DB, DW, DD, DQinstructions. For example:
section .data
message: db 'Hello world!' ; Declare message to contain the bytes 'Hello world!' (without quotes)
msglength: equ 12 ; Declare msglength to have the constant value 12buffersize: dw 1024 ; Declare buffersize to be a word containing 1024
8/2/2019 Assembly Language Programming in NASM
7/23
The Parts of an Assembly Program The .bss section
This section is where you declare uninitialized variables. You use the RESB,
RESW, RESD, RESQ instructions to reserve uninitialized space in memory for
your variables, like this:
section .bss
filename: resb 255 ; Reserve 255 bytes
number: resb 1 ; Reserve 1 byte
bignum: resw 1 ; Reserve 1 word (1 word = 2 bytes)
realarray: resq 10 ; Reserve an array of 10 quads
8/2/2019 Assembly Language Programming in NASM
8/23
The Parts of an Assembly Program The .text section
This is where the actual assembly code is written. The .text section must beginwith the declaration of a global symbol, like global main, which just tells thekernel where the program execution begins. (It's like the main function in C or
Java, only it's not a function, just a starting point.) Eg.:
section .text
global main
main:mov eax, 4 ; Here is the where the program actually begins . .
8/2/2019 Assembly Language Programming in NASM
9/23
CPU Registers (32 bit)
base pointer register
esi
edi
esp
ebp
stack pointer register
eax
ebx
ecx
edx
31 0Accumulator register
Base register
Counter register
Data register
source index register
destination index register
8/2/2019 Assembly Language Programming in NASM
10/23
CPU Registers (16 bit)
esi
edi
esp
ebp
si
di
sp
bp
bx
cx
dx
eax
ebx
ecx
edx
ax
31 16 15 0
The least significant 16-bits ofthese registers have anadditional register name that
can be used for accessing justthose 16-bits.
Note: There are no registernames for the most significant
16-bits
8/2/2019 Assembly Language Programming in NASM
11/23
CPU Registers (8 bit)
ax
bx
cx
dx
eax
ebx
ecx
edx
31 16 15 0
bh bl
ch cl
dh dl
ah al
15 8 7 0
The 2 least significant bytes of registers eax, ebx, ecx and edx also have registernames, that can be used for accessing those bytes.
Note: There are no register names for accessing the 2 least significant bytes ofesi, edi, esp, ebp.
8/2/2019 Assembly Language Programming in NASM
12/23
Basic Assembly Instructions
Each NASM standard source line contains a combination of the 4 fields:
comment;operands(pseudo) instruction:label
optional fields
Either required or forbidden
by an instruction
:Notes
1. backslash (\) uses as the line continuation character: if a line ends withbackslash, the next line is considered to be a part of the backslash-ended line.
2. no restrictions on white space within a line.3. a colon after a label is optional.
:Examples
1. mov ax, 2 ; moves constant 2 to the register ax2. buffer: resb 64 ; reserves 64 bytes
8/2/2019 Assembly Language Programming in NASM
13/23
A typical instruction has 2 operands.
The left operand is the target operand, while the right operand is the source operand
3 kinds of operands exists:
1. Immediate, i.e. a value mov eax, 5
2. Register, such as AX,EBP,DL mov eax, ebx
3. Memory location; a variable or a pointer. mov eax, [var]
One should notice that the x86 processor does not allow
both operands be memory locations.
mov [var1],[var2]
Instruction arguments
8/2/2019 Assembly Language Programming in NASM
14/23
Calling C functions from NASM
8/2/2019 Assembly Language Programming in NASM
15/23
Calling C functions from NASM
8/2/2019 Assembly Language Programming in NASM
16/23
Looping in NASM
8/2/2019 Assembly Language Programming in NASM
17/23
Looping in NASM
8/2/2019 Assembly Language Programming in NASM
18/23
Macros in NASM
8/2/2019 Assembly Language Programming in NASM
19/23
Macros in NASM
8/2/2019 Assembly Language Programming in NASM
20/23
Including another file in NASM
8/2/2019 Assembly Language Programming in NASM
21/23
Including another file in NASM
8/2/2019 Assembly Language Programming in NASM
22/23
Including another file in NASM
8/2/2019 Assembly Language Programming in NASM
23/23
Recommended