0% found this document useful (0 votes)
13 views

Lecture 18 Directives

Directives are included in assembly language source code and provide instructions to the compiler, unlike instructions which are converted to machine code. Common directives include ORG to define the starting address and END to indicate the end of the program. Variables and arrays are defined using directives like DB for a byte, DW for a word, and specifying the name, length, and value. Arrays can also be defined with multiple elements. The address of a variable can be obtained using LEA or OFFSET to then access or modify the variable's value in memory.
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)
13 views

Lecture 18 Directives

Directives are included in assembly language source code and provide instructions to the compiler, unlike instructions which are converted to machine code. Common directives include ORG to define the starting address and END to indicate the end of the program. Variables and arrays are defined using directives like DB for a byte, DW for a word, and specifying the name, length, and value. Arrays can also be defined with multiple elements. The address of a variable can be obtained using LEA or OFFSET to then access or modify the variable's value in memory.
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/ 4

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‫ انـ‬:‫مالحظة‬

Variables and Arrays


 The definition of a variable or an array is a directive (not an instruction).
 Variables and arrays are defined at the end of the source code (in com files) after RET instruction.

A) Variables:

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

Name DB Value
DW

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

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

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


MOV SI, 4
MOV AL, A[SI]

‫ ٔتذٌٔ انحاجح نًؼشفح انًٕقغ انز٘ ذى خضَٓا‬,‫ح انثشَايج‬ٚ‫ َٓا‬ٙ‫فٓا ف‬ٚ‫ ذى ذؼش‬ٙ‫ى انر‬ٛ‫ح نهٕصٕل انٗ انق‬َٛ‫ُا ايكا‬ٚ‫خالل انثشَايج نذ‬
.ٍٛ‫ٍ انغاتقر‬ٛ‫قر‬ٚ‫ ٔرنك تاحذٖ انطش‬,‫ّ يٍ انزاكشج‬ٛ‫ف‬
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

You might also like