Assembly ٠٨٤٦٢١

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Assembly lab

Engineer: Ahlam Ali


Assembly Language
Assembly Language is a programming language that is very similar to machine
language,
but uses symbols instead of binary numbers. It is converted by the assembler into
executable machine-language programs.

8086 CPU registers


The 8086 contains 14 registers. Each register is 16 bits long. It divided to: -
❖ General Purpose Register (GPR)
1. Ax (Accumulator) used for arithmetic an logic expression and used for
input and output data also used in multiplication and division
expression
2. BX (Base Register) used for addressing the memory)
3. CX (Counter Register) used as a counter of loops
4. DX (Data Register) used in multiplication and division expression and
used for input and output data

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

❖ Index and Pointer Register


1. SI (source Index) pointer that point to the source data in the memory
2. DI (Destination Index) pointer that point to the location that the data
will store in
3. SP (Stack Pointer) pointer that is point at the current memory of stack
location (top of stack)
4. BP (Base Pointer) pointer that point at location in stack ,and can also
point to another segment
5. IP (instruction Pointer) it contain the address of the next instruction to
be execute
❖ Segment Registers (SR)
1. CS (Code Segment) points at the segment containing the code of the
current program
2. DS (Data Segment) generally points at segment when variable are
defined
3. SS (Stack Segment) point at the segment containing the stack
4. ES (Extra Segment) is a segment register used as an extra data segment

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

How to find the physical address?


The physical address for the location is generated by shifting the segment part left
one hex digit and then adding the offset to it
Example
If CS=25F6H and IP=635AH find
1- The logical address
2- The offset address
3- The physical address
4- The lower range
5- The upper range
The solution
1- The logical address is 25F6:635A
2- The offset address is 635AH
3- The physical address is 25F60 + 635A = 2C2BA
4- The lower range is 25F60 + 0000 = 25F60
5- The upper address is 25F60 + FFFF = 35F5F

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

White assembly program to add the numbers 435AH and A34BH

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

1- Mov BH,38H 38 0011 1000

Add BH,2FH +2F +0010 1111

67 0110 0111

CF = 0 , ZF = 0 , AF = 1 , PF = 0 , SF = 0

2- Mov AX,34F5H 34F5 0011 0100 1111 0101

Add AX,95EBH +95EB +1001 0101 1110 1011

CAE0 1100 1010 1110 0000

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

Mov AX,6E2FH 6E2F 0110 1110 0010 1111

Mov CX,13D4H +13D4 +0001 0011 1101 0100

Add CX,AX 8203 1000 0010 0000 0011

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

The 2's complement of 31H is


Mov AL,0FH
0011 0001 1100 1111 CF
SUB AL,31H
0F 0000 1111

+CF +1100 1111

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

4- register Indirect addressing mode:


in register indirect, the address of the memory location where the operand
inside is hold by a register ,and the register that is used for this purpose are:-
SI,DI and BX if these three register are used as pointer, they must be combined
with DS in order to generate the 20-bit physical address
Example
MOV AL, [BX] ; move into AL the contents of memory location DS:BX
MOV CL, [SI] ; move into AL the contents of memory location DS:SI
MOV DL, [DI] ; move into AL the contents of memory location DS:DI

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

MOV AL, [BX][SI] + 8; Physical address = DS(shift left) + BX + SI +8


MOV CH, [BP][DI] + 12; Physical address = SS(shift left) + BP + DI +12
Also the code can be written as MOV CH , [BP + DI + 12]
Note (MOV AL , [SI][DI] + displacement) is illegal
❖ Segment overrides
The 8086 CPU allow the programmer to override the default segment and
use any segment register as
MOV AL, [BX] ; the default segment is DS to override it by using MOV
AL,SS:[BX] Now the address of the operand being moved to AL is SS:BX
instead of DS:BX

Example

Assume that the registers have the following values


BP = 2400H DS = 3400H SI = 6430H DI = 5430H SS = 5420H BX = 3250H. calculate
the physical address of each of the following instruction:
1- Mov [DI] , Al 2- Mov [DI][BX]+5,DX 3- Mov Ax,[BP][SI]+7
The solution
1- PA = DS * 10H + DI = 34000 + 5430 = 39430H
2- PA = DS *10H + [DI+BX+5] = 34000 + [5430 + 3250 + 5] = 34000 + 8685 =
3C685H
3- PA = SS *10H + [BP+SI+7] = 54200 + [2400 + 6430 + 7] = 54200 + 8837 =
5CA37H

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

We can write the code as

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

o MUL (byte * word)


One of the operand (byte type) must be in AL, AH must be zero
The result will be in AX&DX

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 65535 0000FFFF FFFF 0000 0

IMUL BX -1 FFFFFFFF FFFF FFFF 0

2- AX = FFFFH ,BX= FFFFH


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

JLE/JNG Jump if less than or equal or jump ZF = 1 or SF != OF


of not greater

❖ Unsigned jumps

Instruction Description Condition to jump


JA/JNBE Jump if above or jump if not below CF=0 & ZF= 0
or equal
JAE/JNB Jump if above or equal or jump if CF = 0
not below than
JB/JNAE Jump if below or jump of not CF = 1
above or equal

JBE/JNA Jump if below or equal or jump of CF = 1 or ZF = 1


not above

18
❖ Single flag jumps:
Instruction Description Condition to jump
JZ/JE Jump if zero or jump if equal ZF= 1

JNZ/JNE Jump if not zero or jump if not ZF = 0


equal
JC Jump if carry CF = 1
JNC Jump if not carry CF = 0

JO Jump if overflow OF = 1

JNO Jump if not overflow OF = 0

JS Jump if sign SF = 1

JNS Jump if not sign SF = 0

How to convert high-level language code in to assembly code?


1- If ……. Then (if condition true then execute true branch statement End if)

Example

Convert the following c program in to


assembly language code

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)

Example Convert the following c program in to assembly language code

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: -

Number of service (put in AH) Service that provides


1 Read a char from keyboard
0A Read a string from keyboard
2 Print a character on the screen
9 Print a string on the screen

1- Reading a character from keyboard


Inputs
putting 1 in AH
outputs
AL will contain the ASCII code of the character that entered from keyboard
2- Display a character on the screen
Inputs
Putting 2 in AH
Putting the ASCII code of character that we want to print it on screen in DL
Outputs
AL will contain The ASCII code of the printed character
Control characters as shown in the following table.
ASCII code Description
7 Sound (Beep)
8 Back space
9 Tab
A New Line
D Start from the beginning of the line

Example

Write assembly program that read a character from the keyboard then print it
in the next line

22
The output

3- Print string in the screen


Inputs
Putting 9 in Ah
putting the offset address of the string that we want to print it in the screen in Dx

Example print the string “Hello Assembly “ on screen 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

Example 2- Copy “assembly” in reverse form in string2

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

You might also like