0% found this document useful (0 votes)
8 views134 pages

معالجات دقيقة

The document provides an introduction to the 8086 microprocessor, detailing its specifications, including memory size, speed, and instruction set. It explains the programming model, including the types of registers and their functions, as well as the concepts of logical and physical addressing. Additionally, it outlines the requirements for understanding binary and hexadecimal numbering systems and provides homework assignments and reference texts for further study.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views134 pages

معالجات دقيقة

The document provides an introduction to the 8086 microprocessor, detailing its specifications, including memory size, speed, and instruction set. It explains the programming model, including the types of registers and their functions, as well as the concepts of logical and physical addressing. Additionally, it outlines the requirements for understanding binary and hexadecimal numbering systems and provides homework assignments and reference texts for further study.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 134

Lecture 1: Introduction to 8086 Mp

 A microprocessor (Mp) is a programmable controller on a chip.

The first Mp in the world, Intel 4004 was invented in 1971. 4004 Mp has:

- A memory of 4096 locations, each has 4-bits width.


- A speed of 50 KIPS (Kilo Instruction Per Second).
- An instruction set that contains 45 instructions.

After 4004, Microprocessors gradually developed in the memory size, speed and no. of
instructions.

 8086 Mp was released by Intel in 1978. 8086 Mp has:


- A memory of 1 Mega locations, each has 8-bits width.
- A speed of 2.5 million KIPS (Kilo Instruction Per Second).
- An instruction set that contains over 20,000 instructions.
- A cash memory to speed up instructions execution.
- A 16-line data bus.
- A 20-line address bus.

,‫ انقٍاو تحساتاخ يرُٕػح‬,‫ يؼاندح يؼهٕياخ‬,‫ يُٓا قشاءج ادخال‬,‫انًؼانح ْٕ سقاقح قاتهّ نهثشيدح ذسرخذو نؼًهٍاخ يرؼذدج‬
.‫ انخ‬... ,‫انرخاطة ٔانسٍطشج ػهى اخٓضج يؼٍُح‬

‫ (يغ اَّ الٌقاسٌ يغ االيكاٍَاخ انًٕخٕدج فً انًؼانداخ انحذٌثح) ْٕ اَّ ًَٕرج يصغش‬0808 ‫سثة دساسح يؼانح‬
, ‫ حٍث اٌ انرطٕس انزي حصم ْٕ فً حدى انزاكشج ٔسشػح انًؼانح ٔػذد االٌؼاصاخ‬,‫نهًؼانداخ انًٕخٕدج فً انٕقد انحاضش‬
.ّ‫أيا االساط فٕٓ راذ‬
The 8086 Mp Programming Model (or Software Model)

8-bit 8-bit

AX AH AL Accumulator

BX BH BL Base Index

CX CH CL Count

DX DH DL Data

SP Stack Pointer

BP Base Pointer

DI Destination Index

SI Source Index
16-bit

IP Instruction Pointer
FLAGS register Flags

CS Code Segment Register

DS Data Segment Register

ES Extra Segment Register

SS Stack Segment Register

‫ حٍث ٌُظش انًثشيح انى انًؼانح ػهى أَّ يدًٕػح يٍ انسدالخ انًرُٕػح ٔانرً ٌرى‬, ‫انشسى أػالِ ًٌثم َظشذُا انى انًؼانح‬
‫ يٕضٕع يادج‬0808‫ ٔسركٌٕ االٌؼاصاخ انخاصح تًؼانح انـ‬,‫انرحكى تانًؼانح أٔ يخاطثح انًؼانح يٍ خالنٓا تاسرخذاو االٌؼاصاخ‬
.‫انًؼانداخ خالل انفصم االٔل اٌ شاء هللا‬
- 8-bit registers are:
AH, AL, BH, BL, CH, CL, DH and DL.
- 16-bit registers are:
AX, BX, CX, DX, SP, BP, DI, SI, IP, FLAGS, CS, DS, ES and SS.
- Multipurpose registers are:
AX, BX, CX, DX, BP, DI and SI.
- Special purpose registers are:
IP, SP, FLAGS, CS, DS, ES and SS.

IP: addresses the next instruction in the code segment.


SP: addresses an area of memory called the stack.
CS: defines the starting address of the section of memory holding code.
DS: points to the starting address of the section of memory holding the data used
by a program.
ES: additional data segment.
SS: points to the starting address of the stack segment.

Segment-Offset Address Combinations


segment offset Special purpose
CS IP Instruction address
SS SP or BP Stack address
DS BX, DI, SI, 8-bit or 16-bit number Data address
ES --- ---

ّ‫اندذٔل اػالِ رٔ اًٍْح تانغح حٍث سٍرى انشخٕع انٍّ تشكم دائى الٌ انًثشيح ٌدة اٌ ٌؼهى انسدم انزي ٌرى اسرخذاي‬
.‫ االستؼح‬segments‫) ػُذ انرؼايم يغ أي يٍ انـ‬offset( ‫كًؤشش‬
Important notes:

1 Byte = 8-bits
Word = 2 Bytes = 16-bits
Double word = 2 words = 4 Bytes = 32-bits
Nibble = 4-bits (1 Byte= 2 nibbles)
1 K Byte = 210 Byte = 1024 Byte
1 M Byte = 210 K Byte = 210 * 210 Byte
b15 … b2 b1 b0

Higher bit
Requirements Lower bit
Higher nibble Lower nibble

Higher byte Lower byte

Requirements:
It is required from students at this level to understand the following:

- Binary numbering
- Hexadecimal numbering
- Converting between Binary, Hexamdecimal and Decimal numbering systems.
- The difference between signed and unsigned numbers.

‫ ٔانرً سثق ٔذى دساسرٓا فً يادج انرقٍُاخ‬,‫يطهٕب يٍ انطهثح يشاخؼح انُقاط أػالِ قثم انثذء فً يادج انًؼانداخ انذقٍقح‬
.‫انشقًٍح فً انصف األٔل‬

HW:

1) Count in Hexadecimal from 0 to 100H (Ascending)

2) Count in Hexadecimal from 400H down to 360H (Descending)

**************

Reference text books:

1) “The Intel Microprocessors”, by: Barry B. Brey


2) “The 8088 and 8086 Mp’s programming, interfacing S/W, H/W and Applications”, by:
W. A. Triebel and A. Singh.
Lecture 2: Logical and Physical Addressing
Logical Addressing
 8086Mp has a 1 MByte memory locations; each size of each location is 8-bits. In order to
reach any location in this memory, we need to know the logical address of that location.
 8086Mp memory is logically divided into segments. The size of each segment is 64 Kbyte.
 A programmer can reach the 8086Mp memory only by using logical addressing. Logical
Address is written as follows: (Segment : Offset)
 The segment part of the address is fixed for all the memory locations that are in this segment
(which is 1000 H in the below example), while the offset value changes from 0000 H to FFFF
H for each segment.
 Offset is like a pointer that points to a specific location inside the segment.
 The segment value can only be stored in one of the segment registers while the offset is stored
in one of the offset registers associated with that segment (go back to the segment-offset
combinations table given in the previous lecture).

ٙ‫ركٌٕ انؼُٕاٌ انًُطم‬ٚ .‫ح‬ٛ‫رؼايم يؼ ٓا اال يٍ خالل انؼَُٕح انًُطم‬ٚٔ ‫صم انٗ ا٘ يٕلغ يٍ انزاكشج‬ٚ ٌ‫ًكٍ نهًثشيج ا‬ٚ‫ال‬
‫ داخم ْزا‬ٙ‫ؤشش انٗ يٕلغ يذذد ف‬ٚ ٘‫ًح انًؤشش انز‬ٛ‫ذًم ل‬ٚ َٙ‫ ) ٔانثا‬Segment(‫ذًم ػُٕاٌ انـ‬ٚ ‫ االٔل‬,ًٍٛ‫يٍ لغ‬
‫ًح‬ٛ‫ذرٕ٘ ل‬ٚ ٘‫كٌٕ طٕل انغجم انز‬ٚ ٌ‫رطهة ا‬ٚ ‫ يٍ انًٕالغ ٔرنك‬64 Kbyte ٗ‫ذرٕ٘ ػه‬ٚ Segment ‫ كم‬.Segment‫انـ‬
.ً‫ الدما‬ٙ‫أذ‬ٛ‫) كًا ع‬CS, DS, ES, SS( ‫ ادذ انغجالخ االستؼح‬ٙ‫ ف‬segment‫ًح انـ‬ٛ‫رى خضٌ ل‬ٚ .16-bit ٘ٔ‫غا‬ٚ segment‫انـ‬
‫ًح‬ٛ‫ فارا كاَد ل‬.ُّ‫خش يٕلغ ي‬ٜ )FFFF H( ٗ‫ ان‬segment‫) الٔل يٕلغ يٍ انـ‬0000 H( ٍ‫ ذرشأح ي‬ٙٓ‫ًح انًؤشش ف‬ٛ‫أيا ل‬
ٌ‫) ٔانؼُٕا‬6A00 H : 0000 H( ْٕ segment ‫ الٔل يٕلغ يٍ انـ‬ٙ‫ فإٌ انؼُٕاٌ انًُطم‬6A00 H ً‫ ذغأ٘ يثال‬segment‫انـ‬
.)6A00 H : FFFF H( ْٕ segment ‫ الخش يٕلغ يٍ انـ‬ٙ‫انًُطم‬
ٙ‫ (الٌ انذجى انكه‬64 Kbyte ٌٕ‫ك‬ٚ ‫ انٕادذ‬segment ‫ فًؼُاِ اٌ دجى انـ‬16-bit ٘ٔ‫غا‬ٚ ّ‫( طٕن‬offset) ‫تًا أٌ انًؤشش‬
.)64Kbyte ٘ٔ‫غا‬ٚ ْٕٔ 61 ‫ نألط‬2 ْٕ ‫تد‬61 ‫ًكٍ ػَُٕرّ تاعرخذاو‬ٚ ٘‫انز‬
‫ح‬ٛ‫ٍ انًُطم‬ٚٔ‫ش انؼُا‬ٛ‫ ٔلذ ذى ذأش‬,)‫ٕظخ يمطؼا ً يٍ انزاكشج (انًذذد تانهٌٕ االصسق انفاذخ‬ٚ ٙ‫ انشعى انران‬ٙ‫انًثال انًؼطٗ ف‬
‫ داخم يٕلغ انزاكشج‬ٙ‫كرة ف‬ٚ ‫غاس أيا يا‬ٛ‫ٍ أٔ ان‬ًٛٛ‫ انٗ جاَة انزاكشج ػهٗ ان‬ٙ‫ائ‬ٚ‫ض‬ٛ‫ أ انف‬ٙ‫رى كراتح انؼُٕاٌ عٕا ًء انًُطم‬ٚ .ّٛ‫ػه‬
.8-bits ّ‫كٌٕ طٕن‬ٚٔ ً ‫ظ ػُٕاَا‬ٛ‫) ٔن‬data‫ؼرثش يذرٕٖ انًٕلغ (أ٘ انـ‬ٛ‫ف‬
ٙ‫ االعفم ٔاخش يٕلغ يُٓا ْٕ انًٕجٕد ف‬ٙ‫ ػُذ سعى انزاكشج َمٕو تاػرثاس أل يٕلغ يُٓا ْٕ انًٕجٕد ف‬: 1‫مالحظة‬
‫ ػهًا ً اٌ تؼط انًصادس‬.‫ انًذاظشج انغاتمح‬ٙ‫ انًؼطٗ ف‬6 ‫ سلى‬ٙ‫ انكراب انًُٓج‬ٙ‫مح انًغرخذيح ف‬ٚ‫ يغ انطش‬ٙ‫ ٔرنك نهًش‬.ٗ‫االػه‬
.‫ذغرخذو ػكظ رنك‬

ّ‫ؼشف‬ٚ ‫فرشض اٌ انطانة‬ٚ ٘‫ ٔانز‬,‫ح‬ٛ‫ائ‬ٚ‫ض‬ٛ‫ح ٔانف‬ٛ‫ٍ انًُطم‬ٚٔ‫ كرات ح انؼُا‬ٙ‫رى اعرخذاو انُظاو انغادط ػشش ف‬ٚ :2 ‫مالحظة‬
.‫ذ‬ٛ‫تشكم ج‬

ٗ‫رى انرطشق ان‬ٚ ٌ‫جادِ تؼذ ا‬ٚ‫ح ا‬ٛ‫ف‬ٛ‫فٓى ك‬ٛ‫ش نهزاكشج تشكم ػالياخ اعرفٓاو الٌ انطانة ع‬ٛ‫ ذى كراتح انًٕلغ االخ‬:3 ‫مالحظة‬
.‫ح‬ٛ‫ائ‬ٚ‫ض‬ٛ‫انؼَُٕح انف‬
8-bits
?H:?H

...
1000 H : FFFF H
...

1000 H : F000 H
64 Kbyte Segment
Logical Addresses

1000 H : 0000 H

0000 H : 0000 H

Physical Addressing (PA): (Some references call it: effective address (EA))

In order to access any location in the 1 Mbyte memory, the Mp must generate a 20-bit
memory address, which is called a Physical Address (PA). Physical address is calculated from
the logical address as follows:

Physical Address (PA) = Segment * 16 + offset

where multiplying the segment by 16 is simply achieved by shifting it to the left and adding a
zero to the lower digit.

1 ْٕ ٙ‫ٍ تد (الٌ دجى انزاكشج انكه‬ٚ‫ركٌٕ يٍ سلى طٕنّ ػشش‬ٚ ٘‫ ٔانز‬ٙ‫م‬ٛ‫ ْٕ انؼُٕاٌ انذم‬ٙ‫ائ‬ٚ‫ض‬ٛ‫انؼُٕاٌ انف‬
‫ ٔرنك تاظافح صفش‬,ٙ‫ انًطهٕب انرؼايم يؼّ يٍ خالل انؼُٕاٌ انًُطم‬ٙ‫ائ‬ٚ‫ض‬ٛ‫مٕو انًؼانج تذغاب انؼُٕاٌ انف‬ٚٔ .)MByte
ٕ٘‫ذر‬ٚ ‫ فانًفشٔض اٌ َذصم ػهٗ سلى‬.offset ‫ًح انًؤشش‬ٛ‫) ثى انجًغ يغ ل‬61 ٙ‫ؼادل ظشب ف‬ٚ ٘‫ (ٔانز‬segment‫انٗ انـ‬
‫) الٔل‬00000 H( ٍٛ‫ ت‬ٙ‫ائ‬ٚ‫ض‬ٛ‫ًح انؼُٕاٌ انف‬ٛ‫) ٔذرشأح ل‬1 hexa digit ‫ (الٌ كم استؼح تد ذًثم‬5 hexa digits ٗ‫ػه‬
.‫) الخش ػُٕاٌ يُٓا‬FFFFF H) ٗ‫ ان‬,‫ػُٕاٌ يٍ انزاكشج‬

Example: Find the PA of the next instruction, if CS =1000 H, DS =A000 H, ES = A80 H, IP


=2000 H.

Solution:
The logical address of the next location is stored in (CS: IP), so that DS and ES will not be
required to calculate the PA of the next instruction.

‫ َالدظ‬.)CS: IP( ٙ‫ انؼُٕاٌ انًُطم‬ٙ‫زِ ف‬ٛ‫مٕو ترُف‬ٛ‫ انز٘ ع‬ٙ‫ؼاص انران‬ٚ‫ز ا٘ تشَايج تخضٌ ػُٕاٌ اال‬ٛ‫مٕو انًؼانج ػُذ ذُف‬ٚ
.‫ يؼطاج نغشض انرأكذ يٍ فٓى انطانة نهًٕظٕع‬ْٙٔ ‫ انذم‬ٙ‫ش داخهح ف‬ٛ‫ انغؤال غ‬ٙ‫ى انغجالخ االخشٖ انًٕجٕدج ف‬ٛ‫أٌ ل‬

PA= 1 0 0 0 0
2000 +
12000H
Example: If a segment register value is (2100H), what are the starting and ending physical
addresses of this segment?

Solution:
- For the starting address, the offset value is (0000 H), so PA is:
PA= 2 1 0 0 0
0000 +
21000H
- For the ending address, the offset value is (FFFF H), so PA is:
PA= 2 1 0 0 0
FFFF +
30FFFH
Example: If SS =A022 H, DI =F019 H, BP =2E60 H, IP =1111 H, find the PA of the stack.
Solution:
The stack is addressed using the logical address (SS: BP) or (SS: SP). The value of SP is not
given in the question, so that BP is used, so PA is:

‫ َؼهى‬,‫ انًذاظشج انغاتمح‬ٙ‫ ٔيٍ خالل انشجٕع انٗ انجذٔل انًؼطٗ ف‬SS‫ انـ‬ٙ‫ يخضٌٔ ف‬stack ‫ انخاص تانـ‬segment ‫انـ‬
.BP ‫ انغؤال فُغرخذو‬ٙ‫ش يؼطاج ف‬ٛ‫ غ‬SP ‫ًح‬ٛ‫ ٔتًا اٌ ل‬.‫ فمط‬BP ٔ SP ْٙ ّ‫تاٌ انًؤششاخ انًغرخذيح يؼ‬

PA= A 0 2 2 0
2E60 +
A3080H

Example: Find a logical address that can take you to the location (255AF H) of the memory.
Solution: There are many logical addresses that lead to the given PA:
‫ذ‬ٚ‫ُا ً أ٘ أٌ ُْانك انؼذ‬ٛ‫غأ٘ سلًا ً يؼ‬ٚ ‫ٍ داصم جًؼًٓا‬ًٛ‫جاد سل‬ٚ‫ يشاتٓح ال‬ْٙ ٙ‫ائ‬ٚ‫ض‬ٛ‫ يٍ انف‬ٙ‫جاد انؼُٕاٌ انًُطم‬ٚ‫ح ا‬ٛ‫ػًه‬
‫ ٔجًؼّ يغ‬segment‫ يٍ خالل ٔظغ صفش انٗ ػُٕاٌ انـ‬ٙ‫ انؼُٕاٌ انًُطم‬ٙ‫ٍ ف‬ًٛ‫جة انرأكذ يٍ صذح انشل‬ٚ ٍ‫ ٔنك‬,‫يٍ انذهٕل‬
.ٗ‫ انًؼط‬ٙ‫ائ‬ٚ‫ض‬ٛ‫طاتك انؼُٕاٌ انف‬ٚ ٌ‫جة ا‬ٚ ٘‫ انز‬ٙ‫ائ‬ٚ‫ض‬ٛ‫ ف ُذصم ػُذْا ػهٗ انؼُٕاٌ انف‬offset‫انـ‬

255A H: 000F H
or
2000 H: 55AF H
or
2500 H: 05AF H
(and many other combinations)

More examples can be found in [Ref. 1, table 2-1]

Best Regards
Dr. Zainab Alomari
Lecture 3: Addressing Modes
8086Mp has a group of instructions that are used by the programmer to specify the task
required to be done by the Mp. The language used for programming 8086Mp is called (Assembly
Language).

(MOV) instruction is the most used 8086Mp instruction, and it is used as follows:

MOV AX, BX
In this example: Source
Destination

- AX is the destination
- BX is the source
- The value of BX will be moved (copied) to AX
- The old value of AX will be removed and now AX=BX
- BX will not be changed

In all instructions:

- The source and destination MUST have the same length.


- The destination is called so because the result of the operation performed by the
instruction is always stored in the destination.
- The source is NOT changed by the instruction.

‫كٌٕ رنك يٍ خالل‬ٚٔ ,)‫اذٓا خالل انفصم األٔل‬ٛ‫رى ذؼهى اعاع‬ٛ‫ ع‬ٙ‫رى انرؼايم يغ انًؼانح يٍ خالل انهغح انخاصح تّ (ٔانر‬ٚ
.‫اخ يرُٕػح‬ٛ‫او تؼًه‬ٛ‫ًكٍ اعرخذايٓا نهق‬ٚ ٙ‫انُظش انٗ انًؼانح ػهٗ ا َّ يدًٕػح يٍ انغدالخ انر‬

‫رى‬ٛ‫ ٔع‬.)destination(‫) انٗ انـ‬source(‫ًح انـ‬ٛ‫ؼاص َقم أ َغخ ق‬ٚ‫) ْٕٔ ا‬MOV( ْٕ ً ‫ؼاص االتغظ ٔاالكثش اعرخذايا‬ٚ‫اال‬
.)Addressing Modes( ‫ؼاص نششذ االَٕاع انًخرهفح يٍ انؼَُٕح‬ٚ‫اعرخذاو ْزا اال‬

Addressing Modes
1) Register Addressing Mode

‫كَٕا تُفظ انطٕل‬ٚ ٌ‫دة ا‬ٚٔ )register( ‫) ػثاسج ػٍ عدم‬destination(‫) ٔانـ‬source(‫كٌٕ كم يٍ انـ‬ٚ ‫ ْزا انُٕع‬ٙ‫ف‬
.)‫تد‬66 ‫تد أٔ كالًْا‬8 ‫(ئيا كالًْا‬

MOV Register, Register


.‫ؼاصاخ‬ٚ‫ يدًٕػح يٍ االيثهح ػهٗ ْزا انُٕع يٍ اال‬ٙ‫ اندذٔل انران‬ٙ‫ف‬
‫ُْانك زاالخ ‪ًُٚ‬غ اعرخذايٓا ْٔ‪ ٙ‬انساالخ انران‪ٛ‬ح‪:‬‬

‫‪ -6‬أٌ ‪ٚ‬كٌٕ طٕل انـ(‪ٔ )source‬انـ(‪ )destination‬غ‪ٛ‬ش يرغا ٍٔ‪( ,‬كًا ف‪ ٙ‬انًثال قثم االخ‪ٛ‬ش يٍ اندذٔل انغاتق)‪.‬‬
‫‪ -2‬أٌ ‪ٚ‬كٌٕ كال انغده‪( ٍٛ‬أ٘ كم يٍ انـ(‪ٔ )source‬انـ(‪ ْٕ ))destination‬يٍ انـ(‪ )segment registers‬االستؼح‬
‫ْٔ‪( )CS, DS, ES, SS( ٙ‬كًا ف‪ ٙ‬انًثال ‪ 8‬يٍ اندذٔل انغاتق )‪ٔ ,‬انغثة أَٓا عدالخ خاصح ٔاعرخذايٓا ف‪ٙ‬‬
‫اال‪ٚ‬ؼاصاخ ‪ٚ‬كٌٕ تشكم يسذٔد فقظ‪ .‬أيا ارا كاٌ أزذ انغده‪ ٍٛ‬فقظ ْٕ يٍ انـ(‪ )segment registers‬فٓزا يًكٍ يا ػذا‬
‫زانح ٔازذج ْٔ‪ ٙ‬أٌ ‪ٚ‬كٌٕ انـ‪ ْٕ CS‬انـ(‪ )destination‬فٓزا ال ‪ٚ‬دٕص (كًا ف‪ ٙ‬انًثال األخ‪ٛ‬ش يٍ اندذٔل انغاتق)‬
‫ٔانغثة أَّ ‪ٚ‬سرٕ٘ ق‪ًٛ‬ح يًٓح ذًثم ػُٕاٌ انـ‪ segment‬انز٘ ‪ٚ‬سرٕ٘ ػهٗ اال‪ٚ‬ؼاصاخ ف‪ ٙ‬انزاكشج ٔف‪ ٙ‬زانح ذغ‪ٛٛ‬ش‬
‫ق‪ًٛ‬رّ ‪ٚ‬رى فقذاٌ انثشايح‪.‬‬

‫مالحظة‪ :‬خً‪ٛ‬غ انغدالخ ال‪ًٚ‬كٍ ذدضئرٓا ف‪ ٙ‬اال‪ٚ‬ؼاصاخ ٔاًَا ‪ٚ‬رى انرؼايم يؼٓا ػهٗ آَا ‪66‬تد دائًاً‪ ,‬يا ػذا ( ‪AX, BX,‬‬
‫‪ )CX, DX‬ز‪ٛ‬ث ‪ًٚ‬كٍ ذدضئح كم يُٓى انٗ خضئ‪ ٍٛ‬يٍ ‪8‬تد (‪.)AL, AH, BL, BH, CL, CH, DL, DH‬‬

‫‪2) Immediate Addressing Mode‬‬

‫‪MOV Register , NUMBER‬‬

‫ال ٔ‪ٚ‬كٌٕ تُفظ طٕل انـ(‪ )destination‬ئيا ‪8‬تد أٔ ‪66‬تد‪ٚٔ .‬ؼطٗ‬ ‫ف‪ْ ٙ‬زا انُٕع ‪ٚ‬كٌٕ انـ(‪ )source‬سقًا ً ٔن‪ٛ‬ظ عد ً‬
‫انشقى ئيا تانُظاو انغادط ػشش٘ (‪ٔ )Hexadecimal‬رنك تأٌ ‪ٚ‬كرة ف‪ ٙ‬آخشِ انسشف ‪( H‬كًا ف‪ ٙ‬انًثال ‪ 2‬يٍ اندذٔل ادَاِ)‪,‬‬
‫أٔ تانُظاو انثُائ‪ٔ )Binary( ٙ‬رنك تاٌ ‪ٚ‬كرة ف‪ ٙ‬آخشِ انسشف ‪( B‬كًا ف‪ ٙ‬انًثال األخ‪ٛ‬ش يٍ اندذٔل ادَاِ)‪ ,‬أٔ تانُظاو انؼشش٘‬
‫(‪ٔ )decimal‬رنك تاٌ ال ‪ٚ‬كرة ف‪ ٙ‬آخشِ ا٘ ش‪ٙ‬ء (كًا ف‪ ٙ‬األيثهح ‪ 4ٔ 3ٔ 6‬يٍ اندذٔل ادَاِ)‪ .‬أيا ارا كاٌ سيضا ً ٔن‪ٛ‬ظ سقًا ً‬
‫ف‪ٛ‬رى ٔضؼّ داخم ػالير‪ُْٔ (‘ ‘) ٙ‬ا ‪ٚ‬رى اخز ق‪ًٛ‬ح انـ(‪ ) ASCII‬انخاصح تٓزا انشيض (ْٔ‪ ٙ‬ػثاسج ػٍ سقى طٕنّ ‪8‬تد ‪ٚ‬قاتم‬
‫انشيض انًؼطٗ ت‪ ٍٛ‬ػالير‪ ٙ‬االقرثاط) ٔ‪ٚ‬رى اػطاؤِ نهـ‪ٔ destination‬انز٘ ‪ٚ‬دة اٌ ‪ٚ‬كٌٕ عدالً تطٕل ‪8‬تد (كًا ف‪ ٙ‬انًثال ‪5‬‬
‫يٍ اندذٔل ادَاِ)‪ٔ .‬ارا ذى ٔضغ سيض‪ ٍٚ‬داخم ػالير‪ ٙ‬االقرثاط يؼُاِ ع‪ٛ‬رى أخز ‪66‬تد (‪8‬تد نكم سيض) ٔٔضؼٓا ف‪ٙ‬‬
‫انـ‪ destination‬انز٘ ‪ٚ‬دة اٌ ‪ٚ‬كٌٕ عدالً تطٕل ‪66‬تد (كًا ف‪ ٙ‬انًثال ‪ 6‬يٍ اندذٔل أدَاِ)‪.‬‬
Examples: MOV AL, 15H (AL=15H)

MOV BX, 6F0AH (BX=6F0AH) or (BL=0AH and BH=6FH)

MOV CX, 1 H (CX=1H) or (CL=01H and CH=00H)

MOV 1FH, AL

MOV 47H, DH NOT ALLOWED  (Destination CAN NOT be a number!)

MOV 55, DX

‫ ٔكزنك انسال ارا‬.‫ح‬ٛ‫ش يُطق‬ٛ‫ح غ‬ٛ‫ ػًه‬ٙٓ‫ ف‬,ّٛ‫ح انُقم ذرى ان‬ٛ‫ سقًا ً الٌ ػًه‬destination‫كٌٕ انـ‬ٚ ٌ‫ًكٍ ا‬ٚ ‫َالزظ أَّ ال‬
.‫دٕص‬ٚ ‫ضا ً ال‬ٚ‫ ػثاسج ػٍ اسقاو فٓزا ا‬destination‫ ٔانـ‬source‫كاٌ كم يٍ انـ‬

MOV DS, 100H NOT ALLOWED 


It is NOT allowed to use immediate addressing mode with segment registers (CS, DS, ES,
SS). In order to put 100H in DS, we can do the following:

MOV AX, 100H

MOV DS, AX ALLOWED 


ٖ‫ ٔاًَا َقٕو تاعرخذاو ازذ انغدالخ االخش‬,‫ االستؼح‬segment registers‫ؼاصاخ يغ انـ‬ٚ‫ًكٍ اعرخذاو ْزا انُٕع يٍ اال‬ٚ ‫ال‬
‫ش‬ٛٛ‫ًكٍ اعرخذايٓا نرغ‬ٚ ‫قح‬ٚ‫) ْٔزِ انطش‬register addressing mode( ‫ًح تاعرخذاو انـُٕع االٔل‬ٛ‫ نُقم انق‬ٙ‫كًخضٌ ٔعط‬
.ً‫ كًا ركش عاتقا‬CS‫ يا ػذا انـ‬segment registers‫ًح ا٘ يٍ انـ‬ٛ‫ق‬

3) Direct Addressing Mode

MOV destination , [ NUMBER ]

MOV [ NUMBER ], source


‫رى اعرخذاو اقٕاط يشتؼح نهٕصٕل‬ٚ ‫ث‬ٛ‫ ز‬,destination‫ أٔ كـ‬source‫رى انرؼايم يغ انزاكشج ٔاعرخذايٓا كـ‬ٚ ‫ ْزا انُٕع‬ٙ‫ف‬
.‫غاػذَا ػهٗ انٕصٕل انٗ انؼُٕاٌ انًطهٕب‬ٛ‫ٍ ْزِ االقٕاط ن‬ٛ‫رى ٔضغ سقى يات‬ٚٔ ‫انٗ انزاكشج‬

Example 1:

MOV AL , [1234H]

In this example, an 8-bit value is taken from a memory location and copied to AL.

How can the Mp calculate the physical address and go to the correct memory location? The
physical address is calculated from (segment : offset). The number given in the instruction
inside square brackets is the offset. When the offset is a number, then the segment is always DS.
The Mp will take the offset from the instruction which is 1234H and adds it with DS value after
multiplying DS by 16 to find the physical
.
address. Let DS=1000H, then:
.
.
66H 1000H:1235H

5FH 1000H:1234H
AL B0H 1000H:1233H
.
.
.

(After execution: AL=5FH)

:‫ّ يٍ انزاكشج‬ٛ‫ انًطهٕب انزْاب ان‬ٙ‫ائ‬ٚ‫ض‬ٛ‫ح زغاب انؼُٕاٌ انف‬ٛ‫ف‬ٛ‫ك‬

ٙ‫ كًا ف‬,66 ٙ‫ تؼذ ضشتّ ف‬DS‫ًح انـ‬ٛ‫ ثى ذدًغ يغ ق‬offset‫ث ذًثم انـ‬ٛ‫ٍ االقٕاط انًشتؼح ز‬ٛ‫ًح انًٕخٕدج ت‬ٛ‫رى اعرخذاو انق‬ٚ
.‫انًثال انغاتق‬

Example 2:

MOV AX, [51FFH]

In this example, 16-bits will be moved from the memory to AX. The memory PA will be
calculated in the same way as in example 1. This time two bytes are required to be copied from
the memory to AX, so that: the first byte (lower byte) of AX (which is AL) will be taken from
the specified memory location and the second byte (higher byte) of AX (which is AH) will be
taken from the next memory location. Let DS=2000H, then:
.
.
.

D9H 2000H:5200H
AH 33H 2000H:51FFH
AL 1FH 2000H:51FEH
.
.
.

After execution: AX=D933H.

.‫ انغإال‬ٙ‫ يدشد يثال ٔذؼطٗ ف‬ْٙ ‫ انزاكشج ُْا‬ٙ‫ى انًخضَٔح ف‬ٛ‫ انق‬:‫مالحظة‬

Note: in the two previous examples, the memory location was the source so that it is not
changed; only the destination is changed.

Example 3:

MOV [13A9H], DX

In this example, two bytes will be copied from DX to the memory location at offset=13A9H
in DS. Let DS=4000H and DX=8F10H, then:

.
.
.
4000H:13ABH
DH
8FH 4000H:13AAH
DL
10H 4000H:13A9H
.
.
.

(after execution DX is not changed, only two memory locations are changed)

MOV [4F00H], [651BH] NOT ALLOWED (memory to memory is not allowed) 


MOV [3A88H], 5H This is allowed BUT: the length of the moved data is not clear (16-bit or
8-bits), therefore we need to specify this length using either (byte ptr) or (word ptr), as follows:

MOV byte ptr[3A88H], 5H One memory location at DS:3A88H will be given the value 05H

MOV word ptr[3A88H],5H Two memory locations will be changed as follows:

DS:3A88H will be given the value (05H)

DS:3A89H will be given the value (00H)

MOV 5H, [3A88H] NOT ALLOWED (destination can NOT be a number) 

MOV DS, [3000H] NOT ALLOWED (segment registers can NOT be used with memory) 

4) Indirect Addressing Mode

‫ نًٕقغ انزاكشج‬offset‫ًرّ ذًثم انـ‬ٛ‫) ق‬register( ‫ظ تشكم سقى ٔاًَا تشكم عدم‬ٛ‫ ن‬offset‫رى اػطاء انـ‬ٚ ‫ ْزا انُٕع‬ٙ‫ف‬
.‫ٍ اقٕاط يشتؼح‬ٛ‫ٕضغ ْزا انغدم ت‬ٚ ‫ث‬ٛ‫ ز‬,‫انًطهٕب‬

MOV Destination, [Register ]

MOV [Register ], Source

Examples: MOV AX, [BX]


MOV [DI], AL
MOV [BP], BX
MOV DX, [SI]
)segment-offset combinations(‫ خذٔل انـ‬ٙ‫ يٕخٕدج ف‬ْٙٔ offset‫ًكٍ اٌ ذغرخذو كـ‬ٚ ‫ُْانك عدالخ يسذدج‬
ّ‫ًكٍ اعرخذاي‬ٛ‫) ف‬SP( ‫ أيا‬,)BP, BX, SI, DI( ْٙ ‫ ٔزغة ْزا اندذٔل فاٌ ْزِ انغدالخ‬.ٗ‫ انًساضشج االٔن‬ٙ‫انًؼطٗ ف‬
‫ؼاصاخ التشكم عدم ٔالتشكم‬ٚ‫ اال‬ٙ‫ًكٍ اعرخذايّ تشكم يثاشش يطهقا ً ف‬ٚ‫ ال‬IP‫ ٔعدم انـ‬,‫ظ كًإشش انٗ انزاكشج‬ٛ‫كغدم فقظ ٔن‬
.‫يإشش‬

‫ يإشش‬ْٙٔ ‫ح‬َٛ‫ح ثا‬ًٛ‫ أٔ ُْانك ذغ‬offset‫ّ اعى انـ‬ٛ‫طهق ػه‬ٚ ‫ش ػهٗ انزاكشج‬ٛ‫ نهرأش‬register ‫ ػُذ اعرخذاو انغدم‬:‫مالحظة‬
.)pointer(

:‫ّ يٍ انزاكشج‬ٛ‫ انًطهٕب انزْاب ان‬ٙ‫ائ‬ٚ‫ض‬ٛ‫ح زغاب انؼُٕاٌ انف‬ٛ‫ف‬ٛ‫ك‬

‫ تؼذ‬segment‫ًح انـ‬ٛ‫ ثى ذدًغ يغ ق‬offset‫ث ذًثم انـ‬ٛ‫ٍ االقٕاط انًشتؼح ز‬ٛ‫ًح انغدم انًٕخٕد ت‬ٛ‫رى اعرخذاو ق‬ٚ
ٙ‫) انًؼطٗ ف‬segment-offset combinations(‫ يٍ خالل انشخٕع انٗ خذٔل انـ‬segment‫رى يؼشفح انـ‬ٚ .66 ٙ‫ضشتّ ف‬
‫ ٔارا كاٌ انًإشش انًغرخذو‬DS ْٕ segment‫كٌٕ انـ‬ٛ‫ فغ‬DI ٔ‫ أ‬SI ٔ‫ أ‬BX ْٕ ‫ فارا كاٌ انًإشش انًغرخذو‬,ٗ‫انًساضشج االٔن‬
.SS ْٕ segment‫كٌٕ انـ‬ٛ‫ فغ‬BP ْٕ
Example: Let BX=1000H, DS=100H. Find the value of AX after executing the following
instruction (note that the memory is given in the question): MOV AX, [BX]
.
.
.
77H 02003H

A7H 02002H

34H 02001H

12H 02000H
.
.
.

Solution: the Physical Address = DS * 16 + BX

PA = 0 1 0 0 0

1000 +

02000H

⸫ After execution, AX= 3412H

‫ انغإال‬ٙ‫ زال ذى اػطائّ ف‬ٙ‫ ٔنكٍ ف‬,‫ر‬ٛ‫ كالًْا صس‬ٙ‫ائ‬ٚ‫ض‬ٛ‫ أٔ انف‬ٙ‫ش انؼُٕاٌ ػهٗ انزاكشج تانشكم انًُطق‬ٛ‫ًكٍ ذأش‬ٚ :‫مالحظة‬
ٍ‫ نُصم نهًٕقغ أ انًٕاقغ انًطهٕتح ي‬ٙ‫ يٍ انؼُٕاٌ انًُطق‬ٙ‫ائ‬ٚ‫ض‬ٛ‫ح فُسراج انٗ زغاب انؼُٕاٌ انف‬ٛ‫ائ‬ٚ‫ض‬ٛ‫قح انؼَُٕح انف‬ٚ‫تطش‬
.‫انزاكشج‬

:‫ الحاالت التالية غير مسموح بها‬:‫مالحظة‬

MOV [1000] , [DI]

MOV [BX], [DI] NOT ALLOWED (no memory to memory operation) 

MOV [100H], [200H]

MOV DS, [DI] NOT ALLOWED (segment registers can NOT be used with memory) 

.Indirect Addressing Mode‫ يدًٕػح يٍ االيثهح انًرُٕػح ػهٗ انـ‬ٙ‫ اندذٔل انران‬ٙ‫ف‬
5) Register Relative Addressing Mode

.‫ داخم االقٕاط انًشتؼح‬offset‫رى اعرخذاو عدم يغ سقى الػطاء انـ‬ٚ ‫ ْزا انُٕع‬ٙ‫ف‬

MOV Destination, [Register + Displacement]

MOV [Register + Displacement], Source

where the displacement is any 16-bit number.

:‫ّ يٍ انزاكشج‬ٛ‫ انًطهٕب انزْاب ان‬ٙ‫ائ‬ٚ‫ض‬ٛ‫ح زغاب انؼُٕاٌ انف‬ٛ‫ف‬ٛ‫ك‬

.66 ٙ‫ تؼذ ضشتّ ف‬segment‫رى خًؼّ يغ انـ‬ٚ ٙ‫ ٔتانران‬offset‫ٍ ْٕ انـ‬ٛ‫ٍ انًشتؼ‬ٛ‫ٍ انقٕع‬ٛ‫رى اػرثاس كم يا يٕخٕد ت‬ٚ
segment-offset (‫ٍ يٍ خالل انشخٕع انٗ خذٔل انـ‬ٛ‫ٍ انقٕع‬ٛ‫ يٍ َٕع انغدم انًغرخذو ت‬segment‫ًكٍ يؼشفح انـ‬ٚٔ
ٌ‫ ٔارا كا‬DS ْٕ segment‫ فانـ‬SI ٔ‫ أ‬DI ٔ‫ أ‬BX ْٕ )offset( ‫ فارا كاٌ انغدم انًغرخذو كًإشش‬,)combinations
.SS ْٕ segment‫ فانـ‬BP ْٕ )offset( ‫انغدم انًغرخذو كًإشش‬

Example: Let BX=100H, DS=200H, find the value of AX after executing the following
instruction (the memory is given in the question): MOV AX, [BX+1000H]

9AH 03102H

70H 03101H

11H 03100H

F5H 030FFH
Solution:

PA= DS * 16 + BX + 1000H

PA = 2 0 0 0

0100

1000 +

03100H

⸫ After execution, AX = 7011 H

:‫ يثال‬,ٍ‫ضا ً يًك‬ٚ‫ خاسج االقٕاط انًشتؼح ْٔزا ا‬displacement‫رى ٔضغ انـ‬ٚ ‫ تؼض انًصادس‬ٙ‫ ف‬:‫مالحظة‬

MOV [BX]+1234H , AL

:ٙ‫ كانران‬,‫تد‬66 ‫تد أو‬8 ْٙ ‫ح ْم‬ٛ‫ذ طٕل انؼًه‬ٚ‫دة ذسذ‬ٛ‫ زانح اعرخذاو ْزا انُٕع يٍ انؼَُٕح يغ سقى ف‬ٙ‫ ف‬:‫مالحظة‬

MOV byte ptr[BX+20H], 4AH One memory location at (DS:BX+20H) will be given the
value 4AH.

MOV word ptr[BX+20H],5A12H Two memory locations will be changed as follows:

(DS: BX+20H) will be given the value (12H)

(DS:BX+26H) will be given the value (5AH)

MOV byte ptr[BX+20H],5A12H NOT ALLOWED (can you know why?) 

6) Base-Plus-Index Addressing Mode (or Base-Indexed Addressing Mode)

:offset‫) إلػطاء انـ‬DI ٔ‫ أ‬SI ( ‫خش‬ٜ‫) ٔا‬BX ٔ‫ أ‬BP( ‫ٍ أزذًْا‬ٛ‫رى اعرخذاو عده‬ٚ ‫ ْزا انُٕع‬ٙ‫ف‬

MOV destination , [ Base Register + Index Register ]

MOV [ Base Register + Index Register ] , source

where:

Base Registers are: BX and BP

Index Registers are: SI and DI


:‫ّ يٍ انزاكشج‬ٛ‫ انًطهٕب انزْاب ان‬ٙ‫ائ‬ٚ‫ض‬ٛ‫ح زغاب انؼُٕاٌ انف‬ٛ‫ف‬ٛ‫ك‬

BP‫ ارا ذى اعرخذاو انـ‬ْٙٔ ‫ يا ػذا زانح ٔازذج‬, PA‫ ػُذ زغاب انـ‬segment‫ كـ‬DS‫رى اعرخذاو انـ‬ٚ ‫ كم انساالخ‬ٙ‫ف‬
.‫ أيثهح ػهٗ ْزا انُٕع‬ٙ‫ اندذٔل انران‬ٙ‫ ف‬.PA‫ ػُذ زغاب انـ‬segment‫ ْٕ انـ‬SS‫ؼرثش انـ‬ٚ ‫فؼُذْا‬

Example: Let BX=AF00H


SI = 1FABH
DS=8000H
SP=F43EH
SS=15FFH
Give the memory locations that will be changed after executing the following instruction:

MOV [BX+SI], SP

Solution:

PA= 8CEABH (can you verify this?)

F4H 8CEACH

3EH 8CEABH

Note: if the instruction is modified to: MOV [BP + SI], SP

then the PA is calculated using SS instead of DS.


7) Base Relative-Plus-Index Addressing Mode

‫ تاإلضافح‬,offset‫) إلػطاء انـ‬DI ٔ‫ أ‬SI ( ‫خش‬ٜ‫) ٔا‬BX ٔ‫ أ‬BP( ‫ٍ أزذًْا‬ٛ‫رى اعرخذاو عده‬ٚ ‫ ْزا انُٕع يٍ انؼَُٕح‬ٙ‫ف‬
.ٍٛ‫ٍ انغاتق‬ٛ‫ أ٘ أٌ ْزا انُٕع ْٕ زاصم ديح انُٕػ‬,‫ًٓا‬ٛ‫ضاف ان‬ٚ ‫انٗ سقى‬

MOV destination , [ Base Register + Index Register + displacement]

MOV [ Base Register + Index Register + displacement] , source

where:

Base Registers are: BX and BP

Index Registers are: SI and DI

Displacement is any 16-bit number

:‫ّ يٍ انزاكشج‬ٛ‫ انًطهٕب انزْاب ان‬ٙ‫ائ‬ٚ‫ض‬ٛ‫ح زغاب انؼُٕاٌ انف‬ٛ‫ف‬ٛ‫ك‬

BP‫ ارا ذى اعرخذاو انـ‬ْٙٔ ‫ يا ػذا زانح ٔازذج‬, PA‫ ػُذ زغاب انـ‬segment‫ كـ‬DS‫رى اعرخذاو انـ‬ٚ ‫ كم انساالخ‬ٙ‫ف‬
‫ًح‬ٛ‫ ق‬ْٙٔ ‫ انزاكشج‬ٙ‫ٍ قٕع‬ٛ‫ًح يا يٕخٕد ت‬ٛ‫ ق‬ٙٓ‫ ف‬offset‫ًح انـ‬ٛ‫ أيا ق‬.PA‫ ػُذ زغاب انـ‬segment‫ كـ‬SS‫غرخذو انـ‬ٚ ‫فؼُذْا‬
.)displacement( ‫ًح انشقى‬ٛ‫ صائذا ً ق‬Base Register‫ًح انـ‬ٛ‫ صائذا ً ق‬Index Register‫انـ‬

Example:

Let DS= 1000H, SS= 50FFH, ES= 9A00H, BX= 20H, SI= 10H, find the value of AX after
executing the following instruction (the memory is given in the question):

MOV AX, [BX + SI + 100H]

67H 10133H

7BH 10132H

ACH 10131H

DH 10130H

81H 1012FH

Solution:

PA= DS * 16 + (BX + SI + 100H) = 10130 H

⸫ AX = AC0DH
Note: if the instruction is modified to: MOV AX, [BP + SI + 100H]

then the PA is calculated using SS instead of DS as the segment.

*****
Notice that all the tables and some examples are from the reference book (The Intel
Microprocessors)- chapter 3.

Best Regards
Dr. Zainab Alomari
Lecture 4:
Exchange Instruction
.‫ يغ تؼضهًا‬destination‫ سىانـ‬source ‫َقىو هزا االَؼاص ترثذَم يحرىَاخ انـ‬

Xchg Register , Register

Register , Memory

Memory , Register

‫ هُا الٌ كم يٍ انقًُرٍُ انًىخىدذٍُ تدىاس االَؼاص‬Immediate addressing mode‫ال َدىص اسرخذاو َىع انـ‬
.‫سىف ذسرثذل يغ االخشي‬

Xchg Memory , Memory NOT Allowed 


Example
Write a piece of code in Assembly language to exchange the contents of DH and DL.

Solution
Xchg DL, DH

Example
Write a piece of code in Assembly language to exchange the contents of DH and DL WITHOUT
using Xchg instruction.

OR: What are the equivalent instructions of Xchg DH, DL?

Solution
MOV AL, DH
MOV DH, DL
MOV DL, AL

Example
Write a piece of code in Assembly language to exchange between the contents of memory
location (FF100H) and AH.

Solution (1)
MOV DX, FF00H

MOV DS, DX

Xchg [100H], AH

Solution (2)

MOV DX, FF00H

MOV DS, DX

MOV BX, 100H

Xchg [BX], AH

Solution (3)

MOV DX, FF00H

MOV DS, DX

MOV AL, AH

MOV AH, [100H]

MOV [100H], AL

Notice that solution (1) is the most efficient solution because:

1. It requires less space in the memory compared to the other two solutions.
2. It uses less number of registers to perform the required operation.

ٌ‫َالحظ اٌ انحم االول اكثش كفائح يٍ انحهٍُ االخشٍَ ورنك الَه َحراج ػذد اقم يٍ االَؼاصاخ وتانرانٍ يساحح اقم نهخض‬
.‫يٍ انزاكشج والَه َسرخذو ػذد اقم يٍ انسدالخ نهقُاو تانؼًهُح انًطهىتح‬

Example
Let DS=1200H and BX= 11AAH, give the new values of all the registers and/or memory
locations that are affected by executing the following instruction:

Xchg [1234H], BX
00H 13235H
(the memory plot is given in the question)
FFH 13234H
Solution
A7H 13233H
We need to find the PA of the memory location:
‫‪PA = DS * 16 + 1234H = 13234H‬‬
‫‪11H‬‬ ‫‪13235H‬‬
‫‪⸫ BXnew = 00FFH‬‬
‫‪AAH‬‬ ‫‪13234H‬‬

‫‪A7H‬‬ ‫‪13233H‬‬

‫‪Translate Instruction‬‬
‫‪XLAT instruction has no destination or source.‬‬

‫قثم اسرخذاو هزا االَؼاص َرى ذدهُض خذول َسًً (‪ )Look up table‬فٍ انزاكشج وهزا اندذول َحرىٌ ػهً قُى نها ػالقح‬
‫يغ ذسهسهها فٍ اندذول‪ ,‬يثالً‪َ :‬كىٌ يحرىي انًىاقغ يٍ اندذول َساوٌ ذشتُغ ذسهسهها أو قًُح َشَذ انىصىل انُها حسة‬
‫انرسهسم انزٌ ذكىٌ يخضوَح فُه‪ .‬ترؼثُش اخش‪َ :‬ؼطٍ ذسهسم يٍ اندذول وَؼطُُا االَؼاص ‪ XLAT‬يحرىي هزا انرسهسم‪ ,‬وَرى‬
‫وضغ هزا انرسهسم فٍ ‪ AL‬قثم اسرذػاء هزا االَؼاص‪.‬‬

‫ػُذ ذُفُز هزا االَؼاص َقىو انًؼانح تدًغ قًُح كم يٍ ‪ BX‬و‪ AL‬تاػرثاسهًا ‪ offset‬أيا انـ ‪ segment‬فهى ‪ DS‬ثى َرى‬
‫انزهاب انً يىقغ يؼٍُ يٍ اندذول وَرى وضغ قًُره فٍ ‪ ,AL‬كانرانٍ‪:‬‬

‫‪[ DS * 16 + BX + AL ]  AL‬‬

‫قثم اسرذػاء االَؼاص ‪َ XLAT‬رى اػطاء ‪ BX‬و‪ DS‬انقُى انرٍ ذخص اول يىقغ فٍ انـ(‪ ,)Look up table‬وتانرانٍ فاَُا‬
‫ارا ثثرُا قًُح ‪ BX‬فاٌ قًُح ‪ AL‬سرؼرثش انًؤشش تحُث ارا كاَد ‪َ 0=AL‬رى انزهاب انً اول يىقغ يٍ انـ(‪)Look up table‬‬
‫وارا ‪َ AL=1‬رى انزهاب انً ثاٍَ يىقغ وهكزا‪ ,‬وانقًُح انرٍ َرى انزهاب انُها ذُقم انً ‪َ AL‬فسها‪.‬‬

‫يالحظح‪ :‬صُغح هزا االَؼاص هٍ فقظ كهًح (‪.)XLAT‬‬


‫‪Example‬‬
‫‪Find the physical address of the memory location that will be used by XLAT instruction‬‬
‫‪if: DS=3000H, BX=100H, AL=3FH.‬‬

‫‪Solution‬‬

‫=‪PA‬‬ ‫‪30000‬‬

‫‪100‬‬

‫‪3F‬‬ ‫‪+‬‬

‫‪3 0 3 3 F H (so this is the PA of the memory location that contain the value‬‬
‫‪that will be given to AL after executing XLAT instruction).‬‬

‫‪Example‬‬
Write a piece of code in Assembly language to find: y=x2 where (0≤x≤15), using XLAT
instruction. Assume that x is a value stored in DH, and the look up table is stored at
8000H:2100H in the Data Segment.
.
Solution .
.
MOV AX, 8000H 10H 82104H
MOV DS, AX
MOV BX, 2100H 09H 82103H
MOV AL, DH 04H 82102H
XLAT
HLT 01H 82101H
00H 82100H

‫ فؼادج يا َرى اسرخذاو هزا‬,‫ ونكٍ هزا انًثال فقظ نهرىضُح‬Mul ‫ كاٌ يٍ انًًكٍ حم انسؤال تاسرخذاو اَؼاص انضشب‬:1‫مالحظة‬
‫االَؼاص ػُذيا الَكىٌ هُانك ػالقح تٍُ انؼُىاٌ وانًحرىي يثالً اٌ َكىٌ انرسهسم هى ذشقُى نهطهثح وانًحرىي هى دسخاخ نهؤالء‬
.‫انطهثح‬

ٍ‫ نهرأشُش انُها وهى َركىٌ ي‬AL ‫ يىقغ فقظ ورنك الَُا َسرخذو‬252 ٌ‫ أ‬28 ‫( هى‬Look up table) ‫ اقصً طىل نهـ‬:2‫مالحظة‬
.‫تد فقظ‬8
x
Q) Write a piece of code in Assembly language to find y=2 where (0≤x≤7), using XLAT
instruction, assuming that x is stored at DS:SI while the look-up table is stored starting at
DS:BX.

DS=4600H, BX=5000H, SI=FF0AH.

‫ غُش يؼطاج واًَا َؼطً سسى انزاكشج‬BX ‫ و‬DS ‫ ًَكٍ اٌ َكىٌ انسؤال تطشَقح اخشي تحُث ذكىٌ قُى انسدالخ‬:‫مالحظة‬
.DS:BX ‫( ًَكٍ يؼشفح قًُح‬Look up table) ‫ويٍ انقُى انًؤششج ػهً انؼُىاٌ الول يىقغ يٍ انـ‬

Questions:
A) Write a piece of code to do each of the following:
1- exchange between AX and the contents of memory locations: 90103H and
90104H.
2- exchange between 2 bytes of data stored at (11000H) in the Stack Segment with
other two bytes of data stored at (50F06H) in the Data Segment.
B) Give the equivalent instructions of XLAT instruction.
Load Effective Address (LEA)
LEA Register(16-bit) , Memory :‫تىجد صيغة واحدة فقط لهذا االيعاز وهي‬

Example

LEA SI, [100H] ; SInew = 100H

LEA DI, [BX+DI+ 5H] ; (Let BX=20H, DI=1000H, then: DI new=1000H+20H+5H=1025H

.‫ارا ً الَرى انزهاب انً انزاكشج تهزا االَؼاص واًَا فقظ َؤخز انؼُىاٌ انًىخىد يا تٍُ االقىاط‬

LEA SI, 100H NOT ALLOWED 


Load DS and Load ES (LDS and LES)
LDS Register(16-bit) , Memory

LDS instruction loads 2bytes from memory to a 16-bit register, then loads the next 2bytes to DS
register.

LES Register(16-bit) , Memory

LES instruction loads 2bytes from memory to a 16-bit register, then loads the next 2bytes to ES
register.

Example
Let DS=1200H, find the values of all the registers that are affected by executing the following
instruction: (the memory plot is given with the question)
.
LDS SI, [200H] .
1BH
.
Solution 13H 12203H

12000 00H 12202H

00H 12201H
200 +
20H 12200H
12200H

⸫ SInew= 0020H, DSnew= 1300H

Notice that in this example, DSold is required to reach the memory and bring the new values of
SI and DS.
Notice that:

LDS AL, [100H] NOT ALLOWED  (Destination is an 8-bit register)

LES AL, [100H]

LDS AX, 100H NOT ALLOWED  (source is not a memory)

LES AX, 100H

Best Regards
Dr. Zainab Alomari
Lecture5: FLAGS Register
FLAGS register is a 16-bits register that contain 9-used bits while the rest bits are not used.
These bits are as follows:

b15 b14 b13 b12 b11 b10 b9 b8 b7 b6 b5 b4 b3 b2 b1 b0


O D I T S Z A P C

Status Flags:
1) Carry Flag (CF):
CF=1 when there is a carry-out or a borrow-in after executing the instruction.
CF=0 otherwise.ss
2) Parity Flag (PF):
PF=1 if the number of ones in the result is even.
PF=0 if the number of ones in the result is odd.
3) Auxiliary Carry Flag (AF):
AF=1 if there is a carry-out into the high nibble from the low nibble, or a borrow-in from
the high nibble into the low nibble of the lower byte of the result.
AF=0 otherwise.
4) Zero Flag (ZF):
ZF=1 when the result of executing an instruction is zero.
ZF=0 when the result of executing an instruction is not zeros.
5) Sign Flag (SF):
SF=1 if the MSB (Most Significant Bit) is 1 (negative number).
SF=0 if the MSB (Most Significant Bit) is 0 (positive number).
Or we can directly say (SF=MSB).
Notice that for unsigned numbers, SF value is neglected.
6) Overflow Flag (OF):
OF=1 if the signed result is out of range. This means that there is a carry added to the
sign bit but no carrys out of the sign bit. (when working with usnsigned numbers, OF
value is neglected).
Example:
127 = 7F = 0111 1111

1+ 1+ 0000 0001 +

+128 0 1000 0000 = -128 NOT CORRECT 


Control Flags:
1) Trap Flag (TF):
If TF is set to 1, it permits executing the program step by step (one instruction at a time).
2) Interrupt Flag (IF):
If IF is 0, all interrupt requests are ignored. If IF is set to 1, interrupts are recognized.
3) Direction Flag (DF):
(Useds with String Instructions).

.flags‫ اليؤثران على الـ‬xchg‫ و‬mov ‫ ايعازي‬:‫مالحظة‬

Best Regards
Dr. Zainab Alomari
Lecture 6: Addition and Subtraction Instructions
1- Addition Instruction
.destination‫ انـ‬ٙ‫جت ف‬ٛ‫ يع بعضًٓا ٔخضٌ انُخ‬destination‫ ٔانـ‬source ‫اث انـ‬ٕٚ‫عاص بجًع يحخ‬ٚ‫قٕو ْزا اال‬ٚ

ADD Destination, Source

Example
ADD AX, BX ; (AX = AX + BX)

All status flags are affected by his instruction (CF, PF, ZF, SF, AF, OF)

.unsigned ٔ‫ أ‬signed ‫ًكٍ اٌ حكٌٕ االسقاو‬ٚ

Example
Set CL to 0FH and CH to 1FH, Find the value of CL and CH with the values of the status flags
after executing the following code:

MOV CL, 0FH

MOV CH, 1FH

ADD CL, CH

Solution
CL = 0 0 0 0 1 1 1 1

CH = 0 0 0 1 1 1 1 1 +

0010 1110

⸫ CLnew= 2EH, CHnew= CHold


Status flags:

CF=0
ZF=0
PF=1
SF=0
AF=1
OF=0
‫‪Examples‬‬

‫يالحظاث يًٓت عُذ انخعايم يع انـ‪:Signed numbers‬‬

‫‪ )1‬عُذ انطشح َأخز انـ‪ 2’s complement‬نهشقى انثاَ‪ ٙ‬ثى َجًع‪.‬‬


‫‪ٚ )2‬خى أخز انـ‪ 2’s complement‬نهشقى يٍ خالل قهب االصفاس انٗ ٔاحذاث ٔانٕاحذاث انٗ اصفاس ثى انجًع يع ‪( 1‬أ‬
‫يًكٍ اسخخذاو طشق اخشٖ)‪.‬‬
‫‪ )3‬ارا كُا َقٕو بجًع اسقاو ‪ٔ signed numbers‬بعذ انجًع كاٌ انـ‪ OF=1‬فًعُاِ أَّ قذ حصم خهم ف‪ ٙ‬انجًع نعذو كفا‪ٚ‬ت‬
‫عذد انبخاث ح‪ٛ‬ث ‪ٚ‬خأثش انـ ‪ sign bit‬أ٘ انـ‪ٔ MSB‬انُخ‪ٛ‬جت حكٌٕ خطأ‪ .‬أيا ارا كاَج االسقاو ‪ unsigned‬فًُٓم ق‪ًٛ‬ت‬
‫انـ‪.OF‬‬
‫‪Example:‬‬
‫‪+48‬‬ ‫‪0011 0000‬‬
‫‪+80‬‬ ‫‪0101 0000 +‬‬

‫‪+128‬‬ ‫= ‪0 1000 0000‬‬ ‫‪-128‬‬

‫َالحظ ف‪ْ ٙ‬زا انًثال اٌ انـ‪ٚ OF‬سأ٘ ‪ 1‬الٌ ُْانك ‪ carry‬يٍ انبج قبم االخ‪ٛ‬ش انٗ بج االشاسة ثى نى ‪ٚ‬كٍ ُْانك‬
‫‪ carry‬يٍ بج االشاسة‪ٔ ,‬بانخان‪ ٙ‬فُٓانك حانت ‪.overflow‬‬

‫‪ )4‬ف‪ ٙ‬حانت جًع االسقاو انـ‪ٔ signed‬كاٌ ُْانك ‪ carry‬ف‪ٛ‬خى اًْانّ‪.‬‬


‫‪Example:‬‬
‫‪+48‬‬ ‫‪0011 0000‬‬
‫‪-48‬‬ ‫‪1101 0000 +‬‬

‫‪0‬‬ ‫= ‪1 0000 0000‬‬ ‫‪0‬‬

‫‪Discard carry‬‬
‫‪ )5‬نًعشفت ق‪ًٛ‬ت انـ‪:signed numbers‬‬
‫‪ .a‬ارا كاٌ انبج االخ‪ٛ‬ش أ بج االشاسة ‪ٚ‬سأ٘ ‪ 0‬يعُاِ اٌ انشقى يٕجب فُقٕو يباششة بحسابّ كق‪ًٛ‬ت يٍ انخحٕ‪ٚ‬م‬
‫يٍ انثُائ‪ ٙ‬انٗ انعشش٘ ٔاالشاسة يٕجبت‪.‬‬
‫‪ .b‬ارا كاٌ انبج االخ‪ٛ‬ش يٍ انشقى ‪ٚ‬سأ٘ ‪ 1‬يعُاِ اٌ انشقى سانب فُقٕو بأخز انـ‪ 2’s complement‬نهشقى (يع‬
‫بج االشاسة) ثى َقٕو با‪ٚ‬جاد ق‪ًٛ‬ت انُاحج ف‪ ٙ‬انُظاو انعشش٘ ٔانخ‪ ٙ‬سخكٌٕ اشاسحٓا سانبت‪.‬‬
‫‪ٚ )6‬خى اسخخذاو َظاو انـ ‪ 2’s complement‬ف‪ ٙ‬االَظًت انحذ‪ٚ‬ثت نخًث‪ٛ‬م االسقاو انسانبت ح‪ٛ‬ث أٌ َظاو انـ ‪1’s‬‬
‫‪ complement‬نذ‪ ّٚ‬يشكهت ف‪ ٙ‬حًث‪ٛ‬م انصفش ح‪ٛ‬ث نّ ق‪ًٛ‬خ‪( ٍٛ‬صفش يٕجب ٔصفش سانب)‪.‬‬

‫‪A figure that shows the difference between 8-bit signed and‬‬
‫‪unsigned numbers‬‬

‫‪2- Add with Carry Instruction‬‬


‫‪ADC destination, source‬‬ ‫ف‪ْ ٙ‬زا اال‪ٚ‬عاص ‪ٚ‬خى انجًع يع اخز ق‪ًٛ‬ت انـ‪ CF‬بُظش االعخباس‪.‬‬

‫‪Examples:‬‬

‫; ‪ADC AX, BX‬‬ ‫)‪(AX = AX + BX + CF‬‬

‫; ‪ADC BL, 5H‬‬ ‫)‪(BL = BL + 5H + CF‬‬

‫‪Notes:‬‬
 All status flags are affected by his instruction (CF, PF, ZF, SF, AF, OF)
 ADC instruction is used when the added numbers are wider than 16-bits.

Example
Add two 32-bit numbers using assembly language instructions, the first number is stored
in (BX AX) and the second number is stored in (DX CX).

Solution
BX AX
ADD AX, CX +
DX CX
ADC BX, DX

HLT BX AX

Example
Add a 32-bit number with 16 bit number, where the first number is in (BX AX) and the
second number is in CX.

Solution
BX AX
ADD AX, CX +
CX
ADC BX, 0

HLT BX AX

Examples

3- Increment Instruction
This instruction adds 1 to a register or memory contents.
Examples

4- Subtraction Instruction
.destination‫ انـ‬ٙ‫جت ف‬ٛ‫ ٔخضٌ انُخ‬destination‫ يٍ انـ‬source ‫اث انـ‬ٕٚ‫عاص بطشح يحخ‬ٚ‫قٕو ْزا اال‬ٚ

SUB Destination, Source

Example
SUB CL, BL ; (CL = CL - BL)

All status flags are affected by his instruction (CF, PF, ZF, SF, AF, OF)

Instead of holding carry:

 CF is used to hold the borrow.


 AF is used to hold the half-borrow.

Example
Write the instructions required to subtract 44H from 22H and put the result in CH.

Solution

MOV CH, 22H

SUB CH, 44H

HLT

Examples
5- Subtract with Borrow Instruction
SBB destination, source )borrow ‫ بُظش االعخباس (باعخباسْا‬CF‫ًت انـ‬ٛ‫خى انطشح يع اخز ق‬ٚ ‫عاص‬ٚ‫ ْزا اال‬ٙ‫ف‬

Examples:

SBB CL, BL ; (CL = CL - BL - CF)

SBB BX, 5H ; (BX = BX - 5H - CF)

Examples

Example
Write a piece of code in Assembly language to compute: Z=X – Y, where:

X is a 32-bit number stored at 92200H,

Y is a 32-bit number stored at 94200H,

Z is stored at 96200H.
Solution

Let: 92200H  9000H:2200H

94200H  9000H:4200H

96200H  9000H:6200H

The three numbers are stored in the data segment with the value of DS equal to 9000H.

MOV AX, 9000H

MOV DS, AX

MOV AX, [2200H]

MOV BX, [2202H]

SUB AX, [4200H]

SBB BX, [4202H]

MOV [6200H], AX

MOV [6202H], BX

HLT

For the same example, if X is stored in the stack segment while Y and Z are in the data segment
(using same addresses):

MOV AX, 9000H

MOV DS, AX

MOV SS, AX

MOV BP, 2200H

MOV AX, [BP]

MOV BX, [BP+2]

SUB AX, [4200H]

SBB BX, [4202H]

MOV [6200H], AX
MOV [6202H], BX

HLT

6- Decrement Instruction
This instruction subtracts 1 from a register or memory contents.

Examples

Notice that the example tables in this lecture are taken from Reference text book 1.

Best Regards
Dr. Zainab Alomari
Lecture 7: Multiplication and Division Instructions
1- Multiplication Instruction
:‫ٌقوم هزا االٌعاص بالضشب باسخخذام الصٍغ الخالٍت‬
MUL operand (for UNSIGNED numbers multiplication)

IMUL operand (for SIGNED numbers multiplication)

where: operand can be a register or a memory.

 If operand is 8-bits, multiplication is performed between AL and the operand and the result
will be 16-bits stored in AX. This is called (8-bits multiplication)
Example:
MUL BL ; (AX = BL * AL) .‫بج‬61 ‫بج ٌساوي‬8* ‫بج‬8 ‫حاصل ضشب‬
IMUL DH ; (AX = DH * AL)
 If operand is 16-bits, multiplication is performed between AX and the operand and the result
will be 32-bits stored in (DX AX). This is called (16-bits multiplication)
Example:
MUL BX ; (DX AX = BX * AX) .‫ بج‬23 ‫بج ٌساوي‬61*‫بج‬61 ‫حاصل ضشب‬
IMUL BP ; (DX AX = BP * AX)

NOTE:

MUL 12H NOT Allowed  (NO immediate multiplication)

MUL DS NOT Allowed  (NO segment registers multiplication)

Examples
Example
Find the result of x2 using Assembly language, if x is an 8-bits signed number stored in the stack
segment at offset (500H). Store the result in CX.

Solution
MOV BP, 500H

MOV AL, [BP]

IMUL AL

MOV CX, AX

HLT

2- Division Instruction
:‫ٌقوم هزا االٌعاص بالقسمت باسخخذام الصٍغ الخالٍت‬

Div operand (for UNSIGNED numbers division)

IDiv operand (for SIGNED numbers division)

where: operand can be a register or a memory.

 If operand is 8-bits: AX is divided by the operand ( ). Result will be stored in AL


and remainder will be stored in AH.
Example:
Div BL ; (AL = , and AH = remainder)

IDiv DH ; (AL = , and AH = remainder)


 If operand is 16-bits: (DX AX) is divided by the operand ( ). Result will be stored
in AX and remainder will be stored in DX.
Example:
Div BX ; (AX = , and DX = remainder)

IDiv SI ; (AX = , and DX = remainder)

NOTE:

Div 12H NOT Allowed  (NO immediate division)

IDiv DS NOT Allowed  (NO segment registers division)

Examples

:‫مالحظة مهمة‬

‫) فيجة تمديد قيمة‬BL ‫ على‬AL ‫تت (مثالً أن يكىن المطلىب تقسيم‬8 ‫تت على‬8 ‫) في حالة أن المطلىب هى تقسيم‬1
:‫ ويتم ذلك تالطريقة التالية‬AX ‫ الى‬AL
1- For unsigned numbers: put zeros in AH.
2- For signed numbers: use CBW.
(CBW=Convert Byte to Word): This instruction extends AL to AX by repeating the MSB
of AL (sign bit) in AH.
‫ بنفس قٍمت‬AH ً‫ ورلك مه خالل ملئ البخاث الموجودة ف‬AX ‫ الى‬AL ً‫ ٌقوم بخمذٌذ القٍمت الموجودة ف‬CBW ‫اٌعاص‬
.‫ باالصفاس‬AH ‫ بالواحذاث وارا كان صفش حمخلئ‬AH ‫ فبالخالً ارا كان هزا البج واحذ حمخلئ‬AL ‫البج االخٍش مه‬

AH AL

‫) فيجة تمديد‬BX ‫ على‬AX ‫تت (مثالً أن يكىن المطلىب تقسيم‬11 ‫تت على‬11 ‫) وفي حالة أن المطلىب هى تقسيم‬2
:‫ ويتم ذلك تالطريقة التالية‬DX AX ‫ الى‬AX ‫قيمة‬
1- For unsigned numbers: put zeros in DX.
2- For signed numbers: use CWD.
(CWD=Convert Word to Double word): This instruction extends AX to DX AX by
repeating the MSB of AX (sign bit) in DX.

DX AX

‫ بنفس قٍمت‬DX ً‫ ورلك مه خالل ملئ البخاث الموجودة ف‬DX ‫ الى‬AX ً‫ ٌقوم بخمذٌذ القٍمت الموجودة ف‬CWD ‫اٌعاص‬
.‫ باالصفاس‬DX ‫ بالواحذاث وارا كان صفش حمخلئ‬DX ‫ فبالخالً ارا كان هزا البج واحذ حمخلئ‬AX ‫البج االخٍش مه‬

Example

Write the Assembly language instructions required to find the result of dividing the byte stored at
DS:BX by the byte stored in the next location. Store the result and remainder in the locations
next to the two numbers. Assume the two numbers are:

1) Unsigned
2) Signed
Solution

1) If numbers are unsigned:

MOV AL, [BX]


MOV AH, 0H
DIV Byte ptr[BX+1]
MOV [BX+2], AX
HLT
2) If numbers are signed:

MOV AL, [BX]


CBW
iDIV Byte ptr[BX+1]
MOV [BX+2], AX
HLT
Note: Some of the applications that may need the division operation are: calculating the average,
checking if a number is even, checking if a number is prime, etc.

Example

What is the result of executing the following code:

MOV DX, 99H


MOV AL, 0A1H
CBW
CWD
HLT

Solution

AXnew = FFA1H, DXnew = FFFFH

*-*-*-*-*-*-*

Q1) Write the equivalent instructions of:

1) CBW
2) CWD

Q2) Write the Assembly language instructions required to find: Z=X/Y, where X and Y are 16-
bit numbers stored in BX and CX, respectively. Store the result at 91F00H in the data segment,
and store the remainder at 55A80H in the stack segment. Assume that X and Y are:

1) Unsigned numbers
2) Signed numbers

Notice that the example tables in this lecture are taken from Reference text book 1.

Best Regards
Dr. Zainab Alomari
Lecture 8: JUMP Instruction
1- Unconditional Jump
JMP operand(16-bits) ; operand can be any 16-bit value (immediate, register or memory)

This instruction is used to specify the location of the next instruction to be executed.

‫حصم قفض انٗ يكاٌ اخش‬ٚ ٙ‫زِ فثانران‬ٛ‫رى ذُف‬ٛ‫ انز٘ س‬ٙ‫عاص انران‬ٚ‫ؤشش انٗ اال‬ٚ ٘‫ًح انًؤشش انز‬ٛ‫ش ق‬ٛٛ‫عاص ترغ‬ٚ‫قٕو ْزا اال‬ٚ
.‫داخم أ خاسج انثشَايج‬

.flags‫عاص عهٗ ا٘ يٍ انـ‬ٚ‫ؤثش ْزا اال‬ٚ‫ ال‬:‫مالحظة‬

Types of Jump operation:

1) Intrasegment Jump
In this type, the value of the new IP is directly given by the instruction. CS is not changed.

Examples

JMP 1234H ; (IPnew = 1234H)

JMP BX ; (IPnew = BX)

JMP BL NOT Allowed  (operand must be 16-bits)


JMP DS NOT Allowed  (operand can’t be a segment register)
Examples

Let BX= 1000H, IP=100H, find the value of IP after executing the following instruction:
1342H:0100H JMP [BX] ; notice that 1342H:0100H is the location (CS:IP) where
the instruction is stored.

Solution
02H DS: 1001H
IPnew= 200H
00H DS: 1000H
⸫ The next location is stored at (1342H:0200H).
A7H DS: 0FFFH
NOTE: any type of memory addressing modes can be used here, example:

JMP [SI+1]

JMP [DI+BX+3]

‫ اخزَاْا‬ٙ‫ انر‬addressing modes‫ ٔتاسرخذاو ا٘ َٕع يٍ إَاع انـ‬IP‫ذج نهـ‬ٚ‫ًح انجذ‬ٛ‫ًكٍ اسرخذاو انزاكشج نجهة انق‬ٚ :‫مالحظة‬
ٍٛ‫ر‬ٚ‫رى اخز تا‬ٛ‫ فس‬jmp ْٕ ‫عاص‬ٚ‫) الَّ طانًا اال‬word ptr(‫ًح انًأخٕرج يٍ انزاكشج تـ‬ٛ‫ذ حجى انق‬ٚ‫ ٔتذٌٔ انحاجح انٗ ذحذ‬,ً‫ساتقا‬
.IP ٙ‫يٍ انزاكشج ٔٔضعٓا ف‬

2) Intersegment Jump
:ٍٛ‫ر‬ٛ‫ٍ انران‬ٛ‫قر‬ٚ‫ ٔرنك تاسرخذاو احذٖ انطش‬IPٔCS ‫ش كم يٍ انـ‬ٛٛ‫رى ذغ‬ٚ ‫ ْزا انُٕع‬ٙ‫ف‬

:ٍٛ‫ر‬ٛ‫ٍ انران‬ٛ‫غر‬ٛ‫عاص ٔرنك تاحذٖ انص‬ٚ‫ اال‬ٙ‫ تشكم يثاشش ف‬IP ٔ CS ‫ًح‬ٛ‫رى اعطاء ق‬ٚ )1

JMP FAR CSnew: IPnew

JMP FAR PTR CSnew: IPnew

Example:

JMP FAR 4000H:200H ; CSnew=4000H, IPnew =200H

:‫ح‬ٛ‫غح انران‬ٛ‫ يٍ انزاكشج تاسرخذاو انص‬IP ٔ CS ‫ًح‬ٛ‫رى اخز ق‬ٚ )2

JMP Dword ptr [ ] ; any addressing mode can be used here.

Examples:

JMP Dword ptr [71FFH]

JMP Dword ptr [BX]

JMP Dword ptr [SI+500H]

.CS‫ٍ ذعطٗ نهـ‬ٛ‫ر‬ٚ‫ تا‬َٙ‫ ٔثا‬IP‫ٍ يُٓا ذعطٗ نهـ‬ٛ‫ر‬ٚ‫ أٔل تا‬,‫راخ يٍ انزاكشج‬ٚ‫رى اخز استع تا‬ٛ‫ ْزِ انحانح س‬ٙ‫ ف‬:‫مالحظة‬

Example

Let BX= 4000H, DS= 2000H, find the new values of CS, IP and BX after executing the
following instruction (the memory is given in the question):

8000H:0100H JMP Dword ptr[BX]

Solution 90H 2000H:4003H


CShi
IPnew = 1000H 00H 2000H:4002H
CSlow
10H 2000H:4001H
IPhi
00H 2000H:4000H
IPlow
CSnew = 9000H

BXnew= BXold

1- Conditional Jump
.‫رى انقفض انٗ انًٕقع انًطهٕب‬ٚ‫رى أ ال‬ٛ‫ًح س‬ٛ‫ٍ ٔحسة انق‬ٛ‫ًح عايم أ تد يع‬ٛ‫ ذعرًذ عهٗ ق‬ٙ‫ إَاع انقفض انر‬ْٙ

Conditional Jump Instructions

Note:

(Above and Below) are used with Unsigned numbers.


(Greater and Less) are used with Signed numbers.

Example

Write the assembly language instructions required to copy the contents of 10 memory locations
(from 9A000H to 9A009H) to the memory locations that start at 96050H.

Solution

MOV AX, 9000H


MOV DS, AX
MOV BX, A000H
MOV SI, 6050H
MOV CX, 0AH
N1 : MOV AL, [BX]
MOV [SI], AL
INC BX
INC SI
DEC CX
JNZ N1
HLT

ANOTHER SOLUTION:

MOV AX, 9000H


MOV DS, AX
MOV BX, 0H
MOV CX, 0AH
N1 : MOV AL, [BX+A000H]
MOV [BX+6050H], AL
INC BX
DEC CX
JNZ N1
HLT

Loop Instructions:
1) LOOP instruction:

LOOP N1

This instruction decrements CX and jumps if (CX ≠ 0) to N1.


)‫ هى فقط المطلىب من الطلبة اما النىعين التاليين فهما لالطالع فقط‬loop ‫ النىع االول من ايعازات‬:‫(مالحظة‬

2) LOOPE and LOOPZ instructions:

LOOPE N1

LOOPZ N1

These instructions decrement CX and check both of CX and ZF as follows:

if (CX ≠ 0 and ZF = 1) jump to N1.

3) LOOPNE and LOOPNZ instructions:

LOOPNE N1

LOOPNZ N1

These instructions decrement CX and check both of CX and ZF as follows:

if (CX ≠ 0 and ZF = 0) jump to N1.

*-*-*-*-*-*-*

Questions:

1) Re-solve the last example, with replacing (JNZ) instruction with (LOOP)
instruction.
2) Write the equivalent instructions of (LOOP NXT).

Best Regards
Dr. Zainab Alomari
‫‪Lecture 9: Logic Instructions‬‬
‫‪1- OR Instruction‬‬
‫‪OR destination, source‬‬

‫‪=A‬‬
‫‪=1‬‬

‫ٌقىو هزا االٌعاص بعًم ‪ Logic OR‬بٍٍ كم بج يٍ انـ‪ destination‬يع انبج انزي ٌقابهه يٍ انـ‪ source‬وٌخى خضٌ انُخٍجت‬
‫فً انـ‪ .destination‬يٍ انُظش انى انـ‪ Truth Table‬انخاصت ببىابت ‪َ OR‬الحظ بأَه‪:‬‬

‫عُذ عًم ‪ OR‬نـ‪ A‬يع صفش فانُخٍجت هً ‪.A‬‬ ‫‪-‬‬


‫عُذ عًم ‪ OR‬نـ‪ A‬يع واحذ فانُخٍجت هً ‪.1‬‬ ‫‪-‬‬

‫بًعُى أَه ًٌكُُا االسخفادة يٍ اٌعاص ‪ OR‬ارا اسدَا جعم بج يعٍٍ ٌساوي واحذ )‪ (set‬يع انًحافظت عهى باقً انبخاث بذوٌ‬
‫حغٍٍش‪ ,‬كًا فً انًثال انخانً‪:‬‬

‫‪Example‬‬
‫‪Set the MSB and LSB of BX using Assembly language.‬‬

‫‪Solution‬‬ ‫‪xxxx xxxx xxxx xxxx‬‬

‫‪OR BX, 8001H‬‬ ‫‪1000 0000 0000 0001‬‬ ‫‪OR‬‬

‫‪HLT‬‬ ‫‪1xxx xxxx xxxx xxx1‬‬


‫مالحظة‪ :‬انحم هى َفسه يهًا كاَج قًٍت ‪ BX‬الحؤثش‪ ,‬ونهزا كخبُاها بشكم ‪ x‬وانخً حعًُ (‪.)don’t care condition‬‬

‫‪Examples‬‬
‫‪2- AND Instruction‬‬
‫‪AND destination, source‬‬

‫‪=0‬‬
‫‪=A‬‬

‫ٌقىو هزا االٌعاص بعًم ‪ Logic AND‬بٍٍ كم بج يٍ انـ‪ destination‬يع انبج انزي ٌقابهه يٍ انـ‪ source‬وٌخى خضٌ‬
‫انُخٍجت فً انـ‪ .destination‬يٍ انُظش انى انـ‪ Truth Table‬انخاصت ببىابت ‪َ AND‬الحظ بأَه‪:‬‬

‫عُذ عًم ‪ AND‬نـ‪ A‬يع صفش فانُخٍجت هً ‪.0‬‬ ‫‪-‬‬


‫عُذ عًم ‪ AND‬نـ‪ A‬يع واحذ فانُخٍجت هً ‪.A‬‬ ‫‪-‬‬

‫بًعُى أَه ًٌكُُا االسخفادة يٍ اٌعاص ‪ AND‬ارا اسدَا جعم بج يعٍٍ ٌساوي صفش )‪ (reset‬يع انًحافظت عهى باقً انبخاث بذوٌ‬
‫حغٍٍش‪ ,‬كًا فً انًثال انخانً‪:‬‬

‫‪Example‬‬
‫‪Reset the MSB and LSB of AX using Assembly language.‬‬

‫‪Solution‬‬ ‫‪xxxx xxxx xxxx xxxx‬‬

‫‪AND AX, 7FFEH‬‬ ‫‪0111 1111 1111 1110‬‬ ‫‪AND‬‬

‫‪HLT‬‬ ‫‪0xxx xxxx xxxx xxx0‬‬


‫مالحظة‪ :‬انحم هى َفسه يهًا كاَج قًٍت ‪ AX‬الحؤثش‪ ,‬ونهزا كخبُاها بشكم ‪ x‬وانخً حعًُ (‪.)don’t care condition‬‬

‫‪Examples‬‬
‫‪3- XOR Instruction‬‬
‫‪XOR destination, source‬‬

‫‪=A‬‬
‫‪=A‬‬

‫ٌقىو هزا االٌعاص بعًم ‪ Logic XOR‬بٍٍ كم بج يٍ انـ‪ destination‬يع انبج انزي ٌقابهه يٍ انـ‪ source‬وٌخى خضٌ‬
‫انُخٍجت فً انـ‪ .destination‬يٍ انُظش انى انـ‪ Truth Table‬انخاصت ببىابت ‪َ XOR‬الحظ بأَه‪:‬‬

‫عُذ عًم ‪ XOR‬نـ‪ A‬يع صفش فانُخٍجت هً ‪.A‬‬ ‫‪-‬‬


‫عُذ عًم ‪ XOR‬نـ‪ A‬يع واحذ فانُخٍجت هً ‪.A‬‬ ‫‪-‬‬

‫بًعُى أَه ًٌكُُا االسخفادة يٍ اٌعاص ‪ XOR‬ارا اسدَا قهب بج يعٍٍ يع انًحافظت عهى باقً انبخاث بذوٌ حغٍٍش‪ ,‬كًا فً انًثال‬
‫انخانً‪:‬‬

‫‪Example‬‬
‫‪Complement the MSB and LSB of DX using Assembly language.‬‬

‫‪Solution‬‬ ‫‪xxxx xxxx xxxx xxxx‬‬

‫‪XOR DX, 8001H‬‬ ‫‪1000 0000 0000 0001‬‬ ‫‪XOR‬‬

‫‪HLT‬‬ ‫‪xxxx xxxx xxxx xxxx‬‬


‫مالحظة‪ :‬انحم هى َفسه يهًا كاَج قًٍت ‪ DX‬الحؤثش‪ ,‬ونهزا كخبُاها بشكم ‪ x‬وانخً حعًُ (‪.)don’t care condition‬‬

‫‪Examples‬‬
4- NOT Instruction
NOT operand ; (this instruction takes the 1’s complement of the operand)

where: operand can be a register or a memory.

A A

Example
What is the value of AL after executing the following code:

MOV AL, 55H


AND AL, 1FH
OR AL, 0C0H
XOR AL, 0FH
NOT AL
HLT
Solution

ALnew = 25H

5- Negaitve Instruction
NEG operand ; (this instruction takes the 2’s complement of the operand)

where: operand can be a register or a memory.

:ً‫ وه‬Neg ‫ ٌىجذ ثالد طشق نهقٍاو بانعًم انًكافئ نعًم اٌعاص‬:NEG ‫االيعبزات المكبفئة اليعبز‬

1 ‫ ثى انجًع يع‬NOT -1
1 ‫ ثى انجًع يع‬FFFFH ‫ يع‬XOR -2
‫) انشقى يٍ انصفش فبانخانً سخكىٌ انُخٍجت سانب هزا انشقى‬sub( ‫ عًهٍت طشح‬-3
6- Test Instruction
TEST destination, source

This instruction performs AND between destination and source, WITHOUT affecting destination
(only flags are affected by the result):

Examples

NOTE: Test instruction is usually used to test a bit or more, depending on the Zero Flag (ZF):

ZF=0 if the bit under test is not zero.

ZF=1 if the bit under test is zero.

The required bit is usually tested against immediate number, example:

1 to test bit 0 (TEST AL, 01H)

2 to test bit 1 (TEST AL, 02H)

4 to test bit 2 (TEST AL, 04H)

8 to test bit 3 (TEST AL, 08H)

and so on.

Example
Find the value of the ZF after executing the following instruction:

TEST byte ptr[1000H], 80H

Solution 5AH DS:1001H

ZF= 0 90H DS:1000H

This result means that the value of the bit under test is 1 (which is the MSB or b7).
Example
Find the number of even values in a block of 50 16-bit signed numbers stored in the memory
starting at DS:DI. Store the result in BL.

Compare Instruction
CMP destination, source

This instruction performs SUB (destination – source) without affecting the destination (only
flags are affected). CMP is usually followed with a conditional jump.

.flags‫ واًَا فقط حؤثش انُخٍجت عهى انـ‬destination‫ ونكٍ الٌغٍش انـ‬SUB ‫ٌقىو هزا االٌعاص بعًم طشح أي َفس عًم اٌعاص‬

Examples

:‫ كًا فً االيثهت انخانٍت‬,‫ باٌعاص يٍ اٌعاصاث انقفض انًششوط‬CMP ‫ عادة ً يا ٌخبع اٌعاص‬:‫مالحظة‬

Examples
CMP AX, BX

JE N1

-------------------------

CMP CX, 10H

JA N1

-------------------------

Example
Write the assembly language instructions required to copy the contents of 10 memory locations
(from 9A000H to 9A009H) to the memory locations that start at 96050H.

.)CMP ‫(هزا انًثال يىجىد فً انًحاضشة انسابقت ونكٍ هُا سُعٍذ انحم باسخخذاو اٌعاص‬

Solution

MOV AX,9000H
MOV DS, AX
MOV BX, 0
N1: MOV AL, [BX+A000H]
MOV [BX+6050H], AL
INC BX
CMP BX, 0AH
JNZ N1
HLT
Example

Write the assembly language instructions required to find the square of 20 8-bits signed numbers
stored in the memory starting at address 8E000H,. Store the results in memory locations starting
at 81000H.
Solution
MOV AX, 8000H
MOV DS, AX
MOV BX, 0H
MOV SI, 0H
NEXT:MOV AL, [BX+E000H]
IMUL AL ;( AX=AL*AL)
MOV [SI+1000H], AX
ADD SI, 2
INC BX
CMP BX, 14H ; or CMP BX, 20
JNZ NEXT
HLT
Notice that the example tables in this lecture are taken from Reference text book 1.

Best Regards
Dr. Zainab Alomari
Lecture 10: Stack Instructions
The stack instructions are important instructions that store and retrieve data from the LIFO
(last-in, first-out) stack memory. Stack instructions are: PUSH and POP instructions.

1- PUSH Instruction
PUSH operand ‫بج في السخاك‬61 ‫يقىم هذا االيعاس بخشى‬

This instruction stores the (operand) value in the stack, where (operand) is any 16-bit value.

Operand can be: 16-bit register


Segment register (CS, DS, ES and SS)
Immediate value
Memory
Examples:
Push BX
Push [DI]
Push 15H
Push DS

:‫ يخن خشى البايخاث في الذاكزة كالخالي‬:‫مالحظة‬

Stack

SS:SP

SS:SP-1 High Byte

SS:SP-2 Low Byte

After storing the 16-bits into the stack, SP is decremented by 2 (SPnew = SP-2).

‫ الوىقع الحالي ال يخن الخشى فيه واًوا يخن‬:‫ بج بالطزيقت الخاليت‬61‫ يخن خشى الـ‬SS:SP ‫باسخخذام الذاكزة الوؤشز عليها باسخخذام‬
‫ وبعذ االًخهاء‬,low‫ هزة ثاًيت وخشى البايج الـ‬6 ‫ بوقذار‬SP ‫ ثن حٌقيص‬high‫ ثن يخن خشى البايج الـ‬6 ‫ بوقذار‬SP ‫حٌقيص الوؤشز‬
.)SP-2( ‫ الجذيذة هي‬SP ‫ سخكىى قيوت‬Push ‫هي حٌفيذ ايعاس‬
Example

Example:
Push AX
SS:SP
Solution
SS:SP-1
AH
AH  SS:[SP-1] SS:SP-2
AL
AL  SS:[SP-2]

SPnew = SP - 2
Example:
Let BX=100H, SP=500H, SS=1FF0H what is the result of executing the following instruction
(the memory is given with the question):

Push [BX]
33H DS:101H

A0H DS:100H

1FH DS:FFH

75H DS:FEH

Solution (Data Segment)

2 bytes will be taken from (DS:BX) to the stack.

These two bytes are (33A0H)

SPnew = 500H -2 =4FEH


1FF0H:500H
33H 1FF0H:4FFH

A0H 1FF0H:4FEH

(Stack Segment)

2- POP Instruction
POP operand operand ‫بج هي السخاك وخشًها بذاخل الـ‬61 ‫يقىم هذا االيعاس باخزاج‬

This instruction takes 16-bits from the stack, and puts it in the operand.

Operand can be: 16-bit register 8-bit registers are NOT allowed
Segment register (DS, ES and SS) CS is NOT allowed
Memory
Examples:
POP BX
POP [DI]
POP DS
‫‪POP 15H‬‬ ‫‪NOT Allowed ‬‬

‫مالحظة‪ :‬يخن أخذ البايخاث هي السخاك كالخالي‪:‬‬

‫‪Stack‬‬

‫‪SS:SP+2‬‬

‫‪SS:SP+1‬‬ ‫‪High Byte‬‬

‫‪SS:SP‬‬ ‫‪Low Byte‬‬

‫‪After taking the 16-bits from the stack, SP is increased by 2 (SPnew = SP+2).‬‬

‫باسخخذام الذاكزة الوؤشز عليها باسخخذام ‪ SS:SP‬يخن أخذ ‪61‬بج بالطزيقت الخاليت‪ :‬يخن أخذ البايج الـ‪ low‬هي الوىقع الحالي ثن‬
‫يخن سيادة الوؤشز ‪ SP‬بوقذار ‪ 6‬ثن يخن أخذ البايج الـ‪ , high‬ثن سيادة الوؤشز ‪ SP‬بوقذار ‪ 6‬هزة ثاًيت‪ ,‬وبعذ االًخهاء هي حٌفيذ‬
‫ايعاس ‪ POP‬سخكىى قيوت ‪ SP‬الجذيذة هي (‪.)SP+2‬‬

‫‪Example:‬‬
‫‪POP BX‬‬
‫‪SS:SP+2‬‬
‫‪Solution‬‬
‫‪SS:SP+1‬‬
‫‪BH‬‬
‫]‪BL  SS:[SP‬‬ ‫‪SS:SP‬‬
‫‪BL‬‬
‫]‪BH  SS:[SP+1‬‬

‫‪SPnew = SP + 2‬‬
Example:

Example:
Push AX

POP DX = MOV DX, AX

NOTE: the Push and POP instructions can be used to save the values of registers before using
them in some tasks.

Example:
If we are required to use AX and DX in Div or Mul instructions, and they contain important
information, we can use stack instructions as follows:
Push AX

Push DX

------

------ (perform required task here using AX and DX)

------

POP DX

POP AX

Example:
Exchange between AX and BX using stack instructions.

Solution

Push AX

Push BX

POP AX

POP BX

HLT

3- PUSHF and POPF Instructions


PUSHF saves the two bytes of the FLAGS register into the stack.

POPF takes two bytes from the stack and puts them in the FLAGS register.

Example:
Save the contents of FLAGS register in DX.

Solution

PUSHF

POP DX

HLT
Example:
Clear all the bits of the FLAGS register.

Solution

MOV DX, 0

PUSH DX

POPF

HLT

Example:
Clear the high byte of the FLAGS register.

Solution

PUSHF

POP DX

MOV DH,00

PUSH DX

POPF

HLT

Example:
Write the required instructions to set the interrupt flag:

Solution:

PUSHF

POP AX

OR AX, 0200H

PUSH AX

POPF

HLT
Flags Control Instructions
LAHF (AH  FLAGS register lower byte)

SAHF (AH  FLAGS register lower byte)

Example:
Save the current contents of the low byte of the FLAGS register into ES:BX, then load this byte
with a new value from DS:SI.

Solution

LAHF

MOV ES:[BX], AH

MOV AH, [SI]

SAHF

HLT

Other instructions that allow changing a single bit in the FLAGS register without affecting other
bits:

CLC (clear carry flag CF)

STC (set carry flag CF)

CMC (complement carry flag CF)

CLI (clear Interrupt flag IF)

STI (set interrupt flag IF)

Notice that some examples in this lecture are taken from Reference text book 1.

Best Regards
Dr. Zainab Alomari
Lecture 11: Shift and Rotate Instructions
1- Shift Instructions

1) Logical Shift

Shifting to the left:

SHL Operand, shift ; shifting the operand to the left

.)shift(‫) انى انٍساس ٔعذد يشاذة انرضحٍف ٌحذدِ انـ‬operand(‫ٌقٕو ْزا االٌعاص ترضحٍف انثراخ انًٕجٕدج فً انـ‬

.‫تد‬61 ٔ‫تد أ‬8 ‫ ًٌٔكٍ اٌ ٌكٌٕ تطٕل‬,‫ سجالً أٔ راكشج‬Operand‫ًٌكٍ اٌ ٌكٌٕ انـ‬ -
.ً‫ حصشا‬CL ‫) أٔ يخضَٔا ً فً سجم‬immediate value( ‫ ٌكٌٕ إيا‬shift‫يقذاس انـ‬ -
.ًٍٍٍ‫ٌرى اضافح اصفاس فً انثراخ انرً سرفشغ يٍ جٓح ان‬ -
.CF‫ٌرى ٔضع اخش تد ذى ذضحٍفّ فً انـ‬ -

Example:
Let AX=877FH , what is the new value of AX after executing the following instruction:

SHL AX, 1

Solution
MSB LSB
0
CF AX

AXold = 1000 0111 0111 1111 b

AXnew = 0000 1110 1111 1110 b

= 0EFE H

ZF = 0 , CF = 1 (CF = last shifted bit)

Example:
Let AX=877FH , what is the new value of AX after executing the following instructions:

MOV CL, 10H


SHL AX, CL
Solution

AXold = 1000 0111 0111 1111 b

AXnew = 0000 0000 0000 0000 b

= 0H

ZF = 1, CF = 1 (CF = last shifted bit = LSB)


n
Note: shifting a number to the left by n bits equals to multiplying the number by 2 (used with
unsigned numbers)

:‫ كًا فً االيثهح االذٍح‬,‫عًهٍح ذشفٍد انى انٍساس ذعادل ضشب انشقى تًقذاس ٌعرًذ عهى عذد تراخ انرضحٍف‬

SHL AX, 1 ; to multiply AX by 2


SHL AX, 2 ; to multiply AX by 4
SHL AX, 3 ; to multiply AX by 8

Shifting to the right:

SHR Operand, shift ; shifting the operand to the right

.)shift(‫) انى انًٍٍٍ ٔعذد يشاذة انرضحٍف ٌحذدِ انـ‬operand(‫ٌقٕو ْزا االٌعاص ترضحٍف انثراخ انًٕجٕدج فً انـ‬

.‫تد‬61 ٔ‫تد أ‬8 ‫ ًٌٔكٍ اٌ ٌكٌٕ تطٕل‬,‫ سجالً أٔ راكشج‬Operand‫ًٌكٍ اٌ ٌكٌٕ انـ‬ -
.ً‫ حصشا‬CL ‫) أٔ يخضَٔا ً فً سجم‬immediate value( ‫ ٌكٌٕ إيا‬shift‫يقذاس انـ‬ -
.‫ٌرى اضافح اصفاس فً انثراخ انرً سرفشغ يٍ جٓح انٍساس‬ -
.CF‫ٌرى ٔضع اخش تد ذى ذضحٍفّ فً انـ‬ -

Example:
Let AX=8F01H , what is the new value of AX after executing the following instruction:

SHR AX, 3H

Solution

AXold = 1000 1111 0000 0001 b

AXnew = 0001 0001 1110 0000 b

= 11E0H

ZF = 0 , CF = 0 (CF = last shifted bit)


Example:
Let AX=8F01H , what is the new value of AX after executing the following instruction:

SHR AX, 11H

Solution

(11H = 17d)

AXold = 1000 1111 0000 0001 b

AXnew = 0000 0000 0000 0000 b

= 0H

ZF = 1, CF = 0 (CF = last shifted bit)


n
Note: shifting a number to the right by n bits equals to dividing the number by 2 (used with
unsigned numbers)

:‫ كًا فً االيثهح االذٍح‬,‫عًهٍح ذشفٍد انى انًٍٍٍ ذعادل قسًح انشقى تًقذاس ٌعرًذ عهى عذد تراخ انرضحٍف‬

SHR AX, 1 ; to divide AX by 2


SHR AX, 2 ; to divide AX by 4
SHR AX, 3 ; to divide AX by 8

2) Arithmetic Shift

signed ‫ذسرخذو عُذ انرعايم يع اسقاو‬


Arithmetic Shift to the Left:

SAL = SHL (exactly the same)

Arithmetic Shift to the Right:

SAR operand, shift

.signed‫أٌضا ً ٌرى انرضحٍف انى انًٍٍٍ تٓزا االٌعاص ٔنكٍ ٌسرخذو يع االسقاو انـ‬

.‫تد‬61 ٔ‫تد أ‬8 ‫ ًٌٔكٍ اٌ ٌكٌٕ تطٕل‬,‫ سجالً أٔ راكشج‬Operand‫ًٌكٍ اٌ ٌكٌٕ انـ‬ -
.ً‫ حصشا‬CL ‫) أٔ يخضَٔا ً فً سجم‬immediate value( ‫ ٌكٌٕ إيا‬shift‫يقذاس انـ‬ -
,‫) فً انثراخ انرً سرفشغ يٍ جٓح انٍساس‬MSB( ‫تذل ٔضع اصفاس انى جٓح انٍساس ٌرى ذكشاس قًٍح انثد االخٍش‬ -
.‫ فاٌ ْزِ انعًهٍح سرحافظ عهى تد االشاسج‬signed ‫ٔتانرانً ارا كاٌ انشقى‬
.CF‫ٌرى ٔضع اخش تد ذى ذضحٍفّ فً انـ‬ -
Example:
Let AX=8000H , what is the new value of AX after executing the following instruction:

SAR AX, 10H

Solution

(10H = 16d)

AXold = 1000 0000 0000 0000 b

AXnew = 1111 1111 1111 1111 b

= FFFF H

ZF = 0, CF = 1 (CF = last shifted bit)

‫ يا‬,‫ ارا كاٌ يقرذاس انرشفٍد اكثش يٍ عذد انثراخ‬CF‫َالحظ تاٌ جًٍع اٌعاصاخ انرشفٍد االستعح ذقٕو تٕضع صفش فً انـ‬
.CF‫ ٌقٕو تٕضع تد االشاسج فً انـ‬SAR ‫عذا اٌعاص‬

Example:
Let AX=FC44H , what is the new value of AX after executing the following instruction:

MOV CL, 12H

SAR AX, CL

Solution

(12H = 18d)

AXold = 1111 1100 0100 0100 b

AXnew = 1111 1111 1111 1111 b

= FFFF H

ZF = 0, CF = 1 (CF = last shifted bit)

(In this example, Let AXold = 7FFFH  AXnew = 0000H, CF=0, ZF=1)

Examples
2- Rotate Instructions
1) Rotate Left:

ROL operand, rotation ; Rotating the operand to the left

.)rotation(‫) انى انٍساس ٔعذد يشاذة انرضحٍف ٌحذدِ انـ‬operand(‫ٌقٕو ْزا االٌعاص ترذٌٔش انثراخ انًٕجٕدج فً انـ‬

.‫تد‬61 ٔ‫تد أ‬8 ‫ ًٌٔكٍ اٌ ٌكٌٕ تطٕل‬,‫ سجالً أٔ راكشج‬Operand‫ًٌكٍ اٌ ٌكٌٕ انـ‬ -
.ً‫ حصشا‬CL ‫) أٔ يخضَٔا ً فً سجم‬immediate value( ‫ ٌكٌٕ إيا‬rotation‫يقذاس انـ‬ -
.ًٍٍٍ‫ٌرى انرذٌٔش تحٍث ذٕضع انثراخ انًضحفح يٍ انٍساس فً يكاٌ انثراخ انرً سرفشغ يٍ جٓح ان‬ -
.CF‫ٌرى ٔضع اخش تد ذى ذذٌٔشِ فً انـ‬ -

Example
Let AX=C010H , what is the new value of AX after executing the following instruction:

ROL AX, 3

Solution
1100 0000 0001 0000

AXnew = 0000 0000 1000 0110 B

=0086H

CF = 0, ZF = 0

Example
What is the new value of AL after executing the following instruction:

ROL AL, 8

Solution

ALnew = ALold

CF = last rotated bit = LSB


Example
What is the new value of AX after executing the following instruction:

ROL AX, 10H

Solution

AXnew = AXold

CF = last rotated bit = LSB

2) Rotate Right:

ROR operand, rotation ; Rotating the operand to the right

.)rotation(‫) انى انًٍٍٍ ٔعذد يشاذة انرضحٍف ٌحذدِ انـ‬operand(‫ٌقٕو ْزا االٌعاص ترذٌٔش انثراخ انًٕجٕدج فً انـ‬

.‫تد‬61 ٔ‫تد أ‬8 ‫ ًٌٔكٍ اٌ ٌكٌٕ تطٕل‬,‫ سجالً أٔ راكشج‬Operand‫ًٌكٍ اٌ ٌكٌٕ انـ‬ -
.ً‫ حصشا‬CL ‫) أٔ يخضَٔا ً فً سجم‬immediate value( ‫ ٌكٌٕ إيا‬rotation‫يقذاس انـ‬ -
.‫ٌرى انرذٌٔش تحٍث ذٕضع انثراخ انًضحفح يٍ انًٍٍٍ فً يكاٌ انثراخ انرً سرفشغ يٍ جٓح انٍساس‬ -
.CF‫ٌرى ٔضع اخش تد ذى ذذٌٔشِ فً انـ‬ -

Example
Let AL=F0H , what is the new value of AL after executing the following instruction:

ROR AL, 4

Solution

ALold = 1111 0000 b

ALnew = 0000 1111 b = 0FH

CF = 0

‫ تاسرخذاو االٌعاص‬Xchg ‫ تذٌٔ انحاجح السرخذاو اٌعاص‬AH ‫ يع‬AL ‫َالحظ أَّ ًٌكٍ االسرفادج يٍ ْزا االٌعاص فً ذثذٌم‬
.)ROL Ax, 8( ٔ‫) أ‬ROR Ax, 8( :ً‫انران‬

3) Rotate with Carry Left:

RCL Operand, rotation


‫فشق ْزا االٌعاص عٍ ‪ ْٕ ROL‬أَّ ٌقٕو تانرذٌٔش يعرثشا ً انـ‪ CF‬يٍ ضًٍ انثراخ انرً سٍرى ذذٌٔشْا انى انٍساس‪ ,‬تانرانً سٍرى‬
‫اسرخذاو قًٍرٓا انقذًٌح عهى آَا أل تد ٌرى ذذٌٔشِ ٔفً انُٓاٌح سرحرٕي عهى قًٍح اخش تد ذى ذذٌٔشِ‪.‬‬

‫‪CF‬‬

‫‪4) Rotate with Carry Right:‬‬

‫‪RCR Operand, rotation‬‬

‫فشق ْزا االٌعاص عٍ ‪ ْٕ ROR‬أَّ ٌقٕو تانرذٌٔش يعرثشا ً انـ‪ CF‬يٍ ضًٍ انثراخ انرً سٍرى ذذٌٔشْا انى انًٍٍٍ‪ ,‬تانرانً سٍرى‬
‫اسرخذاو قًٍرٓا انقذًٌح عهى آَا أل تد ٌرى ذذٌٔشِ ٔفً انُٓاٌح سرحرٕي عهى قًٍح اخش تد ذى ذذٌٔشِ‪.‬‬

‫‪CF‬‬

‫‪Example‬‬
‫‪Let AX=FF05H, CF=0, CL=3, what is the difference between the results of the following‬‬
‫‪instructions:‬‬

‫‪RCR AX, CL‬‬

‫‪ROR AX, CL‬‬

‫‪Solution‬‬

‫]‪[1‬‬ ‫‪RCR AX, CL‬‬

‫‪AXold = 1111 1111 0000 0101‬‬ ‫‪0‬‬

‫‪AXnew = 0101 1111 1110 0000‬‬ ‫‪1‬‬

‫‪⸫ AXnew = 5FE0 H , CF = 1‬‬

‫‪[2] ROR AX, CL‬‬

‫‪AXold = 1111 1111 0000 0101‬‬

‫‪AXnew = 1011 1111 1110 0000‬‬

‫‪⸫ AXnew = BFE0 H , CF = 1‬‬


Examples

Notice that example tables in this lecture are taken from Reference text book 1.

Best Regards
Dr. Zainab Alomari
Lecture 12: Subroutine Instructions
1- Call Instruction
‫ ػَيٍح‬.ٍْٔ ‫ ٌقً٘ تاىقفض اىى تشّاٍح فشػً ٌتٌ تْفٍزٓ ثٌ اىؼ٘دج المَاه اىثشّاٍح ٍِ تؼذ اىَناُ اىزي تٌ اىقفض‬Call ‫اٌؼاص‬
.IPٗ CS ٍِ ‫ فقظ اٗ تغٍٍش مو‬IP ‫اىقفض تؼًْ تغٍٍش اىَؤشش اىخاص تاىثشّاٍح ٕٗ٘ إٍا‬

Call Instructions are divided into two types: Intersegment and Intrasegment Calls.

1) Intrasegment Call
In this type of Call, only IP is changed within the same CS.

CALL operand

Operand can be: immediate value


16-bit register
memory
Examples:
CALL 1234H ; IPnew= 1234H

CALL BX ; IPnew= BX

CALL [BX] ; IPnew= 8A60H (the memory is given in this example)

77H DS: BX+2


IPhi
8AH DS: BX+1
IPlow
60H DS: BX

In this type of Call, the following happens:

(IP) of the instruction that follows call is pushed into the stack, with decrementing SP by 2.

‫ فً اىستاك تْفس طشٌقح ػَو اٌؼاص‬Call ‫ ىالٌؼاص اىزي ٌقغ تؼذ اٌؼاص‬IP ‫ تخضُ قٍَح‬Call‫ٌقً٘ ٕزا اىْ٘ع ٍِ اىـ‬ -
ٌ‫ ث‬,)ٓ‫ اىى اىقٍَح اىدذٌذج (ٗاىتً تؼ٘د الٗه اٌؼاص فً اىثشّاٍح اىفشػً اىَطي٘ب تْفٍز‬IP‫ ٗرىل قثو اُ ٌغٍش اىـ‬push
.2 ‫ تَقذاس‬SP ‫ٌقً٘ تتْقٍص قٍَح‬
ً‫ ٕزٓ فً اىستاك ٕ٘ الخو اىؼ٘دج اىٍٖا تؼذ االّتٖاء ٍِ تْفٍز اىثشّاٍح اىفشػ‬IP‫سثة االحتفاظ تقٍَح اىـ‬ -
.)subroutine(

Example
What is the result of executing the following instruction?

CALL [1234H]

Solution

IPHi  SS:[SP-1]

IPLow  SS:[SP-2]

SP= SP-2

(IPold is the IP of the instruction that follows CALL instruction in the main program)
2) Intersegment Call
ٌ‫ ٌٗت‬.‫ فقظ‬IP ‫ ٗىٍس اىـ‬CALL ‫ فً اٌؼاص‬IP ٗ CS ٍِ ‫ تحٍث ٌتٌ تغٍٍش قٍَح مو‬segment‫فً ٕزا اىْ٘ع ٌتٌ اىقفض خاسج اىـ‬
:‫ مَا فً االٍثيح اىتاىٍح‬,‫ إٍا ٍثاششج اٗ تؤخز قٍََٖا ٍِ اىزامشج‬IPٗ CS ٌٍ‫اػطاء ق‬

Examples
CALL 1000H:2000H

CALL DWORD PTR [ DI ]

CALL FAR PTR [SI+5]

In this type of Call, the following happens:

Both of CS and IP of the instruction that follows call are pushed into the stack, with
decrementing SP by 4, and then CS and IP are loaded with the new values.

.‫ فً اىستاك‬IPLow ٌ‫ ث‬IPHi ٌ‫ ث‬CSLow ٌ‫ ث‬CSHi ‫ ٌتٌ ٗضغ‬CALL‫ فً ٕزا اىْ٘ع ٍِ اىـ‬:‫ٍالحظح‬

CSHi  SS: [SP-1]


CSLow  SS: [SP-2]
IPHi  SS: [SP-3]
IPLow  SS: [SP-4]
SP= SP-4

Example
Give the new values of all the given registers with drawing all the memory locations (addresses
and values) that are affected or used by the following instruction (memory is given with the
question):
Data Segment
CALL FAR PTR [BX]
CShi
26H 8088DH
Let BX= 88AH CSLow
SP= 100H CCH 8088CH
DS= 8000H IPhi
12H 8088BH
CS= 2000H IPlow
IP= 30H ABH 8088AH
SS= B00H
22H 80889H

80888H
Stack Segment
Solution

80000 B00H:100H

88A + 20H B00H: FFH


00H B00H: FEH
8088AH
00H B00H: FDH
⸫ From the given memory:
30H B00H: FCH
CSnew = 26CCH

IPnew = 12ABH

SPnew = SPold – 4 = 00FCH

BXnew = BXold

DSnew = DSold

SSnew = SSold

Example
Give the new values of all the given registers with drawing all the memory locations (addresses
and values) that are affected or used by the following instruction:

CALL FAR PTR [SI]

Let BX= 1000H


SI= 200H
DI= F100H
SP= 7E90H
DS= 5A00H
CS= 6400H
IP= 5A0H
SS= 19F0H

You are given that (IPnew = 6F7BH, CSnew = A590H).


Solution

Data Segment Stack Segment

A5H 5A00H:203H 19F0H:7E90H


90H 5A00H:202H 64H 19F0H:7E8FH
6FH 5A00H:201H 00H 19F0H:7E8EH
7BH 5A00H:200H 05H 19F0H:7E8DH
A0H 19F0H:7E8CH

SPnew = 7E90H – 4 = 7E8CH

SInew = SIold

DInew = DIold

DSnew = DSold

BXnew = BXold

SSnew = SSold

2- Return Instruction
Every subroutine must end by executing (RET) instruction to return control to the main program.

‫ ٗرىل‬CALL ‫ ٗتاىضثظ اىى االٌؼاص اىزي ٌيً اٌؼاص‬,ٍْٔ ‫ٌقً٘ ٕزا االٌؼاص تاسخاع اىَؼاىح اىى اىثشّاٍح اىشئٍسً اىزي تٌ اىقفض‬
.POP ‫ ٍِ اىستاك تْفس ٍثذأ ػَو اٌؼاص‬IP ٗ CS ٍِ ‫ أٗ مو‬IP ‫ٍِ خاله استشخاع قٍَح‬

After executing RET instruction the following happen:

A new value for IP is fetched from the stack (if the CALL was intrasegment) with increasing
SP by 2, or a new value for CS and IP are fetched from the stack (if the CALL was intersegment)
with increasing SP by 4.

1) RET in Intrasegment CALL Stack Segment

IPLow  SS: [SP]


SS: SP+2
IPHi  SS: [SP+1] IPHi SS: SP+1

SPnew = SP + 2 IPLow
SS: SP
2) RET in Intersegment CALL
Stack Segment
IPLow  SS:[SP] SS: SP+4

IPHi  SS:[SP+1] CSHi SS: SP+3

CSLow SS: SP+2


CSLow  SS:[SP+2]
IPHi SS: SP+1
CSHi  SS:[SP+3]
IPLow SS: SP

SPnew = SP + 4

Best Regards
Dr. Zainab Alomari
Lecture 13: Delay Loops
Each instruction in 8086Mp takes a specific number of clock cycles for execution.

The time required to execute any instruction = the no. of required clock cycles * clock time (T)

where T is (1/f), where f is the Microprocessor frequency. For 8086Mp, f=5MHz or 10MHz.

ً‫ التً ٌحتاجٍا االٌعاز مضرَتا ً ف‬clock cycles ‫الُقت الري ٌستغرقً اي اٌعاز لٍتم تىفٍري مه قثل المعالج ٌُ عدد الـ‬
.10MHz َ‫ أ‬5MHz ‫ ٌكُن التردد إما‬0808 ‫ َفً معالج‬.‫) ٌُ مقلُب التردد‬T( ‫ الُاحدج ٌَرا السمه‬clock ‫زمه الـ‬

ً‫ التً ٌحتاجٍا كل اٌعاز (ٌَُ ٌعطى للطالة) َكرلك تردد المعالج فٍمكه ان وعلم السمه الكل‬clock cycles‫فاذا علمىا عدد الـ‬
.‫الري ٌستغرقً تروامج معٍه لٍتم تىفٍري‬

Examples

XOR  3T
Push  11T
POP  8T
MOV  4T
JNZ  16/4T

CALL  19T

RET  16T

Loop  17/5T

(where T is the clock time)

Example

Calculate the delay time taken by the following instructions:

MOV CX, 3
N1: LOOP N1

(if: MOV  4T, Loop  17/5T)

Solution

MOV instruction: 1 time

LOOP instruction: 3 times (2 times: jump is done, 1 time: jump is not done)
⸫ Delay = 4T + (3-1)*17T + 5T

= (4 + 34 + 5)T = 43T

Let frequency = 5MHz

⸫ Delay = 43*1/(5*106) = 8.6 µsec

NOW if the code is changed to:

N1: MOV CX, 3


LOOP N1

Delay time = ∞

Example

Write a subroutine that generates a delay of 200msec if 8086Mp frequency is 5MHz.

Solution
XOR  3T
CS:300H PUSH CX
Push  11T
CS:IP CALL 300H MOV CX, N
POP  8T
RPT: LOOP RPT
POP CX MOV  4T
RET JNZ  16/4T

CALL  19T
Delay time = 19T RET  16T
+ 11T Loop  17/5T
+ 4T

+ (N-1)*17T + 5T

+ 8T

+ 16T

200msec = 58T + 5T – 17 T + 17NT

200msec/T = 46 + 17N

N=58821 d  N = E5C5 H
Example

For the code in the previous example, what is the value of N that maximizes the delay? And what
is this maximum delay?

Solution

Nmax= FFFF H

delay / T = 46 + 17 Nmax

⸫ delay = 223msec

Notice that this subroutine is not enough when the required delay time is greater than 223msec.

Example

Write a subroutine that generates a delay of 500msec:

Solution
XOR  3T
CS:500H PUSH CX
Push  11T
CS:IP CALL 500H MOV CX, N
POP  8T
RPT: NOP
PUSH AX MOV  4T
POP AX JNZ  16/4T
LOOP RPT
CALL  19T
POP CX
RET  16T
RET
Loop  17/5T
Delay time = 19T
NOP  3T
+ 11T

+ 4T

+ N (3T + 11T + 8T)

+ (N-1)*17T + 5T

+ 8T

+ 16T
500msec / T =63 – 17 +17N + 22N

N=64101 d  N =FA65 H

Example

Write a subroutine that generates a delay of 10sec if the 8086Mp frequency is 5MHz:

Solution
XOR  3T
CS:A00H PUSH CX
Push  11T
PUSH AX
POP  8T
CS:IP CALL A00H MOV AX, N
LP2: MOV CX, 0FFFFH MOV  4T
LP1: NOP JNZ  16/4T
PUSH AX
CALL  19T
PUSH AX
RET  16T
POP AX
POP AX Loop  17/5T
LOOP LP1 NOP  3T
DEC AX
DEC  2T
JNZ LP2
POP AX
POP CX
RET

Delay time = 19T


+ 11T
+ 11T
+ 4T
+ N [ 4T + 65535 * (3T + 11T + 11T + 8T + 8T) + 65534 * (17T) + 5T + 2T ]
+ (N-1)*16T + 4T
+ 8T
+ 8T
+ 16T
10 sec = 65T + 3801040 NT

At f= 5MHz:

N=13.15 ≈ 13 d  N = 0C H

In order to perform a delay of 40seconds using the same code, only the value of N is changed.

At delay = 40sec

40 sec = = 65T + 3801040 NT

N= 52.6 ≈ 53 d  N = 35 H

Example

Find the delay time of the following subroutine:

CS:600H PUSH CX
PUSH AX
CS:IP CALL 600H MOV CX, 800H
NXT: MOV AX, 01H
PUSH AX
POP CX
LOOP NXT
POP AX
POP CX
RET

Solution

All the instructions of this subroutine will be executed only one time.

Delay time = 19T + 11T + 11T + 4T +4T + 11T + 8T + 5T + 8T + 8T + 16T

=105T = 105/(5*106) = 21 µsec

Example

What happens to the delay time of the previous subroutine if instruction 4 is changed to:

MOV AX, 2
Solution

Delay time = ∞

Example

What happens to the delay time of the previous subroutine if instruction 5 is changed to:

PUSH CX

Solution

The instruction (Loop NXT) and the 3 instructions before it will be executed 800H times.

Delay time = 16.4 µsec

Example

Find the delay time of the following subroutine:

CS:1ABCH PUSH CX
MOV CX, 400H
CS:IP CALL 1ABCH NXT: PUSH AX
POP AX
LOOP NXT
POP CX
RET

Solution

Delay time = 7.382 msec

*_*_*_*_*_*_*_*

Best Regards
Dr. Zainab Alomari
Lecture 1: In/Out Instructions
To deal with input/output devices, 8086Mp programmer needs to know three things:

1- Type of device: it is important to know if the device is an input or output device, in


order to use the suitable instruction. Some devices are (input/output) devices.
2- Port number: This is a number given to the I/O device during manufacturing. Each I/O
device has its own unique port number. Port numbers can be 8-bits or 16-bits number.
3- Data Length: An I/O device will send/receive a piece of information to/from 8086Mp.
This information is of 8-bits or 16-bits length (depending on the type of the device).

Note1: These three parameters are fixed for each device and can’t be modified.

Note2: the length of the port number and data of any device are not related together, there can be
a device with a data length of 16-bits and a port number of 8-bits length, or a device with 8-bits
data length and 16-bits port number.

Note3: some devices can be input and output in the same time, i.e. you can read data from such a
device and also you can send data to it using its port number.

ْٕ ‫ ْم انجٓاص ْٕ جٓاص ادخال او اخشاج أو كالًْا؟ ٔيا‬:‫عُذ انخعايم يع اجٓضة االدخال ٔاالخشاج يجب يعشفت ثالد ايٕس‬
ٌ‫بج؟ ْزِ انًعهٕياث يجب ا‬61 ‫بج أو‬8 ْٕ ‫ نٓزا انجٓاص‬data length‫) انخاص بّ؟ ْٔم انـ‬Port number( ‫سقى انبٕسث‬
.‫حعطى في انسؤال‬
In instruction:
This instruction transfers data from external I/O device to AL or AX.

In AL, port no. ; used when the data length is 8-bits

In AX, port no. ; used when the data length is 16-bits

Out Instruction:
This instruction transfers data from AL or AX to external I/O device.

Out port no. , AL ; used when the data length is 8-bits

Out port no. , AX ; used when the data length is 16-bits

Note: only AL or AX is used by the 8086Mp to send/receive data to/from I/O devices:

AL if the data length of the device is 8-bits.


AX if the data length of the device is 16-bits.
Fixed and Variable Port Addressing
If the device has an 8-bits port number, then it is used directly in the In/Out Instruction, as in
the above examples. This is called (Fixed Port Addressing).

‫ كًا في االيثهت‬outٔ in ‫بج فقط فيخى اسخخذاو ْزا انشقى يباششة ً في ايعاص‬8 ٍ‫ارا كاٌ سقى انبٕسث انخاص بانجٓاص يخكٌٕ ي‬
:‫انخانيت‬

Examples

In AL, 96H ; an 8-bit data is copied from an input device that has a port no.= 96H to AL.

In AX, 7AH ; a 16-bit data is copied from an input device that has a port no.= 7AH to AX.

Out 3FH, AL ; an 8-bit data is sent from AL to an output device that has a port no.= 3FH.

Out 19H, AX ; a 16-bit data is sent from AX to an output device that has a port no.= 19H.

While if the port number of a device is of 16-bits length, it must be given to DX, and then DX is
used in the In/Out Instruction. This is called (Variable Port Addressing).

‫ بم يجب ٔضعّ في‬outٔ in ‫بج فال يجٕص اٌ يسخخذو يباششة في ايعاص‬61 ٍ‫أيا ارا كاٌ سقى انبٕسث انخاص بانجٓاص يخكٌٕ ي‬
:‫ كًا في االيثهت انخانيت‬,‫ عهى آَا سقى انبٕسث‬DX ‫ ٔبعذْا حسخخذو‬MOV ‫ باسخخذاو ايعاص‬DX

Examples

Mov DX, 3FA0H


In AL, DX

Mov DX, 99A3H


In AX, DX

Mov DX, 51BCH


Out DX,AL

Mov DX, 972H


Out DX,AX
Example

(THE INTEL MICROPROCESSORS) \Example 4-12 (Page 140)


Write the required instructions to set the right most two bits of the speaker (port no.= 61H),
then clear them after a specific delay time. (The speaker has an 8-bit register).

Solution

IN AL, 61H
OR AL, 3
OUT 61H, AL

MOV CX, 1000H


L1: NOP
LOOP L1

IN AL, 61H
AND AL, 0FCH
OUT 61H, AL
HLT

NOTE: You can also find some help on this topic from the tutorials available in the Emulator
program.

Best Regards
Dr. Zainab Alomari
‫‪Lecture 2: Interrupts‬‬ ‫)‪(Part 1‬‬

‫‪Interrupts are special type of (CALL) instruction. If any interrupt occurs while the Mp is‬‬
‫‪executing a program, it breaks the execution and starts executing a subroutine called Interrupt‬‬
‫‪Service Routine (ISR). After executing the ISR, the Mp returns to the main program and‬‬
‫‪continues from the point it stopped at.‬‬

‫ًٌرهك انًعانح ‪ 0808‬انقذس ج عهى انرعايم يع األخٓضج انخاسخٍح (أخٓضج االدخال ٔاخٓضج االخشاج) يثم انطاتعح‪ ,‬يصاتٍح‬
‫اإلضائح‪ ,LED ,‬انشاشح‪ ,‬يقٍاط انضغظ ‪ ,‬يقٍاط انحشاسج‪ ,‬انًشٔحح ‪ ...‬انخ‪.‬‬

‫ٌٔرطهة انرعايم يع ْزِ االخٓضج ٔخٕد ذقٍُح انًقاطعح‪ ًْٔ ,‬قذسج اي خٓاص أٌ ٌقٕو تًخاطثح انًعانح نطهة خذيح يعٍُح‪,‬‬
‫ٌٔقٕو انًعانح تاالعرداتح نٓزِ انًقاطعح حرى فً حال كَّٕ ٌقٕو ترُفٍز تشَايح يعٍٍ حٍث ٌرٕقف عٍ انرُفٍز ٌٔغردٍة نهدٓاص‬
‫صاحة انطهة‪ٌٔ ,‬قٕو ترُفٍز تشَايح انخذيح انخاص تٓزا اندٓاص ٔانزي ٌغًى (‪ٔ )ISR‬تعذ رنك ٌعٕد انى انثشَايح ٌٔكًم يٍ‬
‫انُقطح انرً ذٕقف عُذْا‪ْٔ .‬زِ انطشٌقح (خذيح اندٓاص ثى انعٕدج انى انثشَايح انشئٍغً) شثٍٓح تًثذأ عًم اٌعاص ‪.CALL‬‬

‫يإْ انـ(‪)ISR‬؟ ْٕ عثاسج عٍ تشَايح فشعً يكرٕب يغثقا ً ٔيخضٌٔ فً راكشج انًعانح‪ٌ ,‬حرٕي عهى اٌعاصاخ يعٍُح ٌرى ذُفٍزْا‬
‫يٍ قثم انًعانح عُذ االعرداتح نطهة يقاطعح يعٍٍ‪ .‬نكم يقاطعح يٍ انًقاطعاخ ‪ ISR‬خاص تٓا‪.‬‬

‫‪There are 256 interrupts in the 8086Mp:‬‬

‫‪From Interrupt 0  to Interrupt 255 (or: from Interrupt 0H  to Interrupt FFH).‬‬

‫‪These interrupts are divided into two types:‬‬

‫‪1- Interrupts that are reserved for the present and future products and system errors.‬‬
‫‪They are: (from Interrupt 0H  Interrupt 1FH), i.e. the first 32 interrupts.‬‬
‫‪2- Interrupts available for user. These are the interrupts from Interrupt 20H  Interrupt‬‬
‫‪FFH.‬‬

‫?‪Q1) How many interrupts are available for user‬‬

‫انًقاطعاخ انًرٕفشج فً يعانح ‪ 8086‬عهى َٕعٍٍ‪:‬‬

‫ال انًقاطعح االٔنى‬‫‪ -1‬يقاطعاخ يحدٕصج (ًْٔ أٔل ‪ 23‬يقاطعح) ٌٔقٕو انًعانح ترُفٍزْا عُذ انحاخح انٍٓا‪ ,‬يث ً‬
‫(‪ٌ )Interrupt 0‬رى ذُفٍز انـ‪ ISR‬انخاص تٓا يٍ قثم انًعانح فً حانح حصٕل قغًح عهى ‪ ,8‬حٍث ذقٕو االٌعاصاخ‬
‫انًٕخٕدج فً ْزا انـ‪ ISR‬تاٌقاف ذُفٍز انثشَايح انحانى ٔانخشٔج يُّ ٔطثاعح ‪ error‬عهى انشاشح‪ .‬تعض يٍ ْزِ‬
‫انًقاطعاخ يحدٕص ٔنكُّ غٍش يغرخذو حٍث كاٌ يٍ انًرٕقع اضافح ذطٌٕشاخ عهى انًعانح فرى حدضْا نخذيح ْزِ‬
‫انرطٌٕشاخ يغرقثالً‪.‬‬
‫‪ -3‬يقاطعاخ يرٕفشج نهًغرخذو‪ًٌ :‬كٍ نهًغرخذو أٌ ٌ عشف يقاطعح يعٍُح ٌٔعطٍٓا سقى يٍ ‪ 20H‬انى ‪ٌٔ FFH‬قٕو تخضٌ‬
‫تشَايح خذيح ْزِ انًقاطعح (‪ ) ISR‬فً انزاكشج‪ٌٔ ,‬قٕو تاعرذعاء ْزِ انًقاطعح يرى شاء خالل تشَايدّ تاعرخذاو اٌعاص‬
‫(‪ٌٔ )INT‬كرة أيايّ انشقى انخاص تانًقاطعح‪ٔ ,‬حٍٍ ٌصم انًعانح خالل ذُفٍز انثشَايح انى اٌعاص انًقاطعح ْزا ٌقٕو‬
‫تانقفض انى انـ‪ ISR‬انًطهٕب ٔذُفٍزِ ثى انعٕدج إلكًال انثشَايح انشئٍظ‪.‬‬
‫(عٍأذٍُا الحقا ً كٍف ٌخراس انًغرخذو سقى انًقاطعح ٔكٍف ٌعهى انًعانح يٕقع خضٌ انـ‪)ISR‬‬
Interrupt Vectors:
Each interrupt has an interrupt vector, which is a 4-byte vector containing the address (CS &
IP) of the ISR. The first 2-bytes contain the IP and the second 2-bytes contain the CS.

 The interrupt vector of Interrupt 0 is stored in the first 4-bytes of the memory
(addresses: 00000H  00003H).
 The interrupt vector of Interrupt 1 is stored in the second 4-bytes of the memory
(addresses: 00004H  00007H).
 The interrupt vector of Interrupt 2 is stored in the third 4-bytes of the memory
(addresses: 00008H  0000BH).

and so on.

‫ ألي‬ISR‫ فثانرانً يًكٍ ذحذٌذ عُٕاٌ انـ‬,00000H ‫ يخضَٔح تشكم يرغهغم فً انزاكشج اترذا ًء يٍ انًٕقع‬ISR‫فثًا اٌ عُأٌٍ انـ‬
:‫ يثال‬, 4 ً‫يقاطعح يٍ خالل ضشب سقى انًقاطعح ف‬

 The interrupt vector of Interrupt 7 is stored in the seventh 4-bytes of the memory.
7 * 4 = 28d = 1CH
⸫ The interrupt vector is stored at addresses: (0001CH  0001FH)

The Mp multiplies the interrupt number of the interrupt by 4 to find the address where the
interrupt vector (CS and IP) is stored.

Thus for 256 interrupts, there are 256 interrupt vectors.

Interrupt Vector Table (IVT):


IVT is the part of the memory where the interrupt vectors of all the interrupts are stored. IVT
is located at the beginning of the 8086Mp memory.

Q2) What is the size of IVT?


Q3) What are the first and last addresses of the IVT?

ٌ‫ ٔرنك ال‬4 ‫) نكم انًقاطعاخ ٌقثم انقغًح عهى‬Interrupt vector‫ (أي انـ‬CS&IP‫َالحظ تأٌ انعُٕاٌ انزي ٌخضٌ فٍّ انـ‬
:ً‫ فثانران‬,‫ نكم يقاطعح ٌحراج انى استع يٕاقع‬ISR ‫عُٕاٌ انـ‬

‫ انخاص تانًقاطعح يٍ سقًٓا فُقٕو تضشب‬ISR‫ ارا كاٌ انًطهٕب اٌداد عُٕاٌ يٕاقع انزاكشج انًخضٌٔ فٍٓا يٕقع انـ‬-1
ً‫) كًا ف‬4 ً‫ ٔرنك تاضافح صفشٌٍ أياو انشقى تعذ ذحٌٕهّ انى انُظ او انثُائً (يًا ٌعادل ضشب ف‬4 ً‫سقى انًقاطعح ف‬
.‫انًثال انغاتق‬
‫ ٔرنك تحزف‬4 ‫ انخاص تٓا فُقٕو ترقغٍى انعُٕاٌ عهى‬IVT‫ ارا كاٌ انًطهٕب اٌداد سقى انًقاطعح يٍ يعشفح عُٕاٌ انـ‬-3
.)4 ‫ترٍٍ يٍ ًٌٍٍ انشقى تعذ ذحٌٕهّ انى انُظاو انثُائً (يًا ٌعادل انقغًح عهى‬
Example

If an interrupt vector is stored in the IVT starting at address: (001A8H), find the number of the
interrupt. Also give the addresses of the IVT where CS and IP of this interrupt are stored.

Sol: 001A8H

0000 0000 0001 1010 1000 :4 ‫َحزف صفشٌٍ يٍ خٓح انًٍٍٍ نهقغًح عهى‬

⸫ The interrupt number is 6AH.

The four addresses of the IVT where CS and IP are stored are:
CShi 001ABH
001A8H, 001A9H, 001AAH, 001ABH.
CSlow 001AAH
IPlow is stored at: 001A8H and IPHi is stored at: 001A9H
IPhi 001A9H
CSlow is stored at: 001AAH and CSHi is stored at: 001ABH IPlow 001A8H

Interrupt Priorities:
Interrupts are served on a priority basis. Interrupt 0 has the highest priority, while interrupt
255 has the lowest priority.
For example: Interrupt 60H has lower priority than interrupt 5AH.
‫ فٍقٕو انًعانح تخذيح‬,‫ٌرى انعًم يع انًقاطعاخ تُظاو األعثقٍح عُذيا ٌكٌٕ ُْانك اكثش يٍ طهة يقاطعح فً انٕقد انٕاحذ‬
.‫ ٔتعذ االَرٓاء يٍ خذيرٓا ٌُرقم نخذيح انًقاطعح راخ االعثقٍح االقم‬,‫انًقاطعح راخ االعثقٍح ا ألعهى ًْٔ انرً سقى يقاطعرٓا أقم‬

Q4) Sort the following interrupts according to their priorities, from the highest priority to the
lowest:
Interrupt ABH, Interrupt A0H, Interrupt BAH and Interrupt BCH.
Answers:
Q1_Ans: There are 224 interrupts available for user.

Q2_Ans: 256 * 4 = 1024 Byte = 1Kbyte.

Q3_Ans: The first address is 00000H and the last address is 003FFH.

Q4_Ans: 1- Interrupt A0H


2- Interrupt ABH
3- Interrupt BAH
4- Interrupt BCH

Best Regards
Dr. Zainab Alomari
Lecture 3: Interrupts (Part 2)

There are three types of Interrupts:

1- Software Interrupts:
This interrupt uses the instruction (INT n), where n is the interrupt number and it can be any
number between (0HFFH).

.0HFFH ٍِ ٌ‫ أي سق‬n ‫) ٗحنُ٘ قٍَت‬INT n( ‫ٕزا اىْ٘ع ٍِ اىَقاطعاث باٍناُ اىَسخخذً اسخذعائٔ باسخخذاً االٌعاص‬

When (INT) instruction is executed, 8086Mp performs the following 5 steps:

1) The Flags register (2bytes) is pushed onto the stack.


2) TF and IF are cleared (TF=0 and IF=0).
3) CS is pushed onto the stack.
4) IP is pushed onto the stack.
(SPnew= SPold-6)
5) New values for CS and IP are fetched from the IVT.

ً٘‫ ٌق‬,)ً٘‫ سقٌ اىَقاطعت (مَا فً اىَثاه اىَشس‬n ‫) زٍث حَثو‬INT n( ‫عْذٍا ٌقً٘ اىَعاىح بخْفٍز بشّاٍح ٍعٍِ ٌٗصو اىى اٌعاص‬
ُ‫ ٗعَيٍت اىقفض ٕزٓ حخطيب اى٘ص٘ه اىى اىَنا‬,‫ اىزي ٌسخ٘ي اٌعاصاث اىخذٍت اىخً حقذٍٖا ٕزٓ اىَقاطعت‬ISR‫اىَعاىح باىقفض اىى اىـ‬
.‫ زسب سقٌ اىَقاطعت مَا ششذ فً اىَساضشة اىسابقت‬IVT‫ ٗاىخً ٌخٌ اىسص٘ه عيٍٖا ٍِ خذٗه اىـ‬ISR‫اىَخضُٗ فٍٔ ٕزا اىـ‬
CS ٍِ ‫ فاُ اىَعاىح بساخت اىى االزخفاظ باىقٌٍ اىقذٌَت ىنو‬, ISR‫ٗىغشض اىع٘دة اىى اىبشّاٍح اىشئٍسً بعذ االّخٖاء ٍِ حْفٍز اىـ‬
CS,IP & ‫ ىنو ٍِ اىـ‬Push ‫ ٌخٌ عَو‬. Flags ً‫ مَا ٌقً٘ اٌضا ً باالزخفاظ بساىت األعال‬,)Call ‫& (مَا ٌسصو فً اٌعاص‬IP
.6 ‫) بَقذاس‬SP( ‫ ٍَا ٌؤدي اىى إّقاص قٍَت اىَؤشش اىخاص باىسخاك‬,‫ اىى اىسخاك‬Flags register

main program: CSn:IPn ___________


___________ 5 steps
___________
___________ ___________ ISRn
___________
.
INT n ; (n=0FFH) .
___________
.
___________
IRET
___________

3 steps
Each ISR ends with (IRET) instruction. When IRET instruction is executed, the Mp performs
the following 3 steps:

1) IP takes its value back from the stack.


2) CS takes its value back from the stack.
3) Flags register takes its value back from the stack.
(SPnew= SPold+6)

ٌ‫ ٍِ اىسخاك ٗاىخً ماُ قذ ح‬CS, IP & Flags register ٍِ ‫) ٗاىزي ٌقً٘ باسخعادة قٌٍ مو‬IRET( ‫ باٌعاص‬ISR ‫ٌْخًٖ مو‬ 
.ٓ‫ ٗرىل ىغشض اىع٘د ة المَاه حْفٍزس اىبشّاٍح اىشئٍسً ٍِ اىَناُ اىزي ح٘قفْا عْذ‬, )INT n( ‫خضّٖا عْذ حْفٍز اٌعاص‬
‫ زٍث أُ اىفشق ٕ٘ اّْا فً اٌعاص اىَقاطعت ّقً٘ بخسذٌذ سقٌ اىَقاطعت‬,) Call(ٗ )INT( ‫ّالزع ْٕا اىخشابٔ اىنبٍش بٍِ اٌعاصي‬ 
.‫ ٍباششة فً االٌعاص‬CS&IP ‫ فٍخٌ اعطاء قٍَت‬Call ‫ أٍا فً اٌعاص‬IVT‫ ٍِ اىـ‬CS&IP ‫ٌٗخٌ أخز اىـ‬

Q) What are the benefits of INT instruction compared to Call instruction?


Answer:
1- No need to remember the address of the system call.
2- Each time INT instruction is used instead of Call instruction, 3 bytes are saved (because INT
is 2 bytes long, while Call is 5 bytes long).

‫ ؟‬Call ‫ بذالً ٍِ اٌعاص‬INT ً‫ٍا اىفائذة ٍِ اسخخذا‬

:‫االخابت‬

.‫ باٌج ٍَا ٌ٘فش ٍ٘اقع ٍِ اىزامشة‬5 ٕ٘ Call ‫ٕ٘ باٌخٍِ فقظ فً زٍِ أُ ط٘ه اٌعاص‬INT ‫ إُ ط٘ه االٌعاص‬:‫ال‬ ً ٗ‫ا‬
.‫ بو ّسخاج اىى حزمش سقٌ اىَقاطعت فقظ‬,ً‫ ىسْا بساخت اىى حزمش قٍَت اىـعْ٘اُ اىخاص باىبشّاٍح اىفشع‬:ً‫ٗثاٍّا‬

2- Hardware Interrupts:
There are three pins used for hardware interrupts in 8086Mp: (NMI, INTR and INTA).

NMI
INTR
INTA

8086Mp
Steps for hardware interrupt:
1) The I/O device requests a service by activating INTR pin (INTR=1).
2) The Mp accepts the request by pulsing INTA pin to 0 logic. If the Mp was busy, he
finishes his task and then pulses INTA to 0 logic.
3) The I/O device puts the number of the interrupt on the first 8-bits of the data bus
(D0D7).
4) The Mp pushes the Flags register on to the stack.
5) TF and IF are cleared.
6) CS and then IP are pushed onto the stack.
7) New CS and IP values are fetched from the IVT.

Interrupt Flag (IF):

Hardware interrupts are disabled when interrupt flag (IF) is 0. When interrupt flag is set to 1,
it enables hardware interrupts to be received. By default hardware interrupts are enabled (IF=1).
Hardware interrupts are disabled (IF=0) automatically when software or hardware interrupt is in
the middle of the execution.

There are two instructions that can be used to clear or set IF:

CLI ; Clear Interrupt Flag (IF=0). This disables INTR pin (no hardware interrupt is received).

STI ; Set Interrupt Flag (IF=1). This enables INTR pin (enables receiving hardware interrupts).

Non-Maskable Interrupt (NMI):

NMI is a special type of hardware interrupt that the system can’t ignore. NMI is used to report
important issues to the Mp such as errors or power failures. When this interrupt occurs, the Mp
stores all the internal registers in a battery-backed up memory or an EEPROM. This interrupt is a
system error and it is given a high priority (INT 2).

ً‫ ) فٖزا ٌعًْ ٗخ٘د ٍشنيت حخطيب قٍاً ٍقاطعت اىَعاىح عِ اىعَيٍاث اىخً ٌقً٘ بٖا ٗاىقٍا‬1 logic( ‫عْذٍا ٌصبر ٕزا االدخاه‬
‫ اىخاصت‬2 ٌ‫ ٕٗزا ٌخٌ ٍِ خاله حْفٍزٓ ىيَقاطعت سق‬,‫بَدَ٘عت ٍِ االٍ٘س إَٖٔا خضُ قٌٍ خٍَع اىسدالث فً رامشة خاصت‬
.NMI‫باىـ‬

3- Internal Interrupts:
These interrupts are executed without the need to use INT instruction, due to a specific
system event or error, like: divide by zero (which resumes INT 0). These interrupts are: INT
0, INT 1, INT 3 and INT 4.

Table 1 describes all the interrupts available in 8086Mp.


Table 1: Description of the function of each of the 8086Mp interrupts

Interrupt Description
INT 0 Division by zero
INT 1 Single step
INT 2 NMI
INT 3 Break point
INT 4 Overflow
INT 5  INT 31
Not used
(or: INT 5  INT 1FH)
INT 32  INT 255
User defined
(or: INT 20H  INT FFH)

Best Regards
Dr. Zainab Alomari
Lecture 4: Directives
Directives are included in the source code that contains assembly language code. Directives
are used to give directions to the compiler (compilers convert the com file in the Emulator to
machine code). Unlike assembly language instructions, directives have no machine code.

Examples

 (Org 100h) is a directive used to tell the compiler to load the program at offset 100H in the
code segment.
 (END) is a directive to stop the compiler.

Note: in the com file, directives are given in violet color while instructions are written in blue.

Mp‫غد يٕجٓح انٗ انـ‬ٛ‫ ٔن‬,Assembly‫ يغ انثشَايج انًكرٕب تهغح انـ‬compiler‫ٓاخ ذؼطٗ نهـ‬ٛ‫ ػثاسج ػٍ ذٕج‬ْٙ
ٌ‫رى خض‬ٛ‫حذد انًٕقغ انز٘ ع‬ٚ ٘‫) انز‬Org 100h( ّٛ‫ يثانٓا ذٕج‬.machine code‫هٓا انٗ انـ‬ٕٚ‫رى ذح‬ٚ ‫ ال‬ٙ‫ فثانران‬,‫ؼاصاخ‬ٚ‫كاال‬
ٌٕ‫ٓاخ ذؼطٗ انه‬ٛ‫ َالحظ أٌ انرٕج‬.compiler‫قاف انـ‬ٚ‫قٕو تا‬ٚ ٘‫) انز‬END( ّٛ‫ ٔذٕج‬,code segment‫ّ يٍ انـ‬ٛ‫انثشَايج ف‬
.‫ انهٌٕ االصسق‬Assembly‫ؼاصاخ نغح انـ‬ٚ‫ٍ ذؼطٗ ا‬ٛ‫ ح‬ٙ‫ ف‬ٙ‫انثُفغج‬

.‫ٓاخ‬ٛ‫ؼاصاخ ٔذٕج‬ٚ‫ يٍ ا‬com file‫ انـ‬ٙ‫م يا يكرٕب ف‬ٕٚ‫قٕو ترشجًح ٔذح‬ٚ ٘‫ ْٕ انثشَايج انز‬compiler‫ انـ‬:‫مالحظة‬

Variables and Arrays


A) Variables:

A variable needs to have name, length (byte or word) and value:

Name DB Value
DW

Note1: Variables are defined at the end of the source code (in com files) after RET instruction.

Note2: DB means define byte, DW means define word.

Examples

1) To define a variable word named k1, which has the value (5F0H), we write:

k1 DW 5F0H

2) To define a variable byte named NUM, which has the value (70d), we write:

NUM DB 70

3) To define a variable word named Var1, which has the value (6AH), we write:

Var1 DW 6AH
Note: It is also possible to give the value of the variable in binary as:

Var1 DW 11010b )‫غاس ذغأ٘ صفش‬ٛ‫ انثراخ يٍ جٓح ان‬ٙ‫ ْزِ انحانح اٌ تاق‬ٙ‫(َالحظ ف‬

4) To define a variable byte named K2, which has no initial value, we write:
K2 DB ?

NOTE: Defining a variable or array is a directive (not an instruction).

When a variable is defined, it is possible to use it in the Assembly language instructions,


where a byte (or word) is allocated in the memory for this variable.

Example

Example
B) Arrays:
It is possible to define an array by giving the array name, length of each element (byte or
word) and elements values.

Example

A DB 48h, 65h, 6ch, 6ch, 6fh, 0h (A[0]=48h, A[1]=65h, …)


B DB ‘Hello’, 0 (B[0]=48h, B[1]=65h, …)
(Note that array B is an exact copy of A, where each character in the string (Hello) is stored as a
byte with the equivalent ASCII code).
‫ش‬ٛ‫ انُٕع االخ‬ٙ‫رى ف‬ٚٔ( Characters ‫ انغادط ػشش ٔسيٕص‬,٘‫ انؼشش‬,ٙ‫ انثُائ‬:‫قح‬ٚ‫ى تاكثش يٍ طش‬ٛ‫ح اػطاء انق‬َٛ‫َالحظ ايكا‬
.)Character‫ نٓزا انـ‬ASCII‫خضٌ انًكافئ انـ‬
Now we can access any element in array A as:
MOV AL, A[4] ; AL= 6fH

Or by using one of the pointers: BX, SI or DI as:


MOV SI, 4
MOV AL, A[SI]
‫ ٔتذٌٔ انحاجح نًؼشفح انًٕقغ انز٘ ذى‬,‫ح انثشَايج‬ٚ‫ َٓا‬ٙ‫فٓا ف‬ٚ‫ ذى ذؼش‬ٙ‫ى انر‬ٛ‫ح نهٕصٕل انٗ انق‬َٛ‫ُا ايكا‬ٚ‫خالل انثشَايج نذ‬
.ٍٛ‫ٍ انغاتقر‬ٛ‫قر‬ٚ‫ ٔرنك تاحذٖ انطش‬,‫ّ يٍ انزاكشج‬ٛ‫خضَٓا ف‬
Getting the address of variables
Two ways can be used to access a variable address:
1) Using LEA instruction:
LEA BX, var1 ; BX = the offset address of var1
2) Using OFFSET:
MOV BX, OFFSET var1 ; BX = the offset address of var1
NOTE1: Any 16-bit register can be used instead of BX in these two examples.
NOTE2: when com file is loaded, the value of DS is set to the same value of CS.

‫ يغ‬,ٍٛ‫ٍ انغاتقر‬ٛ‫قر‬ٚ‫رى رنك تاحذٖ انطش‬ٛ‫ّ يٍ انزاكشج ف‬ٛ‫ٍ ف‬ٛ‫ش يؼ‬ٛ‫ حانح انحاجح نهٕصٕل انٗ انؼُٕاٌ انز٘ ذى خضٌ يرغ‬ٙ‫ف‬
.16-bit ّ‫ عجم طٕن‬ٙ‫جة خضَّ ف‬ٚ ٙ‫ ٔتانران‬16-bit offset ٍ‫كٌٕ ػثاسج ػ‬ٛ‫ّ ع‬ٛ‫االَرثاِ انٗ أٌ انؼُٕاٌ انز٘ عُحصم ػه‬
‫ش؟‬ٛ‫رى خضٌ ْزا انًرغ‬ٚ ‫ عٕف‬Segment ٘‫ أ‬ٙ‫ ف‬/‫ط‬
ٍ‫ش كالًْا انٗ َفظ انًقطغ ي‬ٛ‫ش‬ٚ ‫ث‬ٛ‫ تح‬DS&CS‫ًح كم يٍ انـ‬ٛ‫ذ ق‬ٛ‫رى ذٕح‬ٚ َّ‫ فا‬com file‫م انـ‬ًٛ‫رى ذح‬ٚ ‫ ػُذيا‬:‫انجٕاب‬
.‫ أ٘ يًُٓا‬ٙ‫ش يخضَٔا ً ف‬ٛ‫ًكُُا اػرثاس انًرغ‬ٚ ٙ‫ ٔتانران‬,‫انزاكشج‬
Example

‫ش يٍ خالل ٔضغ‬ٛٛ‫ ٔذى انرأكذ يٍ حصٕل انرغ‬44h ٗ‫ ان‬22h ٍ‫ ي‬VAR1 ‫ش‬ٛ‫ًح انًرغ‬ٛ‫ش ق‬ٛٛ‫ ذى ذغ‬,ِ‫ انًثال أػال‬ٙ‫ف‬
‫ انزاكشج‬ٙ‫ّ ف‬ٛ‫ش يٍ خالل انزْاب انٗ انًٕقغ انز٘ ذى خضَّ ف‬ٛ‫ًح ْزا انًرغ‬ٛ‫ك ق‬ٛٛ‫ضا ً ذج‬ٚ‫ًكٍ أ‬ٚ ٍ‫ ٔنك‬,AL ٙ‫ش ف‬ٛ‫ًح انًرغ‬ٛ‫ق‬
.‫ز انثشَايج‬ٛ‫ تؼذ ذُف‬22h ٗ‫ًح ان‬ٛ‫ش انق‬ٛ‫) ٔيالحظح ذغ‬DS:BX( ‫رًثم تـ‬ٚ ٘‫ٔانز‬

Note:
MOV AX, offset VAR1
In this instruction, if the variable VAR1 is an array, then AX will be the offset of the first
element of this array, i.e. :
MOV AX, offset VAR1 = MOV AX, offset VAR1[0]
= LEA AX, VAR1 = LEA AX, VAR1[0]

Best Regards
Dr. Zainab Alomari
Lecture 5: DOS Interrupts
The interrupt types 20h-3Fh are serviced by DOS routines that provide high-level service to
hardware as well as system resources such as files and directories. The most useful is INT 21H,
which provides many functions for doing keyboard, video, and file operations.

INT 21H (INT 33)


This interrupt performs various operations depending on the value of AH.

1) Reading a Character
After executing (INT 21h), if AH is equal to (1) then a character is read from keyboard with its
ASCII stored in AL.

Example

MOV AH, 01
INT 21H

If the number 4 is inserted by keyboard after execution, then AL will be equal to (34H).

2) Printing a Character
After executing (INT 21h), if AH is equal to (2) then a character is printed on screen, the
ASCII code of this character is taken from DL.
NOTE: after printing the character on screen, the value of DL is also copied to AL.

Example

MOV DL, 52H


MOV AH, 02
INT 21H

After execution, the letter (R) is printed on screen and AL = DL = 52H.

3) Printing a String
After executing (INT 21h), if AH is equal to (9) then a string of characters is printed on
screen. The printed string must be stored starting at (DS:DX), i.e. the offset of the first
character in the string is stored at offset = DX. The printing is stopped when it reaches $.
Example
org 100h
MOV DX, OFFSET msg
MOV AH, 9
INT 21H
RET
msg DB “Hello World$”

4) Reading a String

After executing (INT 21h), if AH is equal to (10) then a string of characters is read from
keyboard and stored starting at (DS:DX+2), where the bytes at DX and DX+1 are reserved for
the buffer size and the number of characters stored in the buffer, respectively.

‫ عهى أل يٕقع‬DS:DX ‫) يسبقب ً ٔانتً ٌجب اٌ ٌؤشش‬Buffer( ‫ ٌجب تٍٓئت راكشة‬21h ‫قبم استخذاو ْزا انُٕع يٍ يقبطعت‬ 
‫ انببٌت االٔل يٍ ْزِ انبفش ًٌثم حجى انبفش ٔانببٌت انثبًَ ًٌثم عذد انقٍى انًٕجٕدة فً انبفش (ٔانزي ٌجب اٌ ال ٌتجبٔص‬.‫يُٓب‬
.DS:DX+2 ٍ‫ ٔببنتبنً انشيٕص انتً ٌتى ادخبنٓب تخضٌ ابتذا ًء ي‬,)‫انطٕل انًعطى فً انببٌت االٔل‬
)enter(‫) يٍ قبم انًستخذو عاليت االَتٓبء يٍ االدخبل ٔنزنك ٌتى تخصٍص حقم نخضٌ قًٍت انـ‬enter( ‫ٌتى اعتببس ادخبل‬ 
.ًَ‫) ٔنكُّ ال ٌحسب ضًٍ انطٕل انًٕجٕد فً انحقم انثب‬0DH( ‫ٔانزي ٌسبٔي‬
. 1- ‫ٔببنتبنً تكٌٕ اقصى قًٍت نهببٌت انثبًَ ًْ قًٍت انببٌت االٔل‬

NOTE: This function does not add $ at the end of the string. So to print this string using INT
21h at AH=9, you must store $ at the string end first, then start printing from DS:DX+2.

Example
Write a program in Assembly language to read a string that has a maximum length of 10
characters, then print this string on the screen.

Solution

org 100h
MOV DX, OFFSET BUFFER
MOV AH, 0AH
INT 21H
MOV BH,00
MOV BL, BUFFER[1]
MOV BUFFER[BX+2], „$‟
MOV DX,OFFSET BUFFER+2
MOV AH, 9
INT 21H
ret
BUFFER DB 10, ?
END
Example
Write a program in Assembly language to print the value of an 8-bit number on the screen
IN BINARY.

Solution

(To solve this question, it is important to notice that printing on screen starts from the left,
therefore the number should be printed from the higher bit to the lower. Also remember that we
need to know the ASCII code of any character in order to print it on screen.)

ASCII Hexadecimal
0 30h
1 31h

org 100h
MOV CL,8 ; counter for printing times
MOV BL,NUM
RPT:MOV DL,30H ; the value of DL will be printed on screen
SHL BL,1 ; checking if the bit is 1 or 0
JNC PRNT ; if the bit is zero, go directly to print
INC DL ; if the bit is one, add one to DL and then print
PRNT:MOV AH,2
INT 21H
DEC CL
JNZ RPT
RET
NUM DB 45H ; example number to be printed on screen in binary

Example

Write a program to print the value of any 16-bit number on screen IN HEXADECIMAL.

Solution

(To write this program, we need to check the digits of the number starting from the higher digit
to the lower, if the digit is between 30H and 39H (after adding 30h to the digit) then we print the
value directly on screen. Otherwise if the digit is more than 39H (then it is a value between A
and F), then we need to add a shift of 7 to the value before printing. Why seven? This can be
explained by looking at the ASCII codes list, we can see that there are 7 characters between
numbers and letters.)
ASCII Hexadecimal
0 30h
1 31h
2 32h
3 33h
4 34h
5 35h
6 36h
7 37h
8 38h
9 39h
(other seven characters whose ASCII codes are: 3Ah,3Bh,3Ch,3Dh,3Eh,3Fh,40h)
A 41h
B 42h
C 43h
D 44h
E 45h
F 46h

org 100h
MOV CL,16
RPT:SUB CL,4
MOV BX,NUM
SHR BX,CL ; shifting the number by 12, 8, 4 and 0 (to take one digit each time)
AND BX,000FH ; to make sure that only one digit is there after shifting
ADD BX,30H
CMP BX,39H ; checking the number
JBE PRNT ; if the number is between 30h and 39h, go directly to print
ADD BX,7 ; if the number is between A and F, add 7 before printing
PRNT:MOV DL,BL ; the number is only 8-bits long
MOV AH,2
INT 21H
CMP CL,0
JNZ RPT
RET
NUM DW 8E1FH ; example number to be printed on screen in hexadecimal
Example

Write a program to print the value of any 16-bit number on screen IN DECIMAL.

Solution

(When converting a number from Hexadecimal to decimal, we divide the number by 10 and the
remainder represents the decimal number. This method will be used in the code so that the
remainder is printed on screen after adding 30h. The remainder is stored in an array in the
memory so that it is printed from the last number to the first).

Simple example:

10 D h remainder

10 1 3

0 1

Divide the whole


number by 10

Store remainder

Yes No
Result=0?

Add 30h to all remainders


stored in the memory

Print them from the end


(from the last to the first)

End
org 100h

MOV SI, 00 ; counter for the number of remainders


MOV BX, OFFSET REMS
MOV CX, 0AH ; to divide by 10
MOV AX, NUM ; the number to be converted from Hexa to decimal
RPT: MOV DX, 00 ; DX is cleared for the division
DIV CX ; dividing the number by 10
MOV [BX], DL ; storing remainder (which is always between 0 and 9)
INC SI
INC BX
CMP AX,00 ; division is repeated until the result of division is 0
JNZ RPT
PRNT: DEC BX ; if division result is zero, start printing
DEC SI
MOV DL, [BX]
ADD DL, 30H ; each number must be added with 30h before printing
MOV AH, 2
INT 21H
CMP SI, 0
JNZ PRNT
RET

NUM DW 0FFFFH ; example number to be printed on screen in decimal


REMS DB ? ; an empty array of bytes for storing division remainders

Example

Write a program to convert a character named (char):

1) from small to capital letter

2) from capital to small letter

3) if capital  small, if small  capital

Best Regards
Dr. Zainab Alomari
Lecture 6: 8086Mp Pin-out Diagram
There are 3 buses in 8086Mp:
 Data bus: 16 data lines (D0 – D15)
 Address bus: 20 Address lines (A0 – A19)
 Control bus: 3 lines (M/IO , RD , WR)

Multiplexing in 8086Mp
 Multiplexing is the use of one line for two different signals.
 Address/Data lines (AD0 – AD15) are multiplexed.
 Address/Data Multiplexing means that the same pin carries an address bit at one time and a
data bit at another time.
 Multiplexing is used to minimize the number of pins.

8086Mp Signal Descriptions


The following signal descriptions are common for both Minimum and Maximum Modes:

AD15-AD0: (Bidirectional signals)

These are the time multiplexed memory I/O address and data lines. Address remains on the
lines during T1 state, while the data is available on the data bus during T2, T3, Tw and T4.

.I/O‫) ًيتى استخذاييا عنذ انتعايم يع انزاكشة أً اجيزة انـ‬data and address(‫ىي خطٌط يشتشكة بيٍ انـ‬ -
.)‫ أي رات اتجاىيٍ (ادخال ًاخشاج‬bidirectional signals ‫ خط ًىي‬61 ٌ‫عذد ىزه انخطٌط ى‬ -
.data‫) تستخذو نهـ‬T2, T3, Tw and T4( ‫ بينًا في‬,)address( ٌ‫ تستخذو ىزه انخطٌط العطاء انعنٌا‬T1 ‫في‬ -

A16/S3
A17/S4
A18/S5
A19/S6
(Status lines are output signals)

Status lines (S3-S6) are multiplexed with address lines (A16-A19). During addressing they
contain the address, while during data transmission they contain status information.

S3 and S4: When data is transmitted through data lines, S3 and S4 lines give the segment used to
transfer data to/from the microprocessor, as in the following table:
S4 S3 Segment Description
0 0 ES Data transfer is to/from ES

0 1 SS Data transfer is to/from SS


1 0 CS/ none* Data transfer is to/from CS

1 1 DS Data transfer is to/from DS

* if this data is not for memory (Ex: Input/Output), then S4 S3 = 10 (none).

NOTE: status lines (S0-S2) are used during maximum mode.

S5: this status line gives the condition of the IF bit as:

When IF = 0  S5=0
When IF = 1  S5=1

S6: this status line is always ‘0’ logic, indicating that 8086 is controlling the system bus.

BHE/S7 (Bus High Enable/S7)

BHE line is used to enable or disable transferring data over the high data lines (D15-D8),
according to the following table:

BHE A0 Description
0 0 word

0 1 One byte is transferred using (D8-D15)


1 0 One byte is transferred using (D0-D7)

1 1 None

* S7 is always at ‘1’ logic.


CLK: is an input signal used for supplying the Microprocessor with the clock signal.
2 GND: input signals that are connected to ground.
VCC: input signal that receives the supply voltage, which must be +5 V.

MN/MX: input signal to specify the mode of operation:

1  Minimum mode: in this mode, the Microprocessor works alone.


0  Maximum mode: in this mode, the Microprocessor works with one or more other
microprocessors.
Control Signals (M/IO , RD , WR): (output signals)

RD: (READ pin) when this pin is at logic ‘0’, it indicates that the Mp is performing memory or
I/O read operation.

WR: (WRITE pin) when this pin is at logic ‘0’, it indicates that the Mp is performing memory or
I/O write operation.

M/IO: this pin is used to specify if the read or write operation performed by the Mp is with the
memory or the I/O device.

M/IO RD WR Description
0 0 1 I/O read
0 1 0 I/O write

1 0 1 memory read

1 1 0 memory write

Hardware Interrupt Lines (INTA, INTR and NMI): explained previously.

ALE: (Address Latch Enable) is an output signal used to inform the memory or I/O device when
a valid address is on the address bus.

DEN: (Data ENable) is an output signal used to inform the memory or I/O device when they
should read/write data on data bus.

HOLD and HLDA: Direct Memory Access Signals (DMA).

External devices can request to take control of the system bus by making HOLD signal =1.
When 8086Mp accepts this request, it makes signal HLDA=1 and enters idle state (or z-state).

TEST: is instruction (wait) is executed, this input is tested as follows:

If TEST =1  execution will continue.

This output is used to decide the direction of data flow through the transreceivers
If TEST =0  Mp remains in idle state until TEST becomes =1.

READY: if a device that is performing a read or write operation with the Mp was not ready for
next data transition, READY signal will be used by this device to tell the Mp to wait
(READY=0) by inserting additional clock cycles between T3 and T4 in read/write bus cycles,
until READY input becomes =1, which indicates that the device is ready to transfer data.
RESET: to reset the Mp. If RESET=1  all registers will be initialized and reset service routine
is executed.

DT/R: (Data Transmit/Receive) this output is used to decide the direction of data flow. ‘1’ logic
means data is transmitted from the microprocessor, ‘0’ logic means data is received by the
microprocessor.

8086Mp Bus Cycles


8086Mp uses memory and I/O devices in periods called (Bus cycles). Each bus cycle
takes 4 system clocks (4T). 8086 Mp Bus Cycles are:

1- Memory Read Bus Cycle


2- Memory Write Bus Cycle
3- Input Bus Cycle
4- Output Bus Cycle

During T1: the address is put on the address/data bus, with ALE=1.
During T2: DEN becomes logic 0, to signal the memory or I/O device when to put data on to the
bus, with RD=0.
During T3: 8086Mp reads data from address/data bus.
During T4: all bus signals are deactivated in preparation for the next clock cycle.
NOTE: Input bus cycle of 8086Mp is equal to Memory read bus cycle, with only one difference:
M/IO is equal to logic 0 during the four cycles (exactly the opposite).

Memory Read bus cycle (‫) يساً نهشسى اعاله ًانخاص بانـ‬Input bus cycle of 8086Mp(‫ سسى انـ‬:‫مالحظة مهمة‬
.‫) تكٌٌ انعكس يًا ىي عهيو في انشسى أعاله‬M/IO( ‫) عذا فشق ًاحذ ًىٌ أٌ االشاسة‬of 8086Mp

NOTE: Output bus cycle of 8086Mp is equal to Memory write bus cycle, with only one
difference: M/IO is equal to logic 0 during the four cycles (exactly the opposite).

Memory Write bus (‫) يساً نهشسى اعاله ًانخاص بانـ‬Output bus cycle of 8086Mp(‫ سسى انـ‬:‫مالحظة مهمة‬
.‫) تكٌٌ انعكس يًا ىي عهيو في انشسى أعاله‬M/IO( ‫) عذا فشق ًاحذ ًىٌ أٌ االشاسة‬cycle of 8086Mp

Best Regards
Dr. Zainab Alomari
Lecture 7: Demultiplexing, Buffering and Latching

Fully Buffered 8086 Microprocessor


Demultiplexing is the operation of splitting multiplexed signals, like separating Address/Data
lines into address lines and data lines.

 Data bus requires demultiplexing and buffering.


 Address bus requires demultiplexing, latching and buffering.
 Control bus requires only buffering.

74LS245
It is a bidirectional buffer used for demultiplexing and buffering data bus. It has 8-lines so
that two buffer are required.

Bus A Bus B
(8 lines) (8 lines) G DIR Operation

‘245 0 0
Transfer data
from bus B to bus A

Transfer data
0 1
G DIR from bus A to bus B

DEN 1 X Isolation

DT/R

G = Gate (works as enable)


DIR = Direction of data transfer

74LS373
It is a unidirectional buffer used for demultiplexing, buffering and latching address lines and
BHE. It has 8 inputs and 8 outputs, so that three buffers are required. (OE = Output Enable)

Input Output
(8 lines) (8 lines)

‘373

G OE

ALE
ALE
74LS244
It is a unidirectional buffer used for buffering the signals of the control bus, so that one buffer
is required.

Input Output
(8 lines) (8 lines)

‘244

OE

Best Regards
Dr. Zainab Alomari
Lecture 8: Memories (part 1)
Every Microprocessor-based system has a memory system. There are two main types of
memories:

- Random Access Memory (RAM)


- Read Only Memory (ROM)

The size of the memory device is indicated as follows:

Memory device name (No. of Memory Locations X Bits per Location)

‫فش اٌىثٍش‬ٛ‫ذر‬ٚ .‫اٌثشاِح اٌّخرٍفح‬ٚ ‫ِاخ‬ٍٛ‫ذحًٍّ اٌّؼ‬ٚ ْ‫ا ٌخض‬ِٙ‫اسرخذا‬ٚ ‫ا تاٌّؼاٌداخ‬ٙ‫ٌرُ ذصٍٕغ سلائك اٌزاوشج ٌٍرُ ستط‬
‫تح‬ٛ‫ْ ِىر‬ٛ‫ ٌّىٓ ِؼشفح حدُ سلالح اٌزاوشج ِٓ خالي االسلاَ اٌرً ذى‬ٚ .‫تأحداَ ِخرٍفح‬ٚ ًّ‫ق اٌؼ‬ٛ‫ِٓ سلالاخ اٌزاوشج فً س‬
‫ط‬ٛ‫ أِا اٌشلُ اٌزي ٌسثك اٌم‬,ٍٓ‫س‬ٛ‫احذ تٍٓ اٌم‬ٌٛ‫لغ ا‬ٌّٛ‫تح فً حدُ ا‬ٚ‫الغ ِضش‬ٌّٛ‫ حٍث ٌرُ اػطاء ػذد ا‬,ٖ‫ا تاٌطشٌمح أػال‬ٍٍٙ‫ػ‬
.‫ا‬ٌٙ ُ‫تّثاتح أس‬ٚ ‫ع ِٓ اٌشلالاخ‬ٛٔ ً‫ سلُ ٍِّض ٌى‬ٛٙ‫ف‬

Example (1)

7216 (2K x 8) memory

This memory device contains 2K locations, 11 address lines and 8 data outputs.

.)2N ٛ٘ ‫الغ‬ٌّٛ‫ ارا واْ ػذد ا‬N ‫ي‬ٚ‫ ٌسا‬address‫ط اٌـ‬ٛ‫الغ (ػذد خط‬ٌّٛ‫ٔح ِٓ خالي ػذد ا‬ٕٛ‫ط اٌؼ‬ٛ‫ٌرُ ِؼشفح ػذد خط‬ 
.‫لغ‬ِٛ ً‫ ِٓ ػذد اٌثراخ ٌى‬data‫ط اٌـ‬ٛ‫ٌرُ ِؼشفح ػذد خط‬ 

Example (2)

62256 (32K x 8) memory

This memory device contains 32K locations, 15 address lines and 8 data outputs.

Note: If only one number is mentioned for a memory device, this number represents the total
number of memory bits and it is called (bit capacity).

‫ فثاٌراًٌ ِٓ ِؼشفح ػذد‬.ًٍ‫ً٘ ػذد اٌثراخ اٌى‬ٚ ‫اط‬ٛ‫دٌٓ فً داخً األل‬ٛ‫خ‬ٌّٛ‫ ٌّثً حاصً ضشب اٌشلٍّٓ ا‬bit capacity‫اٌـ‬
‫لغ‬ٌّٛ‫ ِٓ ِؼشفح ػذد اٌثراخ فً ا‬:ٚ‫ أ‬,‫احذ‬ٌٛ‫لغ ا‬ٌّٛ‫الغ ٌّىٓ ِؼشفح ػذد اٌثراخ فً ا‬ٌّٛ‫ػذد ا‬ٚ (bit capacity) ًٍ‫اٌثراخ اٌى‬
.ًٍ‫الغ اٌى‬ٌّٛ‫ ٌّىٕٕا ِؼشفح ػذد ا‬bit capacity‫اٌـ‬ٚ ‫احذ‬ٌٛ‫ا‬

Example (3)

(1K x 8) memory is listed as an 8K memory device, while (64K x 4) memory is listed as a


256K memory device.
Types of Memory Devices
There are two main memory devices:

 Random Access Memory (RAM)


 Read Only Memory (ROM)

The main differences between these types are:

1- A RAM is written under normal operation. A ROM can be programmed, but normally it is only
read.
2- RAMs are used to store temporary data, while ROMs are used to store permanent data.

General block diagram of RAM memory devices


ً‫ حٍث ذحراج دائّا ً اٌى ادخاي ٌّث‬,‫ا‬ّٙ‫ّا واْ حد‬ِٙ RAM ‫ اٌزي ٌّثً أي سلالح‬block diagram‫اٌشىً اٌراًٌ ٌّثً اٌـ‬
‫ لشاءج فمظ فً حاٌح أْ ادخاي‬ٚ‫ذؼًّ اٌشلالح ػٍٍّح وراتح أ‬ٚ . data‫ط اٌـ‬ٛ‫ اٌرً ذّثً خط‬Input/Output‫ط اٌـ‬ٛ‫خط‬ٚ ْ‫ا‬ٕٛ‫اٌؼ‬
‫ٔح فٍرُ ِؼشفرٗ ِٓ خالي‬ٕٛ‫ط اٌؼ‬ٛ‫أٗ تخط‬ٕٛ‫لغ اٌّحذد ػ‬ٌٍّٛ ‫ أِا اٌؼٍٍّح ً٘ ً٘ لشاءج أَ وراتح‬.) 0 logic ‫ ِفؼً (أي‬CS‫اٌـ‬
.)0 logic ‫ّا‬ٌٙ‫ّا ِفؼً (أي أ‬ٌٙ‫ أ‬WE ٚ RE ٍٓ‫اٌخط‬

A0 O0
A1 O1
A2 O2
. .
N-Address Lines K-Data Lines
. .
(Input) . . (In/Out)
AN OK

CS= Chip Select


CS RE WE
RE= Read Enable

WE= Write Enable

General block diagram of RAM memory devices


General block diagram of ROM memory devices
ٗٔ‫) وّا أ‬output( ‫احذ‬ٚ ٖ‫ ً٘ تاذدا‬ROM‫ فً اٌـ‬data‫ط اٌـ‬ٛ‫اٌّالحظ ِٓ اٌشسُ أْ خط‬ٚ ٍٓ‫ػ‬ٌٕٛ‫اٌفشق اٌشئٍسً تٍٓ ا‬
.)RE( ‫أّا ٌٍمشاءج فمظ‬ٚ )WE( ‫خذ خظ ٌٍىراتح‬ٌٛ‫ال‬

A0 O0
A1 O1
A2 O2
. .
N-Address Lines K-Data Lines
. .
(Input) . . (Output)
AN OK

CS RE

General block diagram of ROM memory devices

8086Mp Memory Organization and Interfacing


8086Mp 1MByte memory is implemented using 2 independent 512Kbyte banks:

- Low Bank (Even Bank)


- High Bank (Odd Bank)

:512Kbyte ‫ّا‬ِٕٙ ً‫وٍٓ حدُ و‬ٍٛ‫ا ذُ تٕاؤ٘ا تاسرخذاَ ت‬ٙٔ‫ٔالحظ تأ‬ٚ ,8086Mp‫ٔأذً اٌى ذصٍُّ اٌزاوشج اٌخاصح تاٌـ‬

ًٙ‫ٌٕر‬ٚ 00000H ‫لغ‬ٌّٛ‫ٌثذأ تا‬ٚ )Even bank( ٚ‫) أ‬Low bank( ‫ٌسّى‬ٚ ‫خٍح‬ٚ‫ٌٓ اٌض‬ٚ‫الغ راخ اٌؼٕا‬ٌّٛ‫ي ا‬ٛ‫ ٌحر‬:‫األول‬
. FFFFEH ‫لغ‬ٌّٛ‫تا‬

ًٙ‫ٌٕر‬ٚ 00001H ‫لغ‬ٌّٛ‫ٌثذأ تا‬ٚ )Odd bank( ٚ‫) أ‬High bank( ‫ٌسّى‬ٚ ‫ٌٓ اٌفشدٌح‬ٚ‫الغ راخ اٌؼٕا‬ٌّٛ‫ي ا‬ٛ‫والثاني ٌحر‬
.FFFFFH ‫لغ‬ٌّٛ‫تا‬
‫‪High Bank‬‬ ‫‪Low bank‬‬

‫‪High and Low Banks of 8086Mp Memory‬‬


‫ِالحظاخ ِ‪ّٙ‬ح خذا ً ػٓ اٌشسُ أػالٖ‪:‬‬

‫تّا أْ حدُ وً تٍ‪ٛ‬ن ٘‪ 512Kbyte ٛ‬فثاٌراًٌ ػذد خط‪ٛ‬ط اٌؼٕ‪ٔٛ‬ح ٌىً ِٓ اٌثٍ‪ٛ‬وٍٓ ٘‪ 91 ٛ‬خظ‪٘ ,‬زٖ اٌخط‪ٛ‬ط ذشتظ ػٍى‬
‫اٌخط‪ٛ‬ط اٌخاصح تاٌـ‪ ِٓ address‬اٌـ‪ٚ 8086Mp‬اٌرً ً٘ ِٓ ‪ A1‬اٌى ‪ A19‬أِا ‪ A0‬فألٔٗ ٌحذد ً٘ اٌؼٕ‪ٛ‬اْ فشدي أَ ص‪ٚ‬خً‬
‫ف‪ٌ ٛٙ‬شتظ ػٍى أشاسج اٌـ‪ٌٍ CS‬ـ‪ Low bank‬فارا واْ اٌؼٕ‪ٛ‬اْ اٌمادَ ِٓ اٌّؼاٌح ص‪ٚ‬خً فإْ ‪ A0‬سٍى‪ٌ ْٛ‬سا‪ٚ‬ي ‪ٚ 0‬تاٌراًٌ سٍرُ‬
‫ذفؼًٍ اشاسج ‪ٌٙ CS‬زا اٌثأه‪.‬‬

‫أِا ارا واْ اٌؼٕ‪ٛ‬اْ اٌّطٍ‪ٛ‬ب فشدي ف‪ٕٙ‬اٌه اشاسج ٌفؼٍ‪ٙ‬ا اٌّؼاٌح ٌالشاسج اٌى و‪ ْٛ‬اٌؼٕ‪ٛ‬اْ فشدي ‪ٔ .BHE ً٘ٚ‬الحظ أٔ‪ٙ‬ا‬
‫ذصثح ذسا‪ٚ‬ي ‪ 0‬ارا واْ اٌؼٕ‪ٛ‬اْ فشدي ‪ٌٙٚ‬زا ٌرُ ستط‪ٙ‬ا تاٌـ‪ CS‬اٌخاص تاٌـ‪.High bank‬‬

‫وً ٘زا اٌىالَ ٘‪ ٛ‬فً حاي و‪ ْٛ‬اٌذاذا اٌّطٍ‪ٛ‬ب لشائر‪ٙ‬ا أ‪ ٚ‬وراتر‪ٙ‬ا فً اٌزاوشج ً٘ تاٌد ‪ٚ‬احذ فمظ‪ ,‬وّا فً األِثٍح اٌراٌٍح‪:‬‬

‫‪‬‬ ‫]‪Mov AL, [200H‬‬


‫‪‬‬ ‫‪Mov [201H], AL‬‬
BHE A0 Memory operation
0 0 Word (Both banks are enabled)
0 1 High byte (Only high bank is enabled)
1 0 Low byte (Only low bank is enabled)
1 1 No operation (Both banks are disabled)

Note:

 If address is even (ex: MOV AX,[300H] ), then both of BHE and A0 are logic 0, and the
data transfer from low bank takes 1 bus cycle (4 clocks).
 If address is odd (ex: MOV AX, [301H] ), then the first byte (lower byte) in high bank
takes 1 bus cycle (4 clocks), where (BHE =0 , A0=1).and then the second byte (high byte)
in low bank takes 1 other bus cycle (4 clocks), where (BHE =1 , A0=0).

ِٓ ٍٓ‫ فثاٌراًٌ سٕحراج اٌى خٍة اٌثاٌر‬,‫ تد‬91 ‫ أي‬word ً٘ ‫ا فً اٌزاوشج‬ٙ‫ وراتر‬ٚ‫ا أ‬ٙ‫ب لشائر‬ٍٛ‫ اٌّط‬data‫أِا فً حاٌح اْ اٌـ‬
‫ فىٍف سررُ اٌؼٍٍّح؟‬.‫احذ فمظ‬ٚ ‫ي ػٍى تاٌد‬ٛ‫ف ٌحر‬ٚ‫ ِؼش‬ٛ٘ ‫لغ ِٓ اٌزاوشج وّا‬ِٛ ً‫ الْ و‬,ٍٓ‫و‬ٍٛ‫وال اٌث‬

)0= ‫ (والّ٘ا سٍصثح‬BHEٚ A0 ِٓ ً‫) فسٍرُ ذفؼًٍ و‬MOV AX,[300H] :‫ال‬ ً ‫خً (ِث‬ٚ‫اْ ص‬ٕٛ‫ْ اٌؼ‬ٛ‫ فً حاٌح و‬:‫أو ًال‬
‫ن اٌفشدي‬ٍٛ‫اٌثأً فً اٌث‬ٚ 300H ْ‫ا‬ٕٛ‫خً فً اٌؼ‬ٚ‫ن اٌض‬ٍٛ‫ي فً اٌث‬ٚ‫ اال‬,‫لؼٍٓ ِٓ اٌزاوشج‬ٌّٛ ٍٓ‫ اػطاء تاٌر‬ٚ‫تاٌراًٌ سٍرُ أخز أ‬ٚ
.301H ْ‫ا‬ٕٛ‫فً اٌؼ‬

‫ اػطاء لٍّح اٌثاٌد‬ٚ‫أخز أ‬ٚ BHE ‫) فسٍرُ ذفؼًٍ اشاسج‬MOV AX,[301H] :‫ال‬ ً ‫اْ فشدي (ِث‬ٕٛ‫ْ اٌؼ‬ٛ‫ فً حاٌح و‬:ً‫ثانيا‬
ُ‫) ثُ تؼذ٘ا ٌر‬4 clock cycles( ‫ اٌىراتح أي‬ٚ‫سج واٍِح ٌٍمشاءج أ‬ٚ‫رٌه ٌسرغشق د‬ٚ high bank‫) ِٓ اٌـ‬lower byte( ‫ي‬ٚ‫األ‬
other 4 ( ‫سج ثأٍح واٍِح أي‬ٚ‫٘زا سٍسرغشق د‬ٚ low bank‫ اػطاء اٌثاٌد اٌراًٌ ِٓ اٌـ‬ٚ‫ تحٍث ٌرُ أخز أ‬A0 ‫ذفؼًٍ اشاسج‬
.)clock cycles

‫احذج‬ٚ ‫ اٌىراتح ِا ػذا فً حاٌح‬ٚ‫ا ًء فً اٌمشاءج أ‬ٛ‫) س‬4 clock cycles( ‫احذج أي‬ٚ bus cycle ‫ اٌرؼاًِ ِغ اٌزاوشج ٌسرغشق‬:ً‫ارا‬
8 ( ‫) أي‬2bus cycles( ‫ ففً ٘زٖ اٌحاٌح سرسرغشق اٌؼٍٍّح‬,‫اْ فشدي‬ٕٛ‫اٌؼ‬ٚ ‫ تد‬91 ‫ وراتح‬ٚ‫ لشاءج أ‬ٛ٘ ‫ب‬ٍٛ‫ً٘ أْ اٌّط‬ٚ
.)clock cycles

Best Regards
Dr. Zainab Alomari
Lecture 9: Memory Devices (Part 2)
Address Decoding
1) Simple NAND gate Decoding

Example

Design the hardware required to implement (128K x 8) RAM starting at address 20000H, using
(64K x 8) RAMs.
‫ فْحراج ٍعشفح‬,ً‫ اقو ٍْٔ حدَا‬RAM ‫ تاسرخذاً تي٘ماخ‬,‫اىَطي٘ب فً ٕزا اىسؤاه ذصٌٍَ ٗستظ رامشج تحدٌ ٍعٍِ اىى اىـَعاىح‬
.‫اىحدٌ ىيزامشج ٍٗعشفح عْ٘اُ اىثذاٌح ىٖا ٗاىزي سٍؤثش عيى طشٌقح اىشتظ‬
Solution

128K/64K = 2
⸫ No. of (64K x 8) RAMs =2
No. of RAMs in each bank =1
Total no. of address lines (for the 128Kbyte memory) = 17
No. of address lines for each (64K x 8) RAM = 16

.odd bank ًّ‫ ٗاىثا‬even bank ‫ دائَا ً ٌرٌ فصو اىثي٘ماخ اىى قسٍَِ أحذَٕا‬:‫ٍالحظح‬
.‫ ٕٗنزا‬, ٍِ‫تَعْى ى٘ ٍثالً احردْا استع تي٘ماخ ىثْاء اىزامشج اىَطي٘تح ىناُ مو تاّل ٌحر٘ي عيى تي٘م‬
‫ ٗتَا أُ حدٌ مو تي٘ك ْٕا فً ٕزا اىسؤاه‬,ُ‫ٗاالُ ٍِ حدٌ اىثي٘ك اى٘احذ سْعشف عذد اىخط٘ط اىرً ّحراج ستطٖا اىى اىعْ٘ا‬
ُ‫ حٍث أ‬,A16‫ ٗتاىراىً ّْرًٖ تـ‬A1 ٍِ ً ‫ فْقً٘ تاسرخذاً خط٘ط اىعْ٘اُ تذءا‬,‫ خظ ىنو تي٘ك‬61 ‫ فثاىراىً ّحراج اىى‬64K ٕ٘
A18 ٗ A17 ًٕٗ 0801 ‫ ذثقى اىخط٘ط اىقادٍح ٍِ اىَعاىح‬.even bank‫ اىخاصح تاىـ‬CS‫ ٌرٌ ستطٔ عيى اشاسج اىـ‬A0
.NAND ‫ ٕزٓ اىخط٘ط اىَرثقٍح ٌرٌ ستطٖا عيى ت٘اتح‬.A19ٗ

A19 A18 A17 A16 A15 A14 A13 A12 A11 A10 A9 A8 A7 A6 A5 A4 A3 A2 A1 A0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 = 20000H

to NAND to address input


gate to CS of even bank

(20000H is the starting address of the designed memory).


 16 bits of this address (from A1 to A16) are used as the address input of the two RAMs.
 A0 is connected to the CS of the low (even) bank with (A19 A18 A17) using NAND gate.
 BHE is connected to the CS of the high (odd) bank with (A19 A18 A17) using NAND gate.
‫( ذشتظ حسة اىعْ٘اُ اىَطي٘ب اعطائٔ ىيزامشج حٍث أّْا ّضع ت٘اتح اىقية عيى‬A19 A18 A17( ُ‫ّالحظ أُ خط٘ط اىعْ٘ا‬
ٍِ ‫ فَعْآ أُ اىعْ٘اُ اىَ٘خ٘د عيى خط٘ط اىعْ٘اُ اىقادٍح‬886 ‫ فثاىراىً ارا ىٌ ذنِ ٕزٓ االشاساخ اىثالثح‬A19ٗ A18 ٍِ ‫مو‬
.CS‫ فال ٌرٌ ذفعٍو اشاساخ اىـ‬, ‫اىَعاىح ال ذخص ٕزٓ اىزامشج اي ىٍسد ضَِ اىشٌْح اىخاص تٖا‬

NAND Output CS state


1 CS is not active
0 CS is active

Q) What is the ending address of the designed memory in the above example?
Answer: we fill the bits of the address from A0 to A16 with ones and this is the ending address,
(= 0011 1111 1111 1111 1111 = 3FFFFH)
2) 74LS138 Decoder
When more than one memory device is used in each bank, a decoder is needed to select
the correct memory device from each bank.

74LS138 decoder has 3 inputs, 8 outputs and 3 enable inputs. When the enable inputs are
all active, one output is activated according to the input. The work of this decoder is summarized
in the following truth tables.

The Block Diagram and Truth Table of 74LS138 Decoder


Example

Design the hardware required to implement a (64K x 8) RAM start at address A0000H, using
(16K x 8) RAMs.
Solution
64K/16K = 4
⸫ No. of (64K x 8) RAMs =4
No. of RAMs in each bank =2
Total no. of address lines (for the 64K byte memory) =16
No. of address lines for each (16K x 8) RAM = 14

A19 A18 A17 A16 A15 A14 A13 A12 A11 A10 A9 A8 A7 A6 A5 A4 A3 A2 A1 A0
1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 =A0000H

to AND gate to address input


of decoder to the first to the enable
input of decoder of the decoder
Example

Design the hardware required to implement a (128K x 8) RAM start at address 00000H, using
(32K x 8) RAMs.

Solution

128K/32K = 4
⸫ No. of (64K x 8) RAMs =4
No. of RAMs in each bank =2
Total no. of address lines (for the 128K byte memory) =17
No. of address lines for each (32K x 8) RAM = 15

A19 A18 A17 A16 A15 A14 A13 A12 A11 A10 A9 A8 A7 A6 A5 A4 A3 A2 A1 A0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 = 00000H

to AND gate to address input


of decoder to the enable
to the first of decoder
input of decoder
Best Regards
Dr. Zainab Rami Alomari
Lecture 10: Basic Input/Output Device Interfacing (Part 1)
1) Basic Input Devices Interfacing
74LS244 Buffer is used to interface input devices to the 8086Mp.

Example

The hardware required to connect 8 switches to 8086Mp is as follows:

NOTE1: I/O address (port no.) can be 8-bits or 16-bits. However, all 16 address lines (A0-A15)
of the 8086Mp are used to enable I/O devices.
If address is 8-bits, the Mp puts zeros on (A8-A15). This is to differentiate between (for
example): port no. 1F3AH (16 bits) and 3AH (8 bits).
Therefore, NAND gate has 19 inputs (M/IO, RD, BHE or A0 + 16 address lines).

NOTE2: I/O device data can be 8-bits or 16-bits.


1) If data is 16 bits: address is always even.
Low byte is transferred over (D0-D7) and high byte is transferred over (D8-D15).
2) If data is 8 bits: address can be:
a. Even: data is transferred over D0-D7.
b. Odd: data is transferred over D8-D15.
Example

Design the hardware required to interface 8 switches to the 8086Mp at port number F301H.

Solution

A15 A14 A13 A12 A11 A10 A9 A8 A7 A6 A5 A4 A3 A2 A1 A0


1 1 1 1 0 0 1 1 0 0 0 0 0 0 0 1 = F301H
2) Basic Output Devices Interfacing
74LS373 Buffer and latch is used to interface output devices to the 8086Mp.

Example

The hardware required to connect 8 LEDs to 8086Mp is as follows:

NOTE: OE of ‘373 is always active (Logic 0). When OUT instruction is executed, the output
data are latched (on LEDs) until the next OUT instruction is executed.

Example

Design the hardware required to interface 16 LEDs to 8086Mp at port no. F300H.

Solution

A15 A14 A13 A12 A11 A10 A9 A8 A7 A6 A5 A4 A3 A2 A1 A0


1 1 1 1 0 0 1 1 0 0 0 0 0 0 0 0 = F300H
Example

Write a code in Assembly language to control the LEDs in the previous example so that
the first LED turns on for a specific delay, then the second LED is turned on for same delay and
so on.

Solution

MOV DX, 0F300H


MOV AX, 0FFFEH
RPT: OUT DX, AX
CALL DELAY1
ROL AX, 1
JMP RPT
HLT

Best Regards
Dr. Zainab Alomari
Lecture 11: Basic Input/Output Device Interfacing (Part 2)
2) Basic Output Devices Interfacing
Single Pole Double Through Relay (SPDT)
SPDT is an electrically operated switch, used to control a circuit by a low power signal.

‫ عىذ انشبط (مب عذا ارا كبن انجهبص‬SPDT‫) فٍجب استخذام انـ‬Output Device( ‫عىذ استخذام اي جهبص اخشاج‬
‫( ارا كبوت االشبسة انقبدمت مه‬Output Device)‫ انى انـ‬Vcc‫ حٍث تعمم كسىٌج ٌقىم بتىصٍم انـ‬,)‫ فال حبجت نزنك‬LEDs
.0= ‫ وتقطع انتىصٍم ارا كبوت االشبسة انقبدمت مه انمعبنج‬1= ‫انمعبنج‬

‫‘) فً حٍه تحتبج االجهضة انكهشببئٍت انى‬1’ Logic = 5Volt) ‫ أن االشبسة انقبدمت مه انـمعبنج تكىن بفىنتٍت واطئت‬:‫وانسبب‬
.)220Volt( ‫فىنتٍت تعبدل‬

Example

Design the hardware required to control a traffic light system (at port no. F300H) of four-road
intersection. Write a code in Assembly language to control it as follows:
Green time = 2 minutes
Yellow time = 20 seconds

Solution

X R Y G X R Y G X R Y G X R Y G
4441H= 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 2min
4442H= 0 1 0 0 0 1 0 0 0 1 0 0 0 0 1 0 20sec
4414H= 0 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0 2min
4424H= 0 1 0 0 0 1 0 0 0 0 1 0 0 1 0 0 20sec
4144H= 0 1 0 0 0 0 0 1 0 1 0 0 0 1 0 0 2min
4244H= 0 1 0 0 0 0 1 0 0 1 0 0 0 1 0 0 20sec
1444H= 0 0 0 1 0 1 0 0 0 1 0 0 0 1 0 0 2min
2444H= 0 0 1 0 0 1 0 0 0 1 0 0 0 1 0 0 20sec
Software:

MOV DX, 0F300H


RPT: MOV BX, 0
MOV CX, 4
NXT: MOV AX, Array[BX]
OUT DX, AX
CALL DELAY1 ; 20 min delay
MOV AX, Array[BX+1]
OUT DX, AX
CALL DELAY2 ; 20 sec delay
ADD BX, 2
LOOP NXT
JMP RPT
HLT
RET
Array DW 4441h, 4442h, 4414h, 4424h, 4144h, 4244h, 1444h, 2444h
END

Solution 2 (‫)يمكن كتابة الكود بطريقة ثانية‬

MOV DX, 0F300H


RPT: MOV CX, 4
LEA BX, Array
NXT: MOV AX, [BX]
OUT DX, AX
CALL DELAY1
MOV AX, [BX+2]
OUT DX, AX
CALL DELAY2
ADD BX, 4
LOOP NXT
JMP RPT
HLT
RET
Array DW 4441h, 4442h, 4414h, 4424h, 4144h, 4244h, 1444h, 2444h
END
Example

Design the hardware and software required to interface a transducer, fan and heater to
8086Mp. The Mp reads the temperature from the transducer (8-bit signed data), and according to
this temperature, it derives the fan and heater to keep temperature between 18oC and 25oC.
Connect the transducer to port no. E300H and connect the fan and heater to port no. A401H.

FAN ON
HEATER OFF
------------------ 25oC
FAN OFF
HEATER OFF
------------------ 18oC
FAN OFF
HEATER ON

Solution
Software:

START: MOV DX, E300H


IN AL, DX
CMP AL, 18
JG N1
MOV AL, 01 ; Heater is ON, Fan is OFF
MOV DX, 0A401H
OUT DX, AL
JMP START
N1: CMP AL, 25
JG N2
MOV AL, 00 ; Heater is OFF, Fan is OFF
MOV DX, 0A401H
OUT DX, AL
JMP START
N2: MOV AL, 02 ; Heater is OFF, Fan is ON
MOV DX, A401H
OUT DX, AL
JMP START
HLT
RET

Best Regards
Dr. Zainab Alomari

You might also like