Introduction To Assembler
Introduction To Assembler
Table of Contents
The registers in the microprocessor that can be used by the programmer for, programming is called
General Purpose Registers. Other registers which is used by the microprocessor for its own internal tasks
are called Special Purpose Registers.
General purpose Register for the x86 family of processors are; AX, BX, CX, and DX. These registers are
16-bit wide while their 32-bit version is called; EAX, EBX, ECX and EDX. A 16-bit register is divided into
two 8-bit register called “high” and “low” portions. For register AX those portions are labeled as AH and
AL.
31 16 15 8 7 0
AH AL
AX
EAX
AX register Known as the Primary Accumulator is used for operations involving inputs/outputs and
arithmetic
BX register Known as the Base Register is used to hold the index in addressing. Can also be used in
computation.
CX register Known as Count Register. Used to control loops. Can also be used in computation.
There are several other registers such as Segment Registers and Index Registers.
SS
ssss
Stack
DS
Data Relocatable
CS
in Memory
Code
CS Offset
You may define any number of segments. To address a particular segment it is necessary to change the
value of the appropriate register. Following are the most common segments in a program.
Code Segment Contains the machine instructions that are to be executed. Code segment
address is stored in the Code Segment (CS) register.
Data Segment Contains the data defined in the programs, constants and data structures. The
Data Segment (DS) register addresses the data segment.
Stack Segment Contains any data and addresses that the program needs to save temporary or
used in sub routines. Stack Segment (SC) register holds the address.
Example 1: What is the actual memory address if the DS register contains 0x03E0 and the offset is
0x32?.
Instruction Pointer (IP) Is a 16-bit register that holds the offset address of the next instruction
to be executed. IP associate with the CS register and address is
indicated as CS:IP.
Stack Pointer (SP) Is a 16-bit register that holds the offset address which points to specific
location within the stack. When associated with the SS register (SS:SP) it
refers to the current word being processed in the stack.
Base Pointer (BP) Base Pointer facilitates referencing parameters, which are data and
addresses that a program passes via a stack. BP is combined with SS
register.
Example 2 : If CS value is 0x39B and IP is 0x514 what is the address of the next instruction to be
executed ?.
= 0x39B + 514
= 8AF
Other than above mentioned register there are other registers such as SI (Source Index), DI (Destination
Index) and Flag register. Flag Register is a 16-bit register that indicates states of various activities. It will
indicate the current status of the processor and the results of processing. Many instructions that do
arithmetic operations and computations do change the status of flags. Each bit in flag register
corresponds to some specific action.
O D I T S Z A P C
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2.1 Statement
An assembly program consists of set of statements which can be categorized as instructions and
directives. Instructions (examples: MOV, ADD) are the ones which are translated in object code.
Directives tell the assembler to perform specific action, such as define a data item.
Identifier, operation, operand and comment must be separated by at least one blank space or tab
character. Tab is the most recommended one. Square brackets indicate optional entries.
Comment Can be used by the programmer to keep notes within the program.
Sample statement:
ADD AX, BX ; AX AX + BX
Assembly Language instruction can be classified into several categories. Under each category many
instructions can be found. However for this course we will focus only on some of the very common
instructions.
Arithmetic Instructions
Logical Operations
AND Logic AND (assume BL has value AND BL, 09; 0001 0101 &&
0x5 = 101) 1001
MOV AX, BX ; AX BX
RCL Rotate Left through Carry (if BL is RCL BL, 1 ; BL = 01110000 &
10111000 and carry flag is 0) carry is 1
Comparison instruction
CMP Compare two values. Will set AF, CMP Ax, BX ; Compare AX & BX
CF, OF, PF, SF, and ZF flags. Used value
with conditional instructions.
Flag Operations
JA/JB Jump if Above / Jump if Below CMP BX, 5 ; compare BX & 0x05
JZ Jump if Zero
MUL BX ; AX AX * BX
Use of MUL and DIV instructions introduce lot of processing overhead. Multiplying or dividing by powers
of 2 (2, 4, 8, 16…2n) can be achieved by shifting bits with less processing overhead. This is shown in the
following example.
Left. (1100)= 12
Example 3: Write an assembly program to convert a given character from uppercase to lowercase or
vice versa.
A character represented in ASCII can be changed from lowercase to uppercase or vice versa by changing
the 5th bit.
A = 0 1 0 0 0 0 0 1 = 65 = 0x41
A= 0 1 1 0 0 0 0 1 = 97 = 0x61
Suppose we are putting the character into the AL register and the character to be changed is C = 67
0 1 0 0 0 0 1 1 = 67 = 0x43
DEC CX ; CX CX - 1
For displaying content to screen or reading input you have to use Software Interrupts.
You need an Assembler (Translating Program) like FASM (Flat Assembler) to translate the assembly
program into machine code.
2. The 8086 emulator is a good graphical emulator which can compile assembler code as well as
emulate the code execution.
Example 5: Write a program to display the term “Hello World” on screen using FASM (This is taken from
one of the examples in FASM);
The first two statements are the COM file start template to include for a Windows COM executable.
display_text = 9
mov dx,hello ;
hello db 'Hello world!',24h ; db stands for definite byte. In the data-segment it defines a
couple of bytes. These bytes contain the information between
the ‘ ‘. "hello" is a name to indentify this byte-string. It’s an
identifier. 24h is an interrupt saying to write string “Hello
World!” to screen.