Lecture 18 Directives
Lecture 18 Directives
Directives are included in the source code that contains assembly language instructions.
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 انـ:مالحظة
A) Variables:
Name DB Value
DW
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
1
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 11001b )غاس ذغأ٘ صفشٛ انثراخ يٍ جٓح انٙ ْزِ انحانح اٌ تاقٙ(َالحظ ف
4) To define a variable byte named K2, which has NO initial value, we write:
K2 DB ?
NOTE: 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
2
B) Arrays:
It is possible to define an array by giving the array name, length of each element (byte or
word) and elements values.
Examples
A DB 48h, 65h, 6ch, 6ch, 6fh, 0h (A[0]=48h, A[1]=65h, …)
B DB ‘Hello’, 0 (B[0]=48h, B[1]=65h, …)
Notice that in this example, array B is an exact copy of array 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
ٔتذٌٔ انحاجح نًؼشفح انًٕقغ انز٘ ذى خضَٓا,ح انثشَايجٚ َٓاٙفٓا فٚ ذى ذؼشٙى انرٛح نهٕصٕل انٗ انقَُٛا ايكاٚخالل انثشَايج نذ
.ٍٍٛ انغاتقرٛقرٚ ٔرنك تاحذٖ انطش,ّ يٍ انزاكشجٛف
Getting the Address of Variables and Arrays
Two ways can be used to access a variable address:
1) Using LEA instruction, for example:
LEA BX, var1 ; BX = the offset address of var1
2) Using OFFSET, for example:
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 a com file is loaded, the value of DS is set to the same value of CS. Therefore,
the data and code are all stored in the same segment in the memory.
يغ,ٍٍٛ انغاتقرٛقرٚرى رنك تاحذٖ انطشّٛ يٍ انزاكشج فٍٛ فٛش يؼٛ حانح انحاجح نهٕصٕل انٗ انؼُٕاٌ انز٘ ذى خضٌ يرغٙف
.16-bit ّ عجم طٕنٙجة خضَّ فٚ ٙ ٔتانران16-bit offset ٍكٌٕ ػثاسج ػّٛ عٛاالَرثاِ انٗ أٌ انؼُٕاٌ انز٘ عُحصم ػه
ش؟ٛرى خضٌ ْزا انًرغٚ عٕفSegment ٘ أٙ ف/س
ٍش كالًْا انٗ َفظ انًقطغ يٛشٚ ثٛ تحDS&CSًح كم يٍ انـٛذ قٛرى ذٕحٚ َّ فاcom fileم انـًٛرى ذحٚ ػُذيا:الجواب
. أ٘ يًُٓاٙش يخضَٔا ً فًٛكُُا اػرثاس انًرغٚ ٙ ٔتانران,انزاكشج
3
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