Anda di halaman 1dari 23

ASSEMBLY LANGUAGE PROGRAMMING IN NASM

Why assembly?
-

Assembly is widely used in industry:


-

Embedded systems. Real time systems. Low level and direct access to hardware

Our First NASM Program

Running Our Code in NASM


To assemble a file, you issue a command of the form > nasm -f <format> <filename> 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!

Basic Data Types


7 0 byte

15

87

0 word

31 high word

16 15 low word

0 doubleword

63 high doubleword

32 31 low doubleword

0 quadword

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, DQ instructions. For example:
section .data message: db 'Hello world!' ; Declare message to contain the bytes 'Hello world!' (without quotes)

msglength: equ 12
buffersize: dw 1024

; Declare msglength to have the constant value 12


; Declare buffersize to be a word containing 1024

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 number: bignum: resb 1 resw 1 ; Reserve 255 bytes ; Reserve 1 byte ; Reserve 1 word (1 word = 2 bytes) ; Reserve an array of 10 quads

realarray: resq 10

The Parts of an Assembly Program

The .text section This is where the actual assembly code is written. The .text section must begin with the declaration of a global symbol, like global main, which just tells the kernel 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 . .

CPU Registers (32 bit)


31 0

eax ebx
ecx edx esi edi esp ebp

Accumulator register Base register Counter register

Data register
source index register destination index register stack pointer register base pointer register

CPU Registers (16 bit)


eax ebx ecx edx esi edi 31 16 15 ax bx cx 0

The least significant 16-bits of these registers have an additional register name that can be used for accessing just those 16-bits. Note: There are no register names for the most significant 16-bits

dx si
di

esp ebp

sp bp

CPU Registers (8 bit)


31 eax ebx ecx edx 16 15 0 15 ah bh ch dh 87 al bl cl dl 0

ax
bx cx

dx

The 2 least significant bytes of registers eax, ebx, ecx and edx also have register names, that can be used for accessing those bytes. Note: There are no register names for accessing the 2 least significant bytes of esi, edi, esp, ebp.

Basic Assembly Instructions


Each NASM standard source line contains a combination of the 4 fields: label: (pseudo) instruction operands ; comment

optional fields

Either required or forbidden by an instruction

Notes: 1. backslash (\) uses as the line continuation character: if a line ends with
backslash, 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 ax 2. buffer: resb 64 ; reserves 64 bytes

Instruction arguments
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. 2. 3. Immediate, i.e. a value Register, such as AX,EBP,DL Memory location; a variable or a pointer. mov eax, 5 mov eax, ebx mov eax, [var]

One should notice that the x86 processor does not allow both operands be memory locations.

mov [var1],[var2]

Calling C functions from NASM

Calling C functions from NASM

Looping in NASM

Looping in NASM

Macros in NASM

Macros in NASM

Including another file in NASM

Including another file in NASM

Including another file in NASM

Anda mungkin juga menyukai