معالجات دقيقة
معالجات دقيقة
The first Mp in the world, Intel 4004 was invented in 1971. 4004 Mp has:
After 4004, Microprocessors gradually developed in the memory size, speed and no. of
instructions.
, انقٍاو تحساتاخ يرُٕػح, يؼاندح يؼهٕياخ, يُٓا قشاءج ادخال,انًؼانح ْٕ سقاقح قاتهّ نهثشيدح ذسرخذو نؼًهٍاخ يرؼذدج
. انخ... ,انرخاطة ٔانسٍطشج ػهى اخٓضج يؼٍُح
(يغ اَّ الٌقاسٌ يغ االيكاٍَاخ انًٕخٕدج فً انًؼانداخ انحذٌثح) ْٕ اَّ ًَٕرج يصغش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
حٍث ٌُظش انًثشيح انى انًؼانح ػهى أَّ يدًٕػح يٍ انسدالخ انًرُٕػح ٔانرً ٌرى, انشسى أػالِ ًٌثم َظشذُا انى انًؼانح
يٕضٕع يادج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.
ّاندذٔل اػالِ رٔ اًٍْح تانغح حٍث سٍرى انشخٕع انٍّ تشكم دائى الٌ انًثشيح ٌدة اٌ ٌؼهى انسدم انزي ٌرى اسرخذاي
. االستؼح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
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:
**************
ٙركٌٕ انؼُٕاٌ انًُطمٚ .حٛرؼايم يؼ ٓا اال يٍ خالل انؼَُٕح انًُطمٚٔ صم انٗ ا٘ يٕلغ يٍ انزاكشجٚ ًٌكٍ نهًثشيج اٚال
داخم ْزاٙؤشش انٗ يٕلغ يذذد فٚ ً٘ح انًؤشش انزٛذًم لٚ َٙ ) ٔانثا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:
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) ٗ ان,ػُٕاٌ يٍ انزاكشج
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)
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:
كٌٕ رنك يٍ خاللٚٔ ,)اذٓا خالل انفصم األٔلٛرى ذؼهى اعاعٛ عٙرى انرؼايم يغ انًؼانح يٍ خالل انهغح انخاصح تّ (ٔانرٚ
.اخ يرُٕػحٛاو تؼًهًٛكٍ اعرخذايٓا نهقٚ ٙانُظش انٗ انًؼانح ػهٗ ا َّ يدًٕػح يٍ انغدالخ انر
رىٛ ٔع.)destination() انٗ انـsource(ًح انـٛؼاص َقم أ َغخ قٚ) ْٕٔ اMOV( ْٕ ً ؼاص االتغظ ٔاالكثش اعرخذاياٚاال
.)Addressing Modes( ؼاص نششذ االَٕاع انًخرهفح يٍ انؼَُٕحٚاعرخذاو ْزا اال
Addressing Modes
1) Register Addressing Mode
كَٕا تُفظ انطٕلٚ ٌدة اٚٔ )register( ) ػثاسج ػٍ عدمdestination() ٔانـsource(كٌٕ كم يٍ انـٚ ْزا انُٕعٙف
.)تد66 تد أٔ كالًْا8 (ئيا كالًْا
-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
ال ٔٚكٌٕ تُفظ طٕل انـ( )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 1FH, AL
MOV 55, DX
ٔكزنك انسال ارا.حٛش يُطقٛح غٛ ػًهٙٓ ف,ّٛح انُقم ذرى انٛ سقًا ً الٌ ػًهdestinationكٌٕ انـٚ ًٌكٍ اٚ َالزظ أَّ ال
.دٕصٚ ضا ً الٚ ػثاسج ػٍ اسقاو فٓزا ا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
.
.
.
ٙ كًا ف,66 ٙ تؼذ ضشتّ فDSًح انـٛ ثى ذدًغ يغ قoffsetث ذًثم انـٍٛ االقٕاط انًشتؼح زًٛح انًٕخٕدج تٛرى اعرخذاو انقٚ
.انًثال انغاتق
Example 2:
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
.
.
.
. انغإالٙ يدشد يثال ٔذؼطٗ فْٙ انزاكشج ُْاٙى انًخضَٔح فٛ انق:مالحظة
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 byte ptr[3A88H], 5H One memory location at DS:3A88H will be given the value 05H
MOV DS, [3000H] NOT ALLOWED (segment registers can NOT be used with memory)
نًٕقغ انزاكشجoffsetًرّ ذًثم انـٛ) قregister( ظ تشكم سقى ٔاًَا تشكم عدمٛ نoffsetرى اػطاء انـٚ ْزا انُٕعٙف
.ٍ اقٕاط يشتؼحٕٛضغ ْزا انغدم تٚ ثٛ ز,انًطهٕب
يإششْٙٔ حَٛح ثاًٛ أٔ ُْانك ذغ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
.
.
.
PA = 0 1 0 0 0
1000 +
02000H
انغإالٙ زال ذى اػطائّ فٙ ٔنكٍ ف,رٛ كالًْا صسٙائٚضٛ أٔ انفٙش انؼُٕاٌ ػهٗ انزاكشج تانشكم انًُطقًٛكٍ ذأشٚ :مالحظة
ٍ نُصم نهًٕقغ أ انًٕاقغ انًطهٕتح يٙ يٍ انؼُٕاٌ انًُطقٙائٚضٛح فُسراج انٗ زغاب انؼُٕاٌ انفٛائٚضٛقح انؼَُٕح انفٚتطش
.انزاكشج
MOV DS, [DI] NOT ALLOWED (segment registers can NOT be used with memory)
.Indirect Addressing Mode يدًٕػح يٍ االيثهح انًرُٕػح ػهٗ انـٙ اندذٔل انرانٙف
5) Register Relative Addressing Mode
. داخم االقٕاط انًشتؼحoffsetرى اعرخذاو عدم يغ سقى الػطاء انـٚ ْزا انُٕعٙف
.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
: يثال,ٍضا ً يًكٚ خاسج االقٕاط انًشتؼح ْٔزا ا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.
:offset) إلػطاء انـDI ٔ أSI ( خشٜ) ٔاBX ٔ أBP( ٍ أزذًْاٛرى اعرخذاو عدهٚ ْزا انُٕعٙف
where:
BP ارا ذى اعرخذاو انـْٙٔ يا ػذا زانح ٔازذج, PA ػُذ زغاب انـsegment كـDSرى اعرخذاو انـٚ كم انساالخٙف
. أيثهح ػهٗ ْزا انُٕعٙ اندذٔل انرانٙ ف.PA ػُذ زغاب انـsegment ْٕ انـSSؼرثش انـٚ فؼُذْا
MOV [BX+SI], SP
Solution:
F4H 8CEACH
3EH 8CEABH
تاإلضافح,offset) إلػطاء انـDI ٔ أSI ( خشٜ) ٔاBX ٔ أBP( ٍ أزذًْاٛرى اعرخذاو عدهٚ ْزا انُٕع يٍ انؼَُٕحٙف
.ٍٍٛ انغاتقٛ أ٘ أٌ ْزا انُٕع ْٕ زاصم ديح انُٕػ,ًٓاٛضاف انٚ انٗ سقى
where:
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):
67H 10133H
7BH 10132H
ACH 10131H
DH 10130H
81H 1012FH
Solution:
⸫ AX = AC0DH
Note: if the instruction is modified to: MOV AX, [BP + SI + 100H]
*****
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 َقىو هزا االَؼاص ترثذَم يحرىَاخ انـ
Register , Memory
Memory , Register
هُا الٌ كم يٍ انقًُرٍُ انًىخىدذٍُ تدىاس االَؼاصImmediate addressing modeال َدىص اسرخذاو َىع انـ
.سىف ذسرثذل يغ االخشي
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.
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 DS, DX
Xchg [BX], AH
Solution (3)
MOV DS, DX
MOV AL, AH
MOV [100H], AL
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فسها.
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.
غُش يؼطاج واًَا َؼطً سسى انزاكشج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
.ارا ً الَرى انزهاب انً انزاكشج تهزا االَؼاص واًَا فقظ َؤخز انؼُىاٌ انًىخىد يا تٍُ االقىاط
LDS instruction loads 2bytes from memory to a 16-bit register, then loads the next 2bytes to DS
register.
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
00H 12201H
200 +
20H 12200H
12200H
Notice that in this example, DSold is required to reach the memory and bring the new values of
SI and DS.
Notice that:
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:
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 +
Best Regards
Dr. Zainab Alomari
Lecture 6: Addition and Subtraction Instructions
1- Addition Instruction
.destination انـٙجت فٛ يع بعضًٓا ٔخضٌ انُخdestination ٔانـsource اث انـٕٚعاص بجًع يحخٚقٕو ْزا االٚ
Example
ADD AX, BX ; (AX = AX + BX)
All status flags are affected by his instruction (CF, PF, ZF, SF, AF, OF)
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:
ADD CL, CH
Solution
CL = 0 0 0 0 1 1 1 1
CH = 0 0 0 1 1 1 1 1 +
0010 1110
CF=0
ZF=0
PF=1
SF=0
AF=1
OF=0
Examples
َالحظ فْ ٙزا انًثال اٌ انـٚ OFسأ٘ 1الٌ ُْانك carryيٍ انبج قبم االخٛش انٗ بج االشاسة ثى نى ٚكٍ ُْانك
carryيٍ بج االشاسةٔ ,بانخان ٙفُٓانك حانت .overflow
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
Examples:
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 اث انـٕٚعاص بطشح يحخٚقٕو ْزا االٚ
Example
SUB CL, BL ; (CL = CL - BL)
All status flags are affected by his instruction (CF, PF, ZF, SF, AF, OF)
Example
Write the instructions required to subtract 44H from 22H and put the result in CH.
Solution
HLT
Examples
5- Subtract with Borrow Instruction
SBB destination, source )borrow بُظش االعخباس (باعخباسْاCFًت انـٛخى انطشح يع اخز قٚ عاصٚ ْزا االٙف
Examples:
Examples
Example
Write a piece of code in Assembly language to compute: Z=X – Y, where:
Z is stored at 96200H.
Solution
94200H 9000H:4200H
96200H 9000H:6200H
The three numbers are stored in the data segment with the value of DS equal to 9000H.
MOV DS, AX
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 DS, AX
MOV SS, AX
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)
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:
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
IMUL AL
MOV CX, AX
HLT
2- Division Instruction
:ٌقوم هزا االٌعاص بالقسمت باسخخذام الصٍغ الخالٍت
NOTE:
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
Example
Solution
*-*-*-*-*-*-*
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.
حصم قفض انٗ يكاٌ اخشٚ ٙزِ فثانرانٛرى ذُفٛ انز٘ سٙعاص انرانٚؤشش انٗ االٚ ً٘ح انًؤشش انزٛش قٛٛعاص ترغٚقٕو ْزا االٚ
.داخم أ خاسج انثشَايج
1) Intrasegment Jump
In this type, the value of the new IP is directly given by the instruction. CS is not changed.
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
Example:
Examples:
.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):
BXnew= BXold
1- Conditional Jump
.رى انقفض انٗ انًٕقع انًطهٕبٚرى أ الًٛح سٍٛ ٔحسة انقًٛح عايم أ تد يعٛ ذعرًذ عهٗ قٙ إَاع انقفض انرْٙ
Note:
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
ANOTHER SOLUTION:
Loop Instructions:
1) LOOP instruction:
LOOP N1
LOOPE N1
LOOPZ N1
LOOPNE N1
LOOPNZ 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ارا اسدَا جعم بج يعٍٍ ٌساوي واحذ ) (setيع انًحافظت عهى باقً انبخاث بذوٌ
حغٍٍش ,كًا فً انًثال انخانً:
Example
Set the MSB and LSB of BX using Assembly language.
Examples
2- AND Instruction
AND destination, source
=0
=A
ٌقىو هزا االٌعاص بعًم Logic ANDبٍٍ كم بج يٍ انـ destinationيع انبج انزي ٌقابهه يٍ انـ sourceوٌخى خضٌ
انُخٍجت فً انـ .destinationيٍ انُظش انى انـ Truth Tableانخاصت ببىابت َ ANDالحظ بأَه:
بًعُى أَه ًٌكُُا االسخفادة يٍ اٌعاص ANDارا اسدَا جعم بج يعٍٍ ٌساوي صفش ) (resetيع انًحافظت عهى باقً انبخاث بذوٌ
حغٍٍش ,كًا فً انًثال انخانً:
Example
Reset the MSB and LSB of AX using Assembly language.
Examples
3- XOR Instruction
XOR destination, source
=A
=A
ٌقىو هزا االٌعاص بعًم Logic XORبٍٍ كم بج يٍ انـ destinationيع انبج انزي ٌقابهه يٍ انـ sourceوٌخى خضٌ
انُخٍجت فً انـ .destinationيٍ انُظش انى انـ Truth Tableانخاصت ببىابت َ XORالحظ بأَه:
بًعُى أَه ًٌكُُا االسخفادة يٍ اٌعاص XORارا اسدَا قهب بج يعٍٍ يع انًحافظت عهى باقً انبخاث بذوٌ حغٍٍش ,كًا فً انًثال
انخانً:
Example
Complement the MSB and LSB of DX using Assembly language.
Examples
4- NOT Instruction
NOT operand ; (this instruction takes the 1’s complement of the operand)
A A
Example
What is the value of AL after executing the following code:
ALnew = 25H
5- Negaitve Instruction
NEG operand ; (this instruction takes the 2’s complement of the operand)
:ً وه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):
and so on.
Example
Find the value of the ZF after executing the following instruction:
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
-------------------------
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.
Stack
SS:SP
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
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
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
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
------
------
POP DX
POP AX
Example:
Exchange between AX and BX using stack instructions.
Solution
Push AX
Push BX
POP AX
POP BX
HLT
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)
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
SAHF
HLT
Other instructions that allow changing a single bit in the FLAGS register without affecting other
bits:
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
.)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
= 0EFE H
Example:
Let AX=877FH , what is the new value of AX after executing the following instructions:
= 0H
: كًا فً االيثهح االذٍح,عًهٍح ذشفٍد انى انٍساس ذعادل ضشب انشقى تًقذاس ٌعرًذ عهى عذد تراخ انرضحٍف
.)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
= 11E0H
Solution
(11H = 17d)
= 0H
: كًا فً االيثهح االذٍح,عًهٍح ذشفٍد انى انًٍٍٍ ذعادل قسًح انشقى تًقذاس ٌعرًذ عهى عذد تراخ انرضحٍف
2) Arithmetic 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:
Solution
(10H = 16d)
= FFFF H
يا, ارا كاٌ يقرذاس انرشفٍد اكثش يٍ عذد انثراخCFَالحظ تاٌ جًٍع اٌعاصاخ انرشفٍد االستعح ذقٕو تٕضع صفش فً انـ
.CF ٌقٕو تٕضع تد االشاسج فً انـSAR عذا اٌعاص
Example:
Let AX=FC44H , what is the new value of AX after executing the following instruction:
SAR AX, CL
Solution
(12H = 18d)
= FFFF H
(In this example, Let AXold = 7FFFH AXnew = 0000H, CF=0, ZF=1)
Examples
2- Rotate Instructions
1) Rotate 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
=0086H
CF = 0, ZF = 0
Example
What is the new value of AL after executing the following instruction:
ROL AL, 8
Solution
ALnew = ALold
Solution
AXnew = AXold
2) Rotate 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
CF = 0
تاسرخذاو االٌعاصXchg تذٌٔ انحاجح السرخذاو اٌعاصAH يعAL َالحظ أَّ ًٌكٍ االسرفادج يٍ ْزا االٌعاص فً ذثذٌم
.)ROL Ax, 8( ٔ) أROR Ax, 8( :ًانران
CF
فشق ْزا االٌعاص عٍ ْٕ RORأَّ ٌقٕو تانرذٌٔش يعرثشا ً انـ CFيٍ ضًٍ انثراخ انرً سٍرى ذذٌٔشْا انى انًٍٍٍ ,تانرانً سٍرى
اسرخذاو قًٍرٓا انقذًٌح عهى آَا أل تد ٌرى ذذٌٔشِ ٔفً انُٓاٌح سرحرٕي عهى قًٍح اخش تد ذى ذذٌٔشِ.
CF
Example
Let AX=FF05H, CF=0, CL=3, what is the difference between the results of the following
instructions:
Solution
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
CALL BX ; IPnew= BX
(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
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.
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
IPnew = 12ABH
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:
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 ٍِ خاله استشخاع قٍَح
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.
SPnew = SP + 2 IPLow
SS: SP
2) RET in Intersegment CALL
Stack Segment
IPLow SS:[SP] SS: SP+4
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
Example
MOV CX, 3
N1: LOOP N1
Solution
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
Delay time = ∞
Example
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/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
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-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
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
N= 52.6 ≈ 53 d N = 35 H
Example
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.
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.
Example
CS:1ABCH PUSH CX
MOV CX, 400H
CS:IP CALL 1ABCH NXT: PUSH AX
POP AX
LOOP NXT
POP CX
RET
Solution
*_*_*_*_*_*_*_*
Best Regards
Dr. Zainab Alomari
Lecture 1: In/Out Instructions
To deal with input/output devices, 8086Mp programmer needs to know three things:
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.
Out Instruction:
This instruction transfers data from AL or AX to external I/O device.
Note: only AL or AX is used by the 8086Mp to send/receive data to/from I/O devices:
كًا في االيثهت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
Solution
IN AL, 61H
OR AL, 3
OUT 61H, AL
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خاص تٓا.
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.
ال انًقاطعح االٔنى -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.
ٌ ٔرنك ال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 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.
Q3_Ans: The first address is 00000H and the last address is 003FFH.
Best Regards
Dr. Zainab Alomari
Lecture 3: Interrupts (Part 2)
1- Software Interrupts:
This interrupt uses the instruction (INT n), where n is the interrupt number and it can be any
number between (0HFFH).
.0HFFH ٍِ ٌ أي سقn ) ٗحنُ٘ قٍَتINT n( ٕزا اىْ٘ع ٍِ اىَقاطعاث باٍناُ اىَسخخذً اسخذعائٔ باسخخذاً االٌعاص
ً٘ ٌق,)ً٘ سقٌ اىَقاطعت (مَا فً اىَثاه اىَشسn ) زٍث حَثوINT n( عْذٍا ٌقً٘ اىَعاىح بخْفٍز بشّاٍح ٍعٍِ ٌٗصو اىى اٌعاص
ُ ٗعَيٍت اىقفض ٕزٓ حخطيب اى٘ص٘ه اىى اىَنا, اىزي ٌسخ٘ي اٌعاصاث اىخذٍت اىخً حقذٍٖا ٕزٓ اىَقاطعتISRاىَعاىح باىقفض اىى اىـ
. زسب سقٌ اىَقاطعت مَا ششذ فً اىَساضشة اىسابقتIVT ٗاىخً ٌخٌ اىسص٘ه عيٍٖا ٍِ خذٗه اىـISRاىَخضُٗ فٍٔ ٕزا اىـ
CS ٍِ فاُ اىَعاىح بساخت اىى االزخفاظ باىقٌٍ اىقذٌَت ىنو, ISRٗىغشض اىع٘دة اىى اىبشّاٍح اىشئٍسً بعذ االّخٖاء ٍِ حْفٍز اىـ
CS,IP & ىنو ٍِ اىـPush ٌخٌ عَو. Flags ً مَا ٌقً٘ اٌضا ً باالزخفاظ بساىت األعال,)Call & (مَا ٌسصو فً اٌعاصIP
.6 ) بَقذاسSP( ٍَا ٌؤدي اىى إّقاص قٍَت اىَؤشش اىخاص باىسخاك, اىى اىسخاكFlags register
3 steps
Each ISR ends with (IRET) instruction. When IRET instruction is executed, the Mp performs
the following 3 steps:
ٌ ٍِ اىسخاك ٗاىخً ماُ قذ حCS, IP & Flags register ٍِ ) ٗاىزي ٌقً٘ باسخعادة قٌٍ موIRET( باٌعاصISR ٌْخًٖ مو
.ٓ ٗرىل ىغشض اىع٘د ة المَاه حْفٍزس اىبشّاٍح اىشئٍسً ٍِ اىَناُ اىزي ح٘قفْا عْذ, )INT n( خضّٖا عْذ حْفٍز اٌعاص
زٍث أُ اىفشق ٕ٘ اّْا فً اٌعاص اىَقاطعت ّقً٘ بخسذٌذ سقٌ اىَقاطعت,) Call(ٗ )INT( ّالزع ْٕا اىخشابٔ اىنبٍش بٍِ اٌعاصي
. ٍباششة فً االٌعاصCS&IP فٍخٌ اعطاء قٍَتCall أٍا فً اٌعاصIVT ٍِ اىـCS&IP ٌٗخٌ أخز اىـ
:االخابت
. باٌج ٍَا ٌ٘فش ٍ٘اقع ٍِ اىزامشة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
(D0D7).
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.
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).
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.
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 انـ:مالحظة
Name DB Value
DW
Note1: Variables are defined at the end of the source code (in com files) after RET instruction.
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 ?
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
يغ,ٍٍٛ انغاتقرٛقرٚرى رنك تاحذٖ انطشّٛ يٍ انزاكشج فٍٛ فٛش يؼٛ حانح انحاجح نهٕصٕل انٗ انؼُٕاٌ انز٘ ذى خضٌ يرغٙف
.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.
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
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
Store remainder
Yes No
Result=0?
End
org 100h
Example
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.
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
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 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
1 1 None
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
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.
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).
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.
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
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
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:
فش اٌىثٍشٛذرٚ .اٌثشاِح اٌّخرٍفحٚ ِاخٍٛذحًٍّ اٌّؼٚ ْا ٌخضِٙاسرخذاٚ ا تاٌّؼاٌداخٌٙرُ ذصٍٕغ سلائك اٌزاوشج ٌٍرُ ستط
تحْٛ ِىرٛ ٌّىٓ ِؼشفح حدُ سلالح اٌزاوشج ِٓ خالي االسلاَ اٌرً ذىٚ .تأحداَ ِخرٍفحٚ ًّق اٌؼِٛٓ سلالاخ اٌزاوشج فً س
طٛ أِا اٌشلُ اٌزي ٌسثك اٌم,ٍٓسٛاحذ تٍٓ اٌمٌٛلغ اٌّٛتح فً حدُ اٚالغ ِضشٌّٛ حٍث ٌرُ اػطاء ػذد ا,ٖا تاٌطشٌمح أػالٍٍٙػ
.اٌٙ ُتّثاتح أسٚ ع ِٓ اٌشلالاخٛٔ ً سلُ ٍِّض ٌىٛٙف
Example (1)
This memory device contains 2K locations, 11 address lines and 8 data outputs.
.)2N ٛ٘ الغٌّٛ ارا واْ ػذد اN يٚ ٌساaddressط اٌـٛالغ (ػذد خطٌّٛٔح ِٓ خالي ػذد إٛط اٌؼٌٛرُ ِؼشفح ػذد خط
.لغِٛ ً ِٓ ػذد اٌثراخ ٌىdataط اٌـٌٛرُ ِؼشفح ػذد خط
Example (2)
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)
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.
A0 O0
A1 O1
A2 O2
. .
N-Address Lines K-Data Lines
. .
(Input) . . (In/Out)
AN OK
A0 O0
A1 O1
A2 O2
. .
N-Address Lines K-Data Lines
. .
(Input) . . (Output)
AN OK
CS RE
:512Kbyte ّإِٙ ًوٍٓ حدُ وٍٛا ذُ تٕاؤ٘ا تاسرخذاَ تٙٔٔالحظ تأٚ ,8086Mpٔأذً اٌى ذصٍُّ اٌزاوشج اٌخاصح تاٌـ
ًٌٕٙرٚ 00000H لغٌٌّٛثذأ تاٚ )Even bank( ٚ) أLow bank( ٌسّىٚ خٍحٌٚٓ اٌضٚالغ راخ اٌؼٕاٌّٛي اٛ ٌحر:األول
. FFFFEH لغٌّٛتا
ًٌٕٙرٚ 00001H لغٌٌّٛثذأ تاٚ )Odd bank( ٚ) أHigh bank( ٌسّىٚ ٌٓ اٌفشدٌحٚالغ راخ اٌؼٕاٌّٛي اٛوالثاني ٌحر
.FFFFFH لغٌّٛتا
High Bank Low bank
تّا أْ حدُ وً تٍٛن ٘ 512Kbyte ٛفثاٌراًٌ ػذد خطٛط اٌؼٕٔٛح ٌىً ِٓ اٌثٍٛوٍٓ ٘ 91 ٛخظ٘ ,زٖ اٌخطٛط ذشتظ ػٍى
اٌخطٛط اٌخاصح تاٌـ ِٓ addressاٌـٚ 8086Mpاٌرً ً٘ ِٓ A1اٌى A19أِا A0فألٔٗ ٌحذد ً٘ اٌؼٕٛاْ فشدي أَ صٚخً
فٌ ٛٙشتظ ػٍى أشاسج اٌـٌٍ CSـ Low bankفارا واْ اٌؼٕٛاْ اٌمادَ ِٓ اٌّؼاٌح صٚخً فإْ A0سٍىٌ ْٛساٚي ٚ 0تاٌراًٌ سٍرُ
ذفؼًٍ اشاسج ٌٙ CSزا اٌثأه.
أِا ارا واْ اٌؼٕٛاْ اٌّطٍٛب فشدي فٕٙاٌه اشاسج ٌفؼٍٙا اٌّؼاٌح ٌالشاسج اٌى و ْٛاٌؼٕٛاْ فشدي ٔ .BHE ً٘ٚالحظ أٔٙا
ذصثح ذساٚي 0ارا واْ اٌؼٕٛاْ فشدي ٌٙٚزا ٌرُ ستطٙا تاٌـ CSاٌخاص تاٌـ.High bank
وً ٘زا اٌىالَ ٘ ٛفً حاي و ْٛاٌذاذا اٌّطٍٛب لشائرٙا أ ٚوراترٙا فً اٌزاوشج ً٘ تاٌد ٚاحذ فمظ ,وّا فً األِثٍح اٌراٌٍح:
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
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.
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
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
Example
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).
Design the hardware required to interface 8 switches to the 8086Mp at port number F301H.
Solution
Example
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
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
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:
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:
Best Regards
Dr. Zainab Alomari