Assembly ٠٨٤٦٢١
Assembly ٠٨٤٦٢١
Assembly ٠٨٤٦٢١
All registers (AX, BX, CX, DX) are 16 bits register and consist of low part
and high part ex Ax as shown in the side
1
A segment as an area of memory that includes up to 64k bytes start with
address 0000h and end with address FFFFh. The segment size of 64KB
comes because the 8085 microprocessor could address maximum of 64k
byte of physical memory since it had only 16 pins of addressing line (216 =
64k). this limitation was carried to design 8086. Where 8085 has only 64kB
for all (code, data and stack). In 8086, there can be up to 64kB assigned to
each category also it has range of 1MB of memory with (20 address pins)
(1M= 220)
Then how to move 64kB to cover all 1MB of memory?
In 8086, there are three type of addresses, which they are:
1- Physical address: is the 20 bits address that actually put on the address
pin of the 8086 microprocessor and have a range of 00000H to FFFFFH
2- Offset address: is a location with a 64kB segment it gives the extra
location of data instruction inside the segment in a range of 0000H to
FFFFh
3- Logical address: it consist of segment value and offset address , it
written as segment: offset .for code segment the logical address consist
of CS (code segment) and IP(instruction pointer) show as CS:IP, and for
data segment we use (BX,DX,and SI) to hold the offset as DS:BX or DS:SI
or DS:DI . for stack we use (SP or BP) as SS:SP or SS:BP
2
How to write a code in assembly language?
A given assembly language program is a serial of statements which are ether assembly
instruction such as ADD,Mov ,…. Or statement called directives also called (pseudo
instructions) which give directions to the assembler about how it should translate the
assembly language instruction in to machine code.
❖ An assembly language instruction consists of four fields, which they are:
[label:] instruction [operands] [;comment] brackets [ ] indicate that field is
optional
❖ Label field allows the program to refer to a line of code by name.it must end
with colon :
❖ Instruction and operand(s) field together perform the real work of the program
and accomplish the tasks which the program was written such as ADD Al,Bl ,
MOV Ax,6763h ADD & MOV are the instruction and "Al,Bl" & "AX,6763" are
the operands the operand field is option because some instructions need two
operands like
MOV AX,BX Ax & BX are the operands AX (called Destination) and BX (called
Source)
In addition, anther instruction need only one operand such as INC CX the
operand is CX and other instruction don't need any operand such as HLT,CLC
❖ The commend field begins with; the assembler ignore comments. It's optional
but are highly recommended to make it easier for someone to read and
understand the program
❖ Directives: the directives don't generate any machine code and used only as
opposed to instructions which are translated in to machine code for cpu
execute
❖ Model definition: this directive select the size of memory model which they
are
1- Model small : use maximum of 64kB of memory for code and another 64kB
for data
2- Model medium : the data must fit in to 64kB ,but code can exceed 64kB
3- Model compact : the data can exceed 64kB,but code can't exceed 64kB
4- Model large : both data and code can exceed 64kB,but no single set of data
should exceed 64kB
5- Model huge : both code and data can exceed 64kB
❖ Segment definition
o .stack (marks the beginning of the stack segment)
o .data (define data here)
o .code (write instruction here)
❖ Data type :-
o DB (define Byte – 8-bit)
o DW (define word – 16- bit)
3
o DD (define Double Word – 32-bit)
o DUP (duplicate) to define a location of memory and give its initial
value as
Data1 = DB 6 DUP(0FFh) ; define 6 location with FFH and the first
location pointed by Data1
o EQU (equate) for defining constant ex: x EQU 5AH
Writing a program that adding two numbers
Exercise
4
Flag Register:
It’s a 16-bit register that contain
(status flags & control flags)
❖ Status flags
1- Cary Flag (CF) set to 1 if
there is a carry from d7 or
d15 out
2- Party Flag (PF) set to 1 if
the lower order byte has
an even number of 1's
3- Auxilary Flag (AF) set to 1 if there is a carry from d3 to d4
4- Zero Flag (ZF) set to 1 if the result is zero
5- Sign Flag (SF) set to 1 if the result is negative (must bit = 1)
6- Overflow Flag (OF) will explain later
❖ Control Flags
1- Trap Flag (TF) : if it set to 1 allow the program to single step meaning to
execute one instruction at a time
2- Interrupt Flag (IF) : this bit is set or cleared to enable or disable the
external interrupt requests
3- Direction Flag (DF) :use with string (to control the direction of string
operation)
Example
Show how the flag register is affected by adding 38H and 2FH
67 0110 0111
CF = 0 , ZF = 0 , AF = 1 , PF = 0 , SF = 0
CF = 0 , ZF = 0 , AF = 1 , PF = 0 , SF = 1
Exercise
Show how the flag register is affected by adding 80H and 80H
5
Singed Number & Arithmetic Operations: -
All data items that used so far have been unsigned number, meaning that the entire
8-bit or 16-bit operand used for magnitude. Many applications require singed data
Now there is a way to represent signed number (positive and negative) by the must
signification bit (MSB) is set aside for the sign (+ or -)
and the rest are used for the magnitude and it
represent 0 for positive and 1 for negative as shown
❖ Positive numbers:
The range of positive number for byte
operand from 0 to 127
0 0000 0000
1 0000 0001
2 0000 0010
127 0111 1111
❖ Negative numbers:
Negative number ranges from -1 to -128
-128 1000 0000
-127 1000 0001
-2 1111 1110
-1 1111 1111
For word size, signed number from D0 to D14 represent the magnitude and D15
represent the sign if D15 is 0 means
the number is positive and if D15 is 1
means negative number and this give
the range of -32768 to 32767
Overflow problem :
The overflow is happened if the
result of an operation on signed number is too large for the register . the cpu
indicate the problem by rising the OF flag but it is up to the programmer to take care
of it.
Example
96 0110 0000
MOV Al,96
+70 +0100 0110
Mov Bl,70
166 1010 0110
Add AL,BL
CF = 0 , ZF = 0 , AF = 0 , PF = 1 , SF = 1 , OF = 1
6
When the overflow flag (OF) is set to 1 at 8-bit operation?
1- If there is a carry from D6 to D7 but no carry out from D7 (CF = 0)
2- If there is a carry from D7 out (CF = 1) but no carry from D6 to D7
Overflow flag in 16-bit operations:
In a 16-bit operation OF set to 1 in either of two case
1- If there is a carry from D14 to D15 but no carry out from D15 (CF = 0)
2- If there is a carry from D15 out (CF = 1) but no carry from D14 to D15
Example
CF = 0 , ZF = 0 , AF = 1 , PF = 1 , SF = 1 , OF = 1
Subtraction:
The instruction of subtraction is SUB and SUB AX,BX means AX AX-BX
In subtraction, we take the 2's complement of the source operand and then add it
with destination.
Example
DE 1101 1110
CF = 1 , ZF = 0 , AF = 0 , PF = 1 , SF = 1 , OF = 0
There is no carry from the subtraction but CF = 1 means borrow
7
Array: -
How to add array of data using assembly?
Loop instruction is dealing with CX register as the counter and this instruction do
both of the following instruction
1- Dec XC
2- JNZ AB
Exercise
Write assembly program that store the summation of number from 1-5 like
1+2+3+4+5=15.
Homework:
1- Write assembly program that store the summation of
(0321H,21A3H,115AH,125AH).
2- Write assembly program that store the summation of Even numbers from 1-
10 like 2+4+6+8+10=30.
8
Stack:
Stack is a section of read/write memory (RAM) used by the CPU to store information
temporarily.
Register inside the CPU that point to stack:
1- SS (stack Segment) which point to the segment contain the stack
2- SP (Stack Pointer) point at the current memory location (the top of the stack).
It decrements as the data pushed and increment as the popped
Instruction that deal with stack are:
1- Push means push data to stack (store the data inside the stack)
2- Pop means pop data from stack (return the data from stack to CPU)
Push and Pop instruction always used with word register (16-bit) means
Push AX not push AL or Push AH
At push operation SP is decrement by two, and at Pop it increments twice
Example 1
Assume SP= 32F6H, Ax = 24B6H, DI = 85F9H and DX = 5F93H write the address
that SP point to and the contain of the stack after these push instruction
Push AX
Push DI
Push DX
9
Example 2
Assume SP 18FAH show the content of the stack and register after these pop
instruction
Pop CX
Pop DX
Pop BX
Addressing mode
It represents How CPU access the data
Type of addressing mode
1- Register addressing mode
In this addressing mode, the data is taking from one register to another register
means no access to memory in addressing mode.
Example
Mov BX, DX ; copy the content of Dx into BX
ADD AL, BH ; add the content of BL to content of AL
Note (the source and the destination register must match in size)
2- Immediate addressing mode
In this addressing mode, the source operand is constant
Example
Mov AX, 2550H ; move 2550H into AX
Note (immediate value must be only source it couldn’t be a destination like
MOV 2250H,AX ;wrong)
3- Direct addressing mode
In direct addressing mode Data is in memory location(s) and the address of the
data in memory come immediately after instruction.
10
The different between immediate addressing mode and direct addressing mode is
In immediate addressing, the operand its self-provided with the instruction,
whereas in direct addressing the address of the operand is provided like:
Mov Dl, [2400]; move the content of DS:2400H into DL
To calculate the physical address by shifting DS one digit left and then add 2400
Another way of direct address as shown by the example
.data
Data1 DB 55H; Data1 is the name of an address in memory
.code
MOV DL,Data1; this type is also Direct addressing mode
To find the physical address by shifting DS left one digit and then adding BX or SI
or DI to it.
5- Based relative addressing mode :-
In this type of addressing, base register (BX and BP) as well as displacement
value is used to calculate the effective address.
The default segments used for calculating physical address are DS for BX and SS
for BP
Example
MOV CX , [BX] + 10; move DS:BX +10 and DS:BX +10 +1 into CX
Physical address = DS(shift left) + BX +10
We can also write MOV CX , [BX] + 10 as MOV CX , [BX + 10] or MOV CX ,
10[BX]
Example for BP:-
MOV A L, [BP] + 5; move SS: BP +5 into A L
Physical address = SS(shift left) + BP +5
6- Indexed relative addressing mode :-
The Indexed relative addressing mode work as Based relative except the register
(SI and DI) hold the offset address
Example:
MOV DX, [SI] + 5; Physical address = DS(shift left) + SI +5
MOV CH, [DI] + 20; Physical address = DS(shift left) + DI +20
11
7- Based Indexed addressing mode :-
By combining based and indexed addressing mode a new addressing is derived
called the based indexed addressing mode
Example
Example
12
Multiplication and Division
❖ Multiplication
In Multiplication, we have to differ between signed and unsigned number, so
there is two type of instruction in multiplication, which they are
o MUL (for unsigned number)
o IMUL (for signed number)
In addition, we deal with
o MUL (byte * byte)
One of the operands must be in AL register and the second
operand be in memory location, register or register indirect, but
no immediate value the result will be in AX
Example
13
Byte PTR means take a byte that SI point to
Word PTR means take a word that SI point to
o MUL (word * word)
One operand must be in AX and result will be in AX & DX
Example
Example
14
How multiplication effect on flags?
SF,ZF,AF and PF don't effect and always zero
CF/OF in MUL operation if the must byte or word = 0 CF/OF = 0
Else the value of CF/OF = 1 (there is a carry and overflow)
In IMUL operation CF/OF = 0 if the must word or byte is extend of the sign of the
low word or byte else it takes 1
Example
1- If AX = 1 and BX = FFFFH what will be the result and CF/OF after
MUL BX
IMUL BX
Instruction Result in Result in Ax DX CF/OF
(decimal) (hexadecimal)
MUL BX
4294836225 FFFE0001 0001 FFFE 1
IMUL BX
1 00000001 0001 0000 0
❖ Division
There are also two instruction in division two number, which they are
o DIV (for unsigned number)
o IDIV (for signed number)
In addition, we have deal with
o Byte/byte
The number must be in AL, AH 0, the result will be in AL, reminder in AH
Note (The denominator can't be immediate but it can be in register or
memory)
15
Example
o Word/word
The number must be in AX, DX 0, the result will be in AX, reminder in DX
Example
o Word/byte
The number must be in AX, the result will be in Al, reminder in AH
16
Example
o Doubleword/word
The number must be in AX and DX, the result will be in AX, reminder in DX
Example
17
Flow control instruction
Conditional JUMP:
CMP instruction (compare):
This instruction used for compare between two numbers, and that by subtract the
source from the destination, but the result does not store in destination by effect
the flag register
In conditional jump the jump will execute if the condition is true
Conditional Jump instruction divide in to
❖ Signed jumps
Instruction Description Condition to jump
JG/JNLE Jump if greater or jump if not less ZF=0 & SF = OF
than or equal
JGE/JNL Jump if greater or equal or jump if SF = OF
not less than
JL/JNGE Jump if less than or jump of not SF != OF
greater than or equal
❖ Unsigned jumps
18
❖ Single flag jumps:
Instruction Description Condition to jump
JZ/JE Jump if zero or jump if equal ZF= 1
JO Jump if overflow OF = 1
JS Jump if sign SF = 1
Example
int main(){
int x = -5;
If (x<0) {
x =- x;
}
}
19
2- If…..then……else…….end if (if condition branch is true execute
true branch statements else execute false branch statements
..end if)
Int main(){
Int x = 10, y = -10, result;
If(x >= y)
result = 2*x + y;
Else
result = x2 - y;
}
20
3- Case (case expression)
value_1 : statement_1
value_2 : statement_2
value_n : statement_n
end_case)
Example
Write assembly code that checkup Ax value
Case_1 Ax > 0 ,put -1 in Bx register
Case_2 Ax = 0, put 0 in Bx register
Case_3 Ax < 0 , put 1 in Bx register
21
Input and output instructions
To enter an input from the keyboard or print an output on the screen using interrupt
instruction int 21h, but before call this instruction we put the number of the service
that you want in ah register. As shown in the following table: -
Example
Write assembly program that read a character from the keyboard then print it
in the next line
22
The output
23
4- Input a string from the keyboard
Putting 0Ah in ah
Declare a variable in the following scope
A db 10,?,6 dup(0)
1- 10 main the user can only enter at most 10 letters
2- ? the asci code of the number of letters that user enter
3- 6 is the number of the duplicated number and it initialized by 0
Then put the offset of the declared variable in Dx
Example Write assembly program that allow user to enter his name then print it in
the next line
24
String instruction
in dealing with string the registers that always used are SI and DI also DS and ES
such that SI point to the source operand and DI point to the destination operand
to generate the physical address always use SI as the offset of DS and DI as the offset
of ES
Direction flag (DF) is one control flags and it specify the direction of the string
DF is set to high or low in order to choose if DI and SI increment or decrement
Instruction used with direction flag are
1- CLD (clear the DF) means put 0 in DF and SI and DI increment automatically
2- STD (set the DF) means put 1 in DF and SI and DI decrement automatically
String instruction
❖ Moving String (MOVSB & MOVSW)
The operation of these instructions is to send a copy of the content of the
address that SI points to the address that DI point (ES:DI DS:SI)
Example 1- Write assembly code that copy the value of string1 which contain the
“assembly” to string2
25
The operation that is happened after execute MOVSB is
❖ [DI] [SI]
❖ INC SI (because DF = 0 by instruction CLD)
❖ INC DI (because DF = 0 by instruction CLD)
the deferent between MOVSB and MOVSW
MOVSB (move string byte) move only one byte and DI & SI increment or
decrement by 1
MOVSB (move string word) move two bytes and DI & SI increment or
decrement by 2
REP instruction is loop instruction that repeats the following instruction until CX = 0
26
❖ Storing String (STOSB & STOSW)
The operation of these instruction to send a copy of the value in AL to the
address that DI point to (ES:DI AL)
STOSW send a copy of the value in AX to the address that DI point to
(ES:DI AX)
Example Order the user to Enter a message then store it in the memory then print it in
the next line
27