Time Delay programs & Assembler directives
of 8086
1
8086 Microprocessor
Contents
• Writing Time Delay programs using 8086
• Assembler directives of 8086 microprocessor (for
assembler EMU8086)
• Program Example
Dheeraj Suri Assistant Professor NIT Delhi
Writing Time Delay Programs
2
8086 Microprocessor
Every instruction in the 8086 requires a definite number of
clock cycles for its execution. The amount of time for
execution of an instruction is obtained by multiplying the
number of clock cycles required for the execution the
instruction, with the clock period at which the 8086 is running.
The steps for writing a time delay program are as follows:
(i) Find the exact time delay (td) required for the given
application.
(ii) Select the instructions to be included in the time delay
program. While selecting the instructions and registers to
be used in the delay program, care must be taken that the
execution of these instructions does-not affect the main
program execution. That is, any memory location or
register used by the main program must not be altered by
time delay program. If a register used in the main program
is needed in the delay program, the content of that
register is pushed into a stack before executing the time
delay program. At the end of the execution of the time
program, its original value will be popped from the stack
and then control will be transferred to the main program.
Dheeraj Suri Assistant Professor NIT Delhi
Writing Time Delay Programs
3
8086 Microprocessor
(iii) Find the period of the clock at which the microprocessor is
running by taking the reciprocal of the 8086’s clock frequency.
T is the duration of one clock period of clock state.
(iv) Find the number of clock states required for execution of
each of the instructions in the time-delay program. Then find
the number of clock states (m) needed to execute the loop in
the delay program once, by adding the clock states required
for each instruction in the delay program.
(v) Find the number of times (i.e., count n) the loop in the
delay program has to be executed by dividing the required
time delay (td) by the time taken to execute the loop once,
which is m X T
Count (n) = td/ (m X T)
The time delay obtained using this method is sufficiently
accurate to be used in many problems. When more accurate
delays are required, the programmable timer IC 8253 or the
8254 can be used.
Dheeraj Suri Assistant Professor NIT Delhi
Writing Time Delay Programs
4
8086 Microprocessor
Example: Write a time delay program to generate a delay of
120ms in an 8086 – based system that runs on a 10Mhz
frequency clock.
Solution:
The time delay program is as follows:
In this program, the instructions DEC BX, NOP, and JNZ L1
form the loop as they are executed repeatedly until BX
becomes zero. Once BX becomes zero, the 8086 returns to the
main program.
Dheeraj Suri Assistant Professor NIT Delhi
Instruction T-states for execution
MOV BX, Count 4
L1: DEC BX 2
NOP 3
JNZ L1 16
RET 8
Writing Time Delay Programs
5
8086 Microprocessor
Example: Write a time delay program to generate a delay of
120ms in an 8086 – based system that runs on a 10Mhz
frequency clock.
Solution (continued):
Number of clock cycles for execution of the loop once
(m) = 2 + 3 + 16 = 21
Time required for the execution of loop once = m X T =
21 X 1/(10 X 10^6)
= 2.1 µs
Count = td/(m X T) = 120 X 10^-3 /(2.1 X 10^-6)
= 57143 = DF37h
By loading DF37h in BX, the time taken to execute the delay
program is approximately 120ms. The NOP included in the
delay program is to increase the execution time of the loop. To
get more delay, the number of NOP instructions in the delay
loop can be increased. The exact delay obtained using this
time delay subroutine can be calculated as shown in the next
slide.
Dheeraj Suri Assistant Professor NIT Delhi
Writing Time Delay Programs
6
8086 Microprocessor
Example: Write a time delay program to generate a delay of
120ms in an 8086 – based system that runs on a 10Mhz
frequency clock.
Solution (continued):
The MOV BX, Count
& RET instructions in the delay program are executed only
once. The JNZ instruction takes 16 T-states when the
condition is satisfied (i.e. Z = 0) and four T – states when the
condition is not satisfied, which occurs only once.
Exact delay = [4 x 0.1 + (2+3) x 57143 x 0.1 + 16 x 57142
x0.1 + 4 X 0.1 + 8 X 0.1 ] µs
= 0.4 + 28571.5 + 91427.2 + 0.4 + 0.8
= 120000.3 µs = 120.0003 ms
The error in the previous calculation is very less as the exact
delay is also very close to 120ms. When 16-bit count register
is used in the delay program, the maximum count value that
can be loaded in it is FFFFh. This may put a limitation on the
maximum time delay that can be generated using the above
delay subroutine.
Dheeraj Suri Assistant Professor NIT Delhi
Writing Time Delay Programs
7
8086 Microprocessor
Example: Write a delay program to create a time delay of
240ms. Assume that 5Mhz clock is used with 8086. Following
is given:
Solution: td = 240 X 10^-3 seconds
One T-state time period = 1/(5 X 10^-6) sec
= 0.2 µs
No. of T-states in one loop , m = 21
No. of times to run the loop, Count =
(240 X 10^-3)/ (0.2 X 10^-6 X 21) = 57143D
= DF37h
Dheeraj Suri Assistant Professor NIT Delhi
Instruction No. of T-states
MOV BX, Immediate 4
DEC Register 2
NOP 3
JNZ Label 16
RET 8
Writing Time Delay Programs
8
8086 Microprocessor
Example: Write a delay program to create a time delay of
240ms. Assume that 5Mhz clock is used with 8086. Following
is given:
Solution(continued):
The exact delay = (Generated Delay) + (4+8 –
12)*0.2µs = approximately the same !
Dheeraj Suri Assistant Professor NIT Delhi
;Description ;No of T-states
MOV BX, DF37h 4
NEXT DEC BX 2
NOP 3
JNZ NEXT 16
RET 8
Writing Time Delay Programs
9
8086 Microprocessor
Self Practice Problem: Write a delay program to create a time
delay of five minutes. Assume that a 10 Mhz clock is used with
8086. Given that
Dheeraj Suri Assistant Professor NIT Delhi
Instruction No. of T-states
MOV BX, Immediate 4
DEC Register 2
NOP 3
JNZ Label 16
RET 8
Assembler Directives
10
8086 Microprocessor
An assembler is a program that is used to convert an
assembly language program into an equivalent machine
language program. The assembler finds the address of
each label and substitutes the value of each constant
and variable in the assembly language program during
the assembly process, to generate the machine
language code. While performing these operations, the
assembler may find syntax errors. They are reported to
the programmer at the end of the assembly process. The
logical and other programming errors are not found by
the assembler. For completing these tasks the
assembler needs some commands from the programmer
– the required storage class for a particular constant or
a variable such as byte, word, or double word, the
logical name of the segments such as CODE, STACK, or
DATA, the type of procedures or routines such as FAR,
NEAR, PUBLIC, or EXTRN, the end of a segment etc.
Dheeraj Suri Assistant Professor NIT Delhi
Assembler Directives
11
8086 Microprocessor
These types of commands are given to the assembler
using a predefined alphabetical strings called Assembler
directives.
Assembler directives are directions for the assembler,
and not the instructions for the 8086.
The Assembler used for this course is EMU8086*
*Note: Different assemblers may have different directives.
Dheeraj Suri Assistant Professor NIT Delhi
Assembler Directives for variable and
Constant Definition
12
8086 Microprocessor
The Assembler directives for variable and constant definition
are as follows:
(i) DB, DW, DD, DQ, and DT: the directives DB (define byte),
DW(define word), DD(define double word), DQ (define quad
word), and DT (define ten bytes) are used to reserve one byte,
one word (i.e. 2 bytes), one double word(i.e. 2 words), one
quad word(i.e. 4 words) and ten bytes in memory,
respectively for storing constants, variables, or strings.
Dheeraj Suri Assistant Professor NIT Delhi
Assembler Directives for variable and
Constant Definition
13
8086 Microprocessor
Example:
Dheeraj Suri Assistant Professor NIT Delhi
DATA 1 DB 20h ; Reserve one byte for storing
; DATA1 and assign the value 20h
; to it.
ARRAY DB 10h, 20h,
30h
; Reserve three bytes for storing
ARRAY1 and initialize it with the
values
; 10h, 20h and 30h
CITY DB “NARELA” ;Store the ASCII code of the
characters
; specified within double quotes in
the
; array or a list named CITY
DATA2 DW 1020h ; Reserve one word for storing
;DATA2 and Assign the value 1020
;to it.
Assembler Directives for variable and
Constant Definition
14
8086 Microprocessor
The directive DUP (duplicate) is used to reserve a series of
bytes, words, double words, or ten bytes and is used with DB,
DW, DD and DT, respectively. The reserved area can be either
filled with a specific value or left uninitialized.
Example:
Dheeraj Suri Assistant Professor NIT Delhi
Array DB 20 DUP(0) ;Reserve 20 bytes in the memory
; for the array named ARRAY and
; initialize all the elements of the
; array to 0 (due to presence of 0
; within the bracket near the DUP
; directive
ARRAY1 DB 25 DUP (?) ; Reserve 25 bytes in the memory
; for the array named ARRAY1 and
; keep all the elements of the array
; uninitialized (due to the question
; mark present within the bracket near
the DUP directive)
ARRAY2 DB 50 DUP
(64h)
; Reserves 50 bytes in the memory
; for the array named ARRAY2 and
; initializes all the elements of the
; array to 64h
Assembler Directives for variable and
Constant Definition
15
8086 Microprocessor
(ii) EQU: The directive EQU(equivalent) is used to assign a
value to a data name.
Example:
Dheeraj Suri Assistant Professor NIT Delhi
NUMBER EQU 50h ; Assign the value 50h to NUMBER.
NAME EQU “RAMESH” ; Assign the string “RAMESH” to NAME.
Assembler Directives Related to
Code(Program) Location
16
8086 Microprocessor
The Assembler directives related to Code location:
(i) ORG: The ORG (origin) directive directs the assembler to
start the memory allocation for a particular segment (data,
code, or stack) form the declared offset address in the ORG
statement. While starting the assembly process for a
memory segment, the assembler initializes a location
counter (LC) to keep track of the allotted offset addresses
for the segment. When the ORG directive is not mentioned ,
LC is initialized with the offset address 0000h. When the
ORG directive is mentioned at the beginning of the
statement, LC is initialized with the offset address specified
in the ORG directive.
Example:
ORG 100h
When this directive is placed at the beginning of the code
segment, the location counter is initialized with 0100h and the
first instruction is stored from the offset address 0100h within
the code segment. If it is placed in the data segment, the next
data storage starts from the offset address 0100h within the
data segment.
Dheeraj Suri Assistant Professor NIT Delhi
Assembler Directives Related to
Code(Program) Location
17
8086 Microprocessor
The Assembler directives related to Code location:
(ii) EVEN: The EVEN directive updates the location
counter to next even address, if the current location
counter content is not an even number.
Example:
ARRAY2 DW 20 DUP (0)
These statements in a segment declare an array
named ARRAY2 having 20 words, starting at an even
address. The advantage of storing an array of words
starting at an even address is that the 8086 takes just
one memory read/write cycle to read/write the entire
word, if the word is stored starting at an even address.
Otherwise, the 8086 takes two memory read/write
cycles to read/write to read/write the word.
Example: on next slide ..
Dheeraj Suri Assistant Professor NIT Delhi
Assembler Directives Related to
Code(Program) Location
18
8086 Microprocessor
The Assembler directives related to Code location:
Example:
Here the procedure RESULT, which is of type NEAR, is
stored starting at an even address in the code segment.
The ENDP directive indicates the end of the RESULT
procedure.
Dheeraj Suri Assistant Professor NIT Delhi
EVEN
RESULT PROC NEAR
….. ; Instructions in the
;RESULT Procedure
RESULT ENDP
Assembler Directives Related to
Code(Program) Location
19
8086 Microprocessor
The Assembler directives related to Code location:
(iii) LENGTH: This directive is used to determine the
length of an array or string in bytes.
Example:
MOV CX, LENGTH ARRAY
CX is loaded with the number of bytes in the ARRAY.
(iv) OFFSET: This operator is used to determine the
offset of a data item in a segment containing it.
Example:
MOV BX, OFFSET TABLE
If the data item named TABLE is present in the data
segment, this statement places the offset address of
TABLE, in the BX register.
Dheeraj Suri Assistant Professor NIT Delhi
Assembler Directives Related to
Code(Program) Location
20
8086 Microprocessor
The Assembler directives related to Code location:
(v) LABEL: The LABEL directive is used to assign a name
to the current value in the location counter. It is used to
specify the destination of the branch-related
instructions such as jump and call. When LABEL is used
to specify the destination, it is necessary to specify
whether it is NEAR or FAR. When the destination is in
the same segment, the label is specified as NEAR and
when the destination is in another segment, it is
specified as FAR.
Example:
Dheeraj Suri Assistant Professor NIT Delhi
REPEAT LABEL NEAR
CALCULATE LABEL FAR
Assembler Directives Related to
Code(Program) Location
21
8086 Microprocessor
The Assembler directives related to Code location:
LABEL can also be used to specify a data item. When it
is used to specify a data item, the type of data item
must be specified. The data may have the type byte or
word.
Example:
A stack segment having 100 words of data is defined
using the following statements:
The second statement reserves 100 words in the stack segment and
fills them with 0. The third statement assigns the name STACK_TOP to
the location present just after the hundredth word. The offset address
of this label can then be assigned to the stack pointer in the code
segment using the following statement:
MOV SP, OFFSET STACK_TOP
Dheeraj Suri Assistant Professor NIT Delhi
STACK SEGMENT
DW 100 DUP (0) ; Reserve 100 words for
; Stack
STACK_TOP LABEL WORD
STACK ENDS
Assembler Directives for Segment
Declaration
22
8086 Microprocessor
The Assembler directives for segment declaration
(i) SEGMENT and ENDS: The SEGMENT and ENDS
directives indicate the start and end of a segment,
respectively. In some cases, the segment may be
assigned a type such as PUBLIC (i.e. , it can be used
by other modules of the program while linking) or
GLOBAL (i.e. it can be accessed by any other
module). Example:
This example indicates the declaration of a code
segment named CODE 1.
Dheeraj Suri Assistant Professor NIT Delhi
CODE 1 SEGMENT
…… ; Instructions of CODE 1
segment
CODE 1 ENDS
Assembler Directives for Segment
Declaration
23
8086 Microprocessor
The Assembler directives for segment declaration
(ii) ASSUME: The ASSUME directive is used to inform the
assembler, the name of the logical segments to be
assumed for different segments used in the program.
This statement informs the assembler that the segment
address where the logical segments CODE1 and DATA1
are loaded in memory during execution is to be stored in
CS and DS registers, respectively.
Dheeraj Suri Assistant Professor NIT Delhi
ASSUME CS : CODE 1, DS: DATA1
Assembler Directives for Segment
Declaration
24
8086 Microprocessor
The Assembler directives for segment declaration
(iii) GROUP: This directive is used to form a logical
group of segments with a similar purpose. The
Assembler passes information to the linker/loader to
form the code, such that the group declared segments
or operands lie within a 64 Kb memory segment. All
such segments can be addressed using the same
segment address. Example:
This statement directs the loader/linker to prepare an
executable file (.exe) such that the CODE1, DATA1, and
STACK1 segments lie within a 64KB memory segment
that is named PROGRAM1.
Dheeraj Suri Assistant Professor NIT Delhi
PROGRAM1 GROUP CODE1, DATA1, STACK1
Assembler Directives for Segment
Declaration
25
8086 Microprocessor
The Assembler directives for segment declaration
(iv) SEG: This segment operator is used to decide the
segment address of the label, variable, or procedure and
substitute the segment address in place of the SEG
label.
Example:
Dheeraj Suri Assistant Professor NIT Delhi
MOV AX, SEG ARRAY1 ; Load the segment address in
which ARRAY1 is present, in AX
MOV DS, AX ; Move the contents of AX to DS.
Assembler Directives for declaring
procedures
26
8086 Microprocessor
The Assembler directives for declaring procedures:
(i) PROC : The PROC directive indicates the start of a named
procedure. The NEAR and FAR directive specify the type of
the procedure: Example:
This statement indicates the beginning of a procedure named
SQUARE_ROOT, which is to be called by a program located in
the same segment. The FAR directive is used for procedures to
be called by the programs present in code segments other
than the one in which this procedure is present. For example,
SALARY PROC FAR indicates the beginning of a FAR type
procedure named SALARY.
Dheeraj Suri Assistant Professor NIT Delhi
SQUARE_ROOT PROC NEAR
Assembler Directives for declaring
procedures
27
8086 Microprocessor
The Assembler directives for declaring procedures:
(ii) ENDP: The ENDP directive is used to indicate the end of a
procedure. To mark the end of a particular procedure, the
name of the procedure may appear as prefix with the directive
ENDP. Example:
Dheeraj Suri Assistant Professor NIT Delhi
SALARY PROC NEAR
…… ; Code of SALARY
;Procedure
SALARY ENDP
Assembler Directives for declaring
procedures
28
8086 Microprocessor
The Assembler directives for declaring procedures:
(iii) EXTRN and PUBLIC: The directive EXTRN (external)
informs the assembler that the procedures, label/labels, and
names declared after this directive has/have already been
defined in some other segments and in the segments where
they actually appear, they must be declared in public, using
the PUBLIC directive. Example:
Dheeraj Suri Assistant Professor NIT Delhi
MODULE1 SEGMENT
PUBLIC SQURE_ROOT
SQUARE_ROOT PROC FAR
…. ; CODE OF SQUARE_ROOT
PROCEDURE
SQUARE_ROOT ENDP
MODULE1 ENDS
; Code continued on next
; slide
Assembler Directives for declaring
procedures
29
8086 Microprocessor
The Assembler directives for declaring procedures:
(iii) EXTRN and PUBLIC (continued):
NOTE: If one wants to call the procedure named SQUARE_ROOT appearing in MODULE1
from MODULE2, it must be declared using the statement PUBLIC SQUARE_ROOT in MODULE1
and it must be declared external using the statement EXTRN SQUARE_ROOT in MODULE2. If a
jump or a call address is external, it must be represented as NEAR or FAR. If data are defined
as external, their size must be represented as BYTE, WORD, or DWORD.
Dheeraj Suri Assistant Professor NIT Delhi
MODULE2 SEGMENT
EXTRN SQUARE_ROOT FAR
…… ; CODE OF MODULE2
CALL SQUARE_ROOT
……
MODULE 2 ENDS
Assembler Directives for declaring
procedures
30
8086 Microprocessor
The Assembler directives for declaring procedures:
(iii) EXTRN and PUBLIC (continued):
NOTE: If one wants to call the procedure named SQUARE_ROOT appearing in MODULE1
from MODULE2, it must be declared using the statement PUBLIC SQUARE_ROOT in MODULE1
and it must be declared external using the statement EXTRN SQUARE_ROOT in MODULE2. If a
jump or a call address is external, it must be represented as NEAR or FAR. If data are defined
as external, their size must be represented as BYTE, WORD, or DWORD.
Dheeraj Suri Assistant Professor NIT Delhi
MODULE2 SEGMENT
EXTRN SQUARE_ROOT FAR
…… ; CODE OF MODULE2
CALL SQUARE_ROOT
……
MODULE 2 ENDS
Other Assembler Directives
31
8086 Microprocessor
Dheeraj Suri Assistant Professor NIT Delhi
PTR The PTR(pointer) operator is used to declare the
type of label, variable, or memory operand.
Examples: INC BYTE PTR[SI]
;Increment the byte contents of the memory
location addressed by SI
INC WORD PTR [BX]
;Increment the word contents of the memory
location addressed by BX
GLOBAL The labels, variables, constants, or procedures
declared GLOBAL may be used by other modules
of the program.
Example: GLOBAL DATA1, DATA2, ARRAY1
; above statement declares the variables DATA1,
; DATA2, and ARRAY1 as GLOBAL variables
LOCAL The label, variables, constants, or procedures
declared LOCAL in a module are to be used only
by that particular module.
Example: LOCAL DATA1, DATA2, ARRAY1, A1, A2
Other Assembler Directives
32
8086 Microprocessor
Dheeraj Suri Assistant Professor NIT Delhi
NAME The NAME directive is used to assign a name to
an assembly language program module. The
module may now be referred to by its declared
name. The names, if selected properly, may
indicate the function of the different modules,
and hence help in good documentation.
SHORT The SHORT operator indicates to the assembler
the only one byte is required to code the
displacement for a jump (i.e. the displacement is
within -128 to +127 bytes from the address of
the byte present next to the JMP instruction)
TYPE The TYPE operator directs the assembler to
decide the data type of the specified label and
replaces the TYPE label with the decided data
type. For the word type variable, the data type is
2. For the double word type, its 4, and for the
byte type its 1.
MACRO and ENDM
33
8086 Microprocessor
Dheeraj Suri Assistant Professor NIT Delhi
; Defining a MACRO
CALCULATE MACRO
MOV AX, [BX]
ADD AX, [BX + 2]
MOV [SI], AX
ENDM
; CALCULATE is the macro name and the macro is used to add two
successive data in the memory, whose offset address is present in
BX and the result is stored in the memory at the offset address in
SI.
Suppose a number of instructions occur repeatedly in the
main program, the program listing becomes lengthy. In such
a situation, a macro definition, i.e. a label, is assigned with
the repeatedly appearing string of instructions. The process
of assigning a label or macro name to the repeatedly
appearing string of instructions is called macro definition.
The macro name is then used throughout the main program
to refer to that string of instructions.
Passing parameters to a MACRO
34
8086 Microprocessor
Dheeraj Suri Assistant Professor NIT Delhi
CALCULATE MACRO OPERAND, RESULT
MOV BX, OFFSET OPERAND
MOV AX, [BX]
ADD AX, [BX + 2]
MOV SI, OFFSET RESULT
MOV [SI], AX
ENDM
Using parameters in macro definition, the programmer
specifies the parameters of the macro that are likely to be
changed each time the macro is called. The macro given
before (CALCULATE) can be modified to calculate the result
for the different sets of data and store it in a different
memory locations as follows:
Example Programs (using Directives)
35
8086 Microprocessor
Dheeraj Suri Assistant Professor NIT Delhi
1. Program to find the average of 10 byte-type data stored in
an array in data segment.
ASSUME CS: CODE1, DS: DATA1
DATA1 SEGMENT ;data segment
; starts
ARRAY DB 12h, 23h, 44h, 56h,
0ABh, 73h, 44h, 0ABh, 0EEh,
0Ah
; 10 bytes are
; stored
COUNT EQU 10 ; Count is the
; number of bytes
; in the array
AVERAGE DB 01 DUP(0) ;Reserve one byte
; to store the
result
DATA1 ENDS ; data segment
; ends
Example Programs (using Directives)
36
8086 Microprocessor
Dheeraj Suri Assistant Professor NIT Delhi
1. Program to find the average of 10 byte-type data stored in
an array in data segment (continued).
CODE1 SEGMENT ; Code segment
; starts
START: MOV AX, DATA1 ; Segment address
of DATA1 is moved
to AX
MOV DS, AX ; MOV AX contents
to DS
MOV SI, OFFSET ARRAY ; Move offset
; address of
ARRAY to SI
XOR AX, AX ; Clear AX and
Carry
; Flag
MOV BX, 0000h ; Clear BX
MOV CX, COUNT ; Move COUNT to
CX
Example Programs (using Directives)
37
8086 Microprocessor
Dheeraj Suri Assistant Professor NIT Delhi
1. Program to find the average of 10 byte-type data stored in
an array in data segment (continued).
NEXT: MOV BL, [SI] ; Move one byte
; from array into
BL
ADD AX, BX ; Add AX and BX
INC SI ; Increment SI to
point to next byte
LOOP NEXT ; Repeat Loop
; NEXT CX times
MOV DH, COUNT ; MOV Count to DH
DIV DH ;Divide AX by CH
MOV AVERAGE, AL ; Store AL contents
; in AVERAGE
CODE1 ENDS ;Code Segment
ends
Self Exercise Program (using Directives)
38
8086 Microprocessor
Dheeraj Suri Assistant Professor NIT Delhi
2. Write a Program to find the parity of a given word-type
data. If the parity is even, store 00h in BL register, and if the
parity is odd, store 01h in BL register.

Time delay programs and assembler directives 8086

  • 1.
    Time Delay programs& Assembler directives of 8086 1 8086 Microprocessor Contents • Writing Time Delay programs using 8086 • Assembler directives of 8086 microprocessor (for assembler EMU8086) • Program Example Dheeraj Suri Assistant Professor NIT Delhi
  • 2.
    Writing Time DelayPrograms 2 8086 Microprocessor Every instruction in the 8086 requires a definite number of clock cycles for its execution. The amount of time for execution of an instruction is obtained by multiplying the number of clock cycles required for the execution the instruction, with the clock period at which the 8086 is running. The steps for writing a time delay program are as follows: (i) Find the exact time delay (td) required for the given application. (ii) Select the instructions to be included in the time delay program. While selecting the instructions and registers to be used in the delay program, care must be taken that the execution of these instructions does-not affect the main program execution. That is, any memory location or register used by the main program must not be altered by time delay program. If a register used in the main program is needed in the delay program, the content of that register is pushed into a stack before executing the time delay program. At the end of the execution of the time program, its original value will be popped from the stack and then control will be transferred to the main program. Dheeraj Suri Assistant Professor NIT Delhi
  • 3.
    Writing Time DelayPrograms 3 8086 Microprocessor (iii) Find the period of the clock at which the microprocessor is running by taking the reciprocal of the 8086’s clock frequency. T is the duration of one clock period of clock state. (iv) Find the number of clock states required for execution of each of the instructions in the time-delay program. Then find the number of clock states (m) needed to execute the loop in the delay program once, by adding the clock states required for each instruction in the delay program. (v) Find the number of times (i.e., count n) the loop in the delay program has to be executed by dividing the required time delay (td) by the time taken to execute the loop once, which is m X T Count (n) = td/ (m X T) The time delay obtained using this method is sufficiently accurate to be used in many problems. When more accurate delays are required, the programmable timer IC 8253 or the 8254 can be used. Dheeraj Suri Assistant Professor NIT Delhi
  • 4.
    Writing Time DelayPrograms 4 8086 Microprocessor Example: Write a time delay program to generate a delay of 120ms in an 8086 – based system that runs on a 10Mhz frequency clock. Solution: The time delay program is as follows: In this program, the instructions DEC BX, NOP, and JNZ L1 form the loop as they are executed repeatedly until BX becomes zero. Once BX becomes zero, the 8086 returns to the main program. Dheeraj Suri Assistant Professor NIT Delhi Instruction T-states for execution MOV BX, Count 4 L1: DEC BX 2 NOP 3 JNZ L1 16 RET 8
  • 5.
    Writing Time DelayPrograms 5 8086 Microprocessor Example: Write a time delay program to generate a delay of 120ms in an 8086 – based system that runs on a 10Mhz frequency clock. Solution (continued): Number of clock cycles for execution of the loop once (m) = 2 + 3 + 16 = 21 Time required for the execution of loop once = m X T = 21 X 1/(10 X 10^6) = 2.1 µs Count = td/(m X T) = 120 X 10^-3 /(2.1 X 10^-6) = 57143 = DF37h By loading DF37h in BX, the time taken to execute the delay program is approximately 120ms. The NOP included in the delay program is to increase the execution time of the loop. To get more delay, the number of NOP instructions in the delay loop can be increased. The exact delay obtained using this time delay subroutine can be calculated as shown in the next slide. Dheeraj Suri Assistant Professor NIT Delhi
  • 6.
    Writing Time DelayPrograms 6 8086 Microprocessor Example: Write a time delay program to generate a delay of 120ms in an 8086 – based system that runs on a 10Mhz frequency clock. Solution (continued): The MOV BX, Count & RET instructions in the delay program are executed only once. The JNZ instruction takes 16 T-states when the condition is satisfied (i.e. Z = 0) and four T – states when the condition is not satisfied, which occurs only once. Exact delay = [4 x 0.1 + (2+3) x 57143 x 0.1 + 16 x 57142 x0.1 + 4 X 0.1 + 8 X 0.1 ] µs = 0.4 + 28571.5 + 91427.2 + 0.4 + 0.8 = 120000.3 µs = 120.0003 ms The error in the previous calculation is very less as the exact delay is also very close to 120ms. When 16-bit count register is used in the delay program, the maximum count value that can be loaded in it is FFFFh. This may put a limitation on the maximum time delay that can be generated using the above delay subroutine. Dheeraj Suri Assistant Professor NIT Delhi
  • 7.
    Writing Time DelayPrograms 7 8086 Microprocessor Example: Write a delay program to create a time delay of 240ms. Assume that 5Mhz clock is used with 8086. Following is given: Solution: td = 240 X 10^-3 seconds One T-state time period = 1/(5 X 10^-6) sec = 0.2 µs No. of T-states in one loop , m = 21 No. of times to run the loop, Count = (240 X 10^-3)/ (0.2 X 10^-6 X 21) = 57143D = DF37h Dheeraj Suri Assistant Professor NIT Delhi Instruction No. of T-states MOV BX, Immediate 4 DEC Register 2 NOP 3 JNZ Label 16 RET 8
  • 8.
    Writing Time DelayPrograms 8 8086 Microprocessor Example: Write a delay program to create a time delay of 240ms. Assume that 5Mhz clock is used with 8086. Following is given: Solution(continued): The exact delay = (Generated Delay) + (4+8 – 12)*0.2µs = approximately the same ! Dheeraj Suri Assistant Professor NIT Delhi ;Description ;No of T-states MOV BX, DF37h 4 NEXT DEC BX 2 NOP 3 JNZ NEXT 16 RET 8
  • 9.
    Writing Time DelayPrograms 9 8086 Microprocessor Self Practice Problem: Write a delay program to create a time delay of five minutes. Assume that a 10 Mhz clock is used with 8086. Given that Dheeraj Suri Assistant Professor NIT Delhi Instruction No. of T-states MOV BX, Immediate 4 DEC Register 2 NOP 3 JNZ Label 16 RET 8
  • 10.
    Assembler Directives 10 8086 Microprocessor Anassembler is a program that is used to convert an assembly language program into an equivalent machine language program. The assembler finds the address of each label and substitutes the value of each constant and variable in the assembly language program during the assembly process, to generate the machine language code. While performing these operations, the assembler may find syntax errors. They are reported to the programmer at the end of the assembly process. The logical and other programming errors are not found by the assembler. For completing these tasks the assembler needs some commands from the programmer – the required storage class for a particular constant or a variable such as byte, word, or double word, the logical name of the segments such as CODE, STACK, or DATA, the type of procedures or routines such as FAR, NEAR, PUBLIC, or EXTRN, the end of a segment etc. Dheeraj Suri Assistant Professor NIT Delhi
  • 11.
    Assembler Directives 11 8086 Microprocessor Thesetypes of commands are given to the assembler using a predefined alphabetical strings called Assembler directives. Assembler directives are directions for the assembler, and not the instructions for the 8086. The Assembler used for this course is EMU8086* *Note: Different assemblers may have different directives. Dheeraj Suri Assistant Professor NIT Delhi
  • 12.
    Assembler Directives forvariable and Constant Definition 12 8086 Microprocessor The Assembler directives for variable and constant definition are as follows: (i) DB, DW, DD, DQ, and DT: the directives DB (define byte), DW(define word), DD(define double word), DQ (define quad word), and DT (define ten bytes) are used to reserve one byte, one word (i.e. 2 bytes), one double word(i.e. 2 words), one quad word(i.e. 4 words) and ten bytes in memory, respectively for storing constants, variables, or strings. Dheeraj Suri Assistant Professor NIT Delhi
  • 13.
    Assembler Directives forvariable and Constant Definition 13 8086 Microprocessor Example: Dheeraj Suri Assistant Professor NIT Delhi DATA 1 DB 20h ; Reserve one byte for storing ; DATA1 and assign the value 20h ; to it. ARRAY DB 10h, 20h, 30h ; Reserve three bytes for storing ARRAY1 and initialize it with the values ; 10h, 20h and 30h CITY DB “NARELA” ;Store the ASCII code of the characters ; specified within double quotes in the ; array or a list named CITY DATA2 DW 1020h ; Reserve one word for storing ;DATA2 and Assign the value 1020 ;to it.
  • 14.
    Assembler Directives forvariable and Constant Definition 14 8086 Microprocessor The directive DUP (duplicate) is used to reserve a series of bytes, words, double words, or ten bytes and is used with DB, DW, DD and DT, respectively. The reserved area can be either filled with a specific value or left uninitialized. Example: Dheeraj Suri Assistant Professor NIT Delhi Array DB 20 DUP(0) ;Reserve 20 bytes in the memory ; for the array named ARRAY and ; initialize all the elements of the ; array to 0 (due to presence of 0 ; within the bracket near the DUP ; directive ARRAY1 DB 25 DUP (?) ; Reserve 25 bytes in the memory ; for the array named ARRAY1 and ; keep all the elements of the array ; uninitialized (due to the question ; mark present within the bracket near the DUP directive) ARRAY2 DB 50 DUP (64h) ; Reserves 50 bytes in the memory ; for the array named ARRAY2 and ; initializes all the elements of the ; array to 64h
  • 15.
    Assembler Directives forvariable and Constant Definition 15 8086 Microprocessor (ii) EQU: The directive EQU(equivalent) is used to assign a value to a data name. Example: Dheeraj Suri Assistant Professor NIT Delhi NUMBER EQU 50h ; Assign the value 50h to NUMBER. NAME EQU “RAMESH” ; Assign the string “RAMESH” to NAME.
  • 16.
    Assembler Directives Relatedto Code(Program) Location 16 8086 Microprocessor The Assembler directives related to Code location: (i) ORG: The ORG (origin) directive directs the assembler to start the memory allocation for a particular segment (data, code, or stack) form the declared offset address in the ORG statement. While starting the assembly process for a memory segment, the assembler initializes a location counter (LC) to keep track of the allotted offset addresses for the segment. When the ORG directive is not mentioned , LC is initialized with the offset address 0000h. When the ORG directive is mentioned at the beginning of the statement, LC is initialized with the offset address specified in the ORG directive. Example: ORG 100h When this directive is placed at the beginning of the code segment, the location counter is initialized with 0100h and the first instruction is stored from the offset address 0100h within the code segment. If it is placed in the data segment, the next data storage starts from the offset address 0100h within the data segment. Dheeraj Suri Assistant Professor NIT Delhi
  • 17.
    Assembler Directives Relatedto Code(Program) Location 17 8086 Microprocessor The Assembler directives related to Code location: (ii) EVEN: The EVEN directive updates the location counter to next even address, if the current location counter content is not an even number. Example: ARRAY2 DW 20 DUP (0) These statements in a segment declare an array named ARRAY2 having 20 words, starting at an even address. The advantage of storing an array of words starting at an even address is that the 8086 takes just one memory read/write cycle to read/write the entire word, if the word is stored starting at an even address. Otherwise, the 8086 takes two memory read/write cycles to read/write to read/write the word. Example: on next slide .. Dheeraj Suri Assistant Professor NIT Delhi
  • 18.
    Assembler Directives Relatedto Code(Program) Location 18 8086 Microprocessor The Assembler directives related to Code location: Example: Here the procedure RESULT, which is of type NEAR, is stored starting at an even address in the code segment. The ENDP directive indicates the end of the RESULT procedure. Dheeraj Suri Assistant Professor NIT Delhi EVEN RESULT PROC NEAR ….. ; Instructions in the ;RESULT Procedure RESULT ENDP
  • 19.
    Assembler Directives Relatedto Code(Program) Location 19 8086 Microprocessor The Assembler directives related to Code location: (iii) LENGTH: This directive is used to determine the length of an array or string in bytes. Example: MOV CX, LENGTH ARRAY CX is loaded with the number of bytes in the ARRAY. (iv) OFFSET: This operator is used to determine the offset of a data item in a segment containing it. Example: MOV BX, OFFSET TABLE If the data item named TABLE is present in the data segment, this statement places the offset address of TABLE, in the BX register. Dheeraj Suri Assistant Professor NIT Delhi
  • 20.
    Assembler Directives Relatedto Code(Program) Location 20 8086 Microprocessor The Assembler directives related to Code location: (v) LABEL: The LABEL directive is used to assign a name to the current value in the location counter. It is used to specify the destination of the branch-related instructions such as jump and call. When LABEL is used to specify the destination, it is necessary to specify whether it is NEAR or FAR. When the destination is in the same segment, the label is specified as NEAR and when the destination is in another segment, it is specified as FAR. Example: Dheeraj Suri Assistant Professor NIT Delhi REPEAT LABEL NEAR CALCULATE LABEL FAR
  • 21.
    Assembler Directives Relatedto Code(Program) Location 21 8086 Microprocessor The Assembler directives related to Code location: LABEL can also be used to specify a data item. When it is used to specify a data item, the type of data item must be specified. The data may have the type byte or word. Example: A stack segment having 100 words of data is defined using the following statements: The second statement reserves 100 words in the stack segment and fills them with 0. The third statement assigns the name STACK_TOP to the location present just after the hundredth word. The offset address of this label can then be assigned to the stack pointer in the code segment using the following statement: MOV SP, OFFSET STACK_TOP Dheeraj Suri Assistant Professor NIT Delhi STACK SEGMENT DW 100 DUP (0) ; Reserve 100 words for ; Stack STACK_TOP LABEL WORD STACK ENDS
  • 22.
    Assembler Directives forSegment Declaration 22 8086 Microprocessor The Assembler directives for segment declaration (i) SEGMENT and ENDS: The SEGMENT and ENDS directives indicate the start and end of a segment, respectively. In some cases, the segment may be assigned a type such as PUBLIC (i.e. , it can be used by other modules of the program while linking) or GLOBAL (i.e. it can be accessed by any other module). Example: This example indicates the declaration of a code segment named CODE 1. Dheeraj Suri Assistant Professor NIT Delhi CODE 1 SEGMENT …… ; Instructions of CODE 1 segment CODE 1 ENDS
  • 23.
    Assembler Directives forSegment Declaration 23 8086 Microprocessor The Assembler directives for segment declaration (ii) ASSUME: The ASSUME directive is used to inform the assembler, the name of the logical segments to be assumed for different segments used in the program. This statement informs the assembler that the segment address where the logical segments CODE1 and DATA1 are loaded in memory during execution is to be stored in CS and DS registers, respectively. Dheeraj Suri Assistant Professor NIT Delhi ASSUME CS : CODE 1, DS: DATA1
  • 24.
    Assembler Directives forSegment Declaration 24 8086 Microprocessor The Assembler directives for segment declaration (iii) GROUP: This directive is used to form a logical group of segments with a similar purpose. The Assembler passes information to the linker/loader to form the code, such that the group declared segments or operands lie within a 64 Kb memory segment. All such segments can be addressed using the same segment address. Example: This statement directs the loader/linker to prepare an executable file (.exe) such that the CODE1, DATA1, and STACK1 segments lie within a 64KB memory segment that is named PROGRAM1. Dheeraj Suri Assistant Professor NIT Delhi PROGRAM1 GROUP CODE1, DATA1, STACK1
  • 25.
    Assembler Directives forSegment Declaration 25 8086 Microprocessor The Assembler directives for segment declaration (iv) SEG: This segment operator is used to decide the segment address of the label, variable, or procedure and substitute the segment address in place of the SEG label. Example: Dheeraj Suri Assistant Professor NIT Delhi MOV AX, SEG ARRAY1 ; Load the segment address in which ARRAY1 is present, in AX MOV DS, AX ; Move the contents of AX to DS.
  • 26.
    Assembler Directives fordeclaring procedures 26 8086 Microprocessor The Assembler directives for declaring procedures: (i) PROC : The PROC directive indicates the start of a named procedure. The NEAR and FAR directive specify the type of the procedure: Example: This statement indicates the beginning of a procedure named SQUARE_ROOT, which is to be called by a program located in the same segment. The FAR directive is used for procedures to be called by the programs present in code segments other than the one in which this procedure is present. For example, SALARY PROC FAR indicates the beginning of a FAR type procedure named SALARY. Dheeraj Suri Assistant Professor NIT Delhi SQUARE_ROOT PROC NEAR
  • 27.
    Assembler Directives fordeclaring procedures 27 8086 Microprocessor The Assembler directives for declaring procedures: (ii) ENDP: The ENDP directive is used to indicate the end of a procedure. To mark the end of a particular procedure, the name of the procedure may appear as prefix with the directive ENDP. Example: Dheeraj Suri Assistant Professor NIT Delhi SALARY PROC NEAR …… ; Code of SALARY ;Procedure SALARY ENDP
  • 28.
    Assembler Directives fordeclaring procedures 28 8086 Microprocessor The Assembler directives for declaring procedures: (iii) EXTRN and PUBLIC: The directive EXTRN (external) informs the assembler that the procedures, label/labels, and names declared after this directive has/have already been defined in some other segments and in the segments where they actually appear, they must be declared in public, using the PUBLIC directive. Example: Dheeraj Suri Assistant Professor NIT Delhi MODULE1 SEGMENT PUBLIC SQURE_ROOT SQUARE_ROOT PROC FAR …. ; CODE OF SQUARE_ROOT PROCEDURE SQUARE_ROOT ENDP MODULE1 ENDS ; Code continued on next ; slide
  • 29.
    Assembler Directives fordeclaring procedures 29 8086 Microprocessor The Assembler directives for declaring procedures: (iii) EXTRN and PUBLIC (continued): NOTE: If one wants to call the procedure named SQUARE_ROOT appearing in MODULE1 from MODULE2, it must be declared using the statement PUBLIC SQUARE_ROOT in MODULE1 and it must be declared external using the statement EXTRN SQUARE_ROOT in MODULE2. If a jump or a call address is external, it must be represented as NEAR or FAR. If data are defined as external, their size must be represented as BYTE, WORD, or DWORD. Dheeraj Suri Assistant Professor NIT Delhi MODULE2 SEGMENT EXTRN SQUARE_ROOT FAR …… ; CODE OF MODULE2 CALL SQUARE_ROOT …… MODULE 2 ENDS
  • 30.
    Assembler Directives fordeclaring procedures 30 8086 Microprocessor The Assembler directives for declaring procedures: (iii) EXTRN and PUBLIC (continued): NOTE: If one wants to call the procedure named SQUARE_ROOT appearing in MODULE1 from MODULE2, it must be declared using the statement PUBLIC SQUARE_ROOT in MODULE1 and it must be declared external using the statement EXTRN SQUARE_ROOT in MODULE2. If a jump or a call address is external, it must be represented as NEAR or FAR. If data are defined as external, their size must be represented as BYTE, WORD, or DWORD. Dheeraj Suri Assistant Professor NIT Delhi MODULE2 SEGMENT EXTRN SQUARE_ROOT FAR …… ; CODE OF MODULE2 CALL SQUARE_ROOT …… MODULE 2 ENDS
  • 31.
    Other Assembler Directives 31 8086Microprocessor Dheeraj Suri Assistant Professor NIT Delhi PTR The PTR(pointer) operator is used to declare the type of label, variable, or memory operand. Examples: INC BYTE PTR[SI] ;Increment the byte contents of the memory location addressed by SI INC WORD PTR [BX] ;Increment the word contents of the memory location addressed by BX GLOBAL The labels, variables, constants, or procedures declared GLOBAL may be used by other modules of the program. Example: GLOBAL DATA1, DATA2, ARRAY1 ; above statement declares the variables DATA1, ; DATA2, and ARRAY1 as GLOBAL variables LOCAL The label, variables, constants, or procedures declared LOCAL in a module are to be used only by that particular module. Example: LOCAL DATA1, DATA2, ARRAY1, A1, A2
  • 32.
    Other Assembler Directives 32 8086Microprocessor Dheeraj Suri Assistant Professor NIT Delhi NAME The NAME directive is used to assign a name to an assembly language program module. The module may now be referred to by its declared name. The names, if selected properly, may indicate the function of the different modules, and hence help in good documentation. SHORT The SHORT operator indicates to the assembler the only one byte is required to code the displacement for a jump (i.e. the displacement is within -128 to +127 bytes from the address of the byte present next to the JMP instruction) TYPE The TYPE operator directs the assembler to decide the data type of the specified label and replaces the TYPE label with the decided data type. For the word type variable, the data type is 2. For the double word type, its 4, and for the byte type its 1.
  • 33.
    MACRO and ENDM 33 8086Microprocessor Dheeraj Suri Assistant Professor NIT Delhi ; Defining a MACRO CALCULATE MACRO MOV AX, [BX] ADD AX, [BX + 2] MOV [SI], AX ENDM ; CALCULATE is the macro name and the macro is used to add two successive data in the memory, whose offset address is present in BX and the result is stored in the memory at the offset address in SI. Suppose a number of instructions occur repeatedly in the main program, the program listing becomes lengthy. In such a situation, a macro definition, i.e. a label, is assigned with the repeatedly appearing string of instructions. The process of assigning a label or macro name to the repeatedly appearing string of instructions is called macro definition. The macro name is then used throughout the main program to refer to that string of instructions.
  • 34.
    Passing parameters toa MACRO 34 8086 Microprocessor Dheeraj Suri Assistant Professor NIT Delhi CALCULATE MACRO OPERAND, RESULT MOV BX, OFFSET OPERAND MOV AX, [BX] ADD AX, [BX + 2] MOV SI, OFFSET RESULT MOV [SI], AX ENDM Using parameters in macro definition, the programmer specifies the parameters of the macro that are likely to be changed each time the macro is called. The macro given before (CALCULATE) can be modified to calculate the result for the different sets of data and store it in a different memory locations as follows:
  • 35.
    Example Programs (usingDirectives) 35 8086 Microprocessor Dheeraj Suri Assistant Professor NIT Delhi 1. Program to find the average of 10 byte-type data stored in an array in data segment. ASSUME CS: CODE1, DS: DATA1 DATA1 SEGMENT ;data segment ; starts ARRAY DB 12h, 23h, 44h, 56h, 0ABh, 73h, 44h, 0ABh, 0EEh, 0Ah ; 10 bytes are ; stored COUNT EQU 10 ; Count is the ; number of bytes ; in the array AVERAGE DB 01 DUP(0) ;Reserve one byte ; to store the result DATA1 ENDS ; data segment ; ends
  • 36.
    Example Programs (usingDirectives) 36 8086 Microprocessor Dheeraj Suri Assistant Professor NIT Delhi 1. Program to find the average of 10 byte-type data stored in an array in data segment (continued). CODE1 SEGMENT ; Code segment ; starts START: MOV AX, DATA1 ; Segment address of DATA1 is moved to AX MOV DS, AX ; MOV AX contents to DS MOV SI, OFFSET ARRAY ; Move offset ; address of ARRAY to SI XOR AX, AX ; Clear AX and Carry ; Flag MOV BX, 0000h ; Clear BX MOV CX, COUNT ; Move COUNT to CX
  • 37.
    Example Programs (usingDirectives) 37 8086 Microprocessor Dheeraj Suri Assistant Professor NIT Delhi 1. Program to find the average of 10 byte-type data stored in an array in data segment (continued). NEXT: MOV BL, [SI] ; Move one byte ; from array into BL ADD AX, BX ; Add AX and BX INC SI ; Increment SI to point to next byte LOOP NEXT ; Repeat Loop ; NEXT CX times MOV DH, COUNT ; MOV Count to DH DIV DH ;Divide AX by CH MOV AVERAGE, AL ; Store AL contents ; in AVERAGE CODE1 ENDS ;Code Segment ends
  • 38.
    Self Exercise Program(using Directives) 38 8086 Microprocessor Dheeraj Suri Assistant Professor NIT Delhi 2. Write a Program to find the parity of a given word-type data. If the parity is even, store 00h in BL register, and if the parity is odd, store 01h in BL register.