You are on page 1of 16

UNIT-III

8086 PROGRAMMING

2.1 PROGRAM DEVELOPMENT STEPS

Program Development and Execution:


The steps involved in Program Development and Execution of assembly language programs. Fig.
8.1 shows these steps. The left side of the figure shows the time period, at which each step in the
overall process takes place.

The first step in the development process is to write an assembly language program. The
assembly language program can be written with an ordinary text editor such as word star, edit
and so on. The assembly language program text is an input to the assembler.

The assembler translates assembly language statements to their binary equivalents, usually
known as object code. Time required to translate assembly code to object code is called
Assemble Time. During assembling process assembler checks for syntax errors and displays
them before giving object code module.

The object code module contains the information about where the program or module to be
loaded in memory. If the object code module is to be linked with other separately assembled
modules then it contains additional linkage information. At link time, separately assembled
modules are combined into one single load module, by the linker.

The linker also adds any required initialization or finalization code to allow the operating system
to start the program running and to return control to the operating system after the program has
completed.

Most linkers allow assembly language modules to be linked with object code modules compiled
from high-level languages as well. This allows the programmer to insert a time-critical assembly
language routines, library modules into a program.

At load time, the program loader copies the program into the computer’s main memory, and at
execution time, program execution begins

2.2 8086 INSTRUCTION SET


The 8086 microprocessor supports 8 types of instructions −

 Data Transfer Instructions


 Arithmetic Instructions
 Bit Manipulation Instructions
 String Instructions
 Program Execution Transfer Instructions (Branch & Loop Instructions)
 Processor Control Instructions
 Iteration Control Instructions
 Interrupt Instructions
Let us now discuss these instruction sets in detail.

Data Transfer Instructions

These instructions are used to transfer the data from the source operand to the destination operand.
Following are the list of instructions under this group −

Instruction to transfer a word

 MOV − Used to copy the byte or word from the provided source to the provided destination.
 PPUSH − Used to put a word at the top of the stack.
 POP − Used to get a word from the top of the stack to the provided location.
 PUSHA − Used to put all the registers into the stack.
 POPA − Used to get words from the stack to all registers.
 XCHG − Used to exchange the data from two locations.
 XLAT − Used to translate a byte in AL using a table in the memory.
Instructions for input and output port transfer

 IN − Used to read a byte or word from the provided port to the accumulator.
 OUT − Used to send out a byte or word from the accumulator to the provided port.

Instructions to transfer the address

 LEA − Used to load the address of operand into the provided register.
 LDS − Used to load DS register and other provided register from the memory
 LES − Used to load ES register and other provided register from the memory.

Instructions to transfer flag registers

 LAHF − Used to load AH with the low byte of the flag register.
 SAHF − Used to store AH register to low byte of the flag register.
 PUSHF − Used to copy the flag register at the top of the stack.
 POPF − Used to copy a word at the top of the stack to the flag register.

Arithmetic Instructions

These instructions are used to perform arithmetic operations like addition, subtraction, multiplication,
division, etc.
Following is the list of instructions under this group −

Instructions to perform addition

 ADD − Used to add the provided byte to byte/word to word.


 ADC − Used to add with carry.
 INC − Used to increment the provided byte/word by 1.
 AAA − Used to adjust ASCII after addition.
 DAA − Used to adjust the decimal after the addition/subtraction operation.

Instructions to perform subtraction

 SUB − Used to subtract the byte from byte/word from word.


 SBB − Used to perform subtraction with borrow.
 DEC − Used to decrement the provided byte/word by 1.
 NPG − Used to negate each bit of the provided byte/word and add 1/2’s complement.
 CMP − Used to compare 2 provided byte/word.
 AAS − Used to adjust ASCII codes after subtraction.
 DAS − Used to adjust decimal after subtraction.

Instruction to perform multiplication

 MUL − Used to multiply unsigned byte by byte/word by word.


 IMUL − Used to multiply signed byte by byte/word by word.
 AAM − Used to adjust ASCII codes after multiplication.

Instructions to perform division

 DIV − Used to divide the unsigned word by byte or unsigned double word by word.
 IDIV − Used to divide the signed word by byte or signed double word by word.
 AAD − Used to adjust ASCII codes after division.
 CBW − Used to fill the upper byte of the word with the copies of sign bit of the lower byte.
 CWD − Used to fill the upper word of the double word with the sign bit of the lower word.

Bit Manipulation Instructions

These instructions are used to perform operations where data bits are involved, i.e. operations like
logical, shift, etc.
Following is the list of instructions under this group −

Instructions to perform logical operation

 NOT − Used to invert each bit of a byte or word.


 AND − Used for adding each bit in a byte/word with the corresponding bit in another byte/word.
 OR − Used to multiply each bit in a byte/word with the corresponding bit in another byte/word.
 XOR − Used to perform Exclusive-OR operation over each bit in a byte/word with the
corresponding bit in another byte/word.
 TEST − Used to add operands to update flags, without affecting operands.

Instructions to perform shift operations

 SHL/SAL − Used to shift bits of a byte/word towards left and put zero(S) in LSBs.
 SHR − Used to shift bits of a byte/word towards the right and put zero(S) in MSBs.
 SAR − Used to shift bits of a byte/word towards the right and copy the old MSB into the new
MSB.

Instructions to perform rotate operations

 ROL − Used to rotate bits of byte/word towards the left, i.e. MSB to LSB and to Carry Flag
[CF].
 ROR − Used to rotate bits of byte/word towards the right, i.e. LSB to MSB and to Carry Flag
[CF].
 RCR − Used to rotate bits of byte/word towards the right, i.e. LSB to CF and CF to MSB.
 RCL − Used to rotate bits of byte/word towards the left, i.e. MSB to CF and CF to LSB.

String Instructions

String is a group of bytes/words and their memory is always allocated in a sequential order.
Following is the list of instructions under this group −
 REP − Used to repeat the given instruction till CX ≠ 0.
 REPE/REPZ − Used to repeat the given instruction until CX = 0 or zero flag ZF = 1.
 REPNE/REPNZ − Used to repeat the given instruction until CX = 0 or zero flag ZF = 1.
 MOVS/MOVSB/MOVSW − Used to move the byte/word from one string to another.
 COMS/COMPSB/COMPSW − Used to compare two string bytes/words.
 INS/INSB/INSW − Used as an input string/byte/word from the I/O port to the provided memory
location.
 OUTS/OUTSB/OUTSW − Used as an output string/byte/word from the provided memory
location to the I/O port.
 SCAS/SCASB/SCASW − Used to scan a string and compare its byte with a byte in AL or string
word with a word in AX.
 LODS/LODSB/LODSW − Used to store the string byte into AL or string word into AX.

Program Execution Transfer Instructions (Branch and Loop Instructions)

These instructions are used to transfer/branch the instructions during an execution. It includes the
following instructions −
Instructions to transfer the instruction during an execution without any condition −
 CALL − Used to call a procedure and save their return address to the stack.
 RET − Used to return from the procedure to the main program.
 JMP − Used to jump to the provided address to proceed to the next instruction.
Instructions to transfer the instruction during an execution with some conditions −
 JA/JNBE − Used to jump if above/not below/equal instruction satisfies.
 JAE/JNB − Used to jump if above/not below instruction satisfies.
 JBE/JNA − Used to jump if below/equal/ not above instruction satisfies.
 JC − Used to jump if carry flag CF = 1
 JE/JZ − Used to jump if equal/zero flag ZF = 1
 JG/JNLE − Used to jump if greater/not less than/equal instruction satisfies.
 JGE/JNL − Used to jump if greater than/equal/not less than instruction satisfies.
 JL/JNGE − Used to jump if less than/not greater than/equal instruction satisfies.
 JLE/JNG − Used to jump if less than/equal/if not greater than instruction satisfies.
 JNC − Used to jump if no carry flag (CF = 0)
 JNE/JNZ − Used to jump if not equal/zero flag ZF = 0
 JNO − Used to jump if no overflow flag OF = 0
 JNP/JPO − Used to jump if not parity/parity odd PF = 0
 JNS − Used to jump if not sign SF = 0
 JO − Used to jump if overflow flag OF = 1
 JP/JPE − Used to jump if parity/parity even PF = 1
 JS − Used to jump if sign flag SF = 1

Processor Control Instructions

These instructions are used to control the processor action by setting/resetting the flag values.
Following are the instructions under this group −
 STC − Used to set carry flag CF to 1
 CLC − Used to clear/reset carry flag CF to 0
 CMC − Used to put complement at the state of carry flag CF.
 STD − Used to set the direction flag DF to 1
 CLD − Used to clear/reset the direction flag DF to 0
 STI − Used to set the interrupt enable flag to 1, i.e., enable INTR input.
 CLI − Used to clear the interrupt enable flag to 0, i.e., disable INTR input.

Iteration Control Instructions

These instructions are used to execute the given instructions for number of times. Following is the list of
instructions under this group −
 LOOP − Used to loop a group of instructions until the condition satisfies, i.e., CX = 0
 LOOPE/LOOPZ − Used to loop a group of instructions till it satisfies ZF = 1 & CX = 0
 LOOPNE/LOOPNZ − Used to loop a group of instructions till it satisfies ZF = 0 & CX = 0
 JCXZ − Used to jump to the provided address if CX = 0

Interrupt Instructions

These instructions are used to call the interrupt during program execution.
 INT − Used to interrupt the program during execution and calling service specified.
 INTO − Used to interrupt the program during execution if OF = 1
 IRET − Used to return from interrupt service to the main program

2.2 8086 ADDRESSING MODES


The different ways in which a source operand is denoted in an instruction is known
as addressing modes. There are 8 different addressing modes in 8086 programming −

Immediate addressing mode

The addressing mode in which the data operand is a part of the instruction itself is known
as immediate addressing mode.
Example
MOV CX, 4929 H, ADD AX, 2387 H, MOV AL, FFH
Register addressing mode
It means that the register is the source of an operand for an instruction.
Example
MOV CX, AX ; copies the contents of the 16-bit AX register into
; the 16-bit CX register),
ADD BX, AX

Direct addressing mode

The addressing mode in which the effective address of the memory location is written
directly in the instruction.
Example
MOV AX, [1592H], MOV AL, [0300H]

Register indirect addressing mode

This addressing mode allows data to be addressed at any memory location through an
offset address held in any of the following registers: BP, BX, DI & SI.
Example
MOV AX, [BX] ; Suppose the register BX contains 4895H, then the contents
; 4895H are moved to AX
ADD CX, {BX}

Based addressing mode

In this addressing mode, the offset address of the operand is given by the sum of contents
of the BX/BP registers and 8-bit/16-bit displacement.
Example
MOV DX, [BX+04], ADD CL, [BX+08]
Indexed addressing mode

In this addressing mode, the operands offset address is found by adding the contents of
SI or DI register and 8-bit/16-bit displacements.
Example
MOV BX, [SI+16], ADD AL, [DI+16]

Based-index addressing mode

In this addressing mode, the offset address of the operand is computed by summing the
base register to the contents of an Index register.
Example
ADD CX, [AX+SI], MOV AX, [AX+DI]

Based indexed with displacement mode

In this addressing mode, the operands offset is computed by adding the base register
contents. An Index registers contents and 8 or 16-bit displacement.

2.4 ASSEMBLER DIRECTIVES

Assembly languages are low-level languages for programming computers,


microprocessors, microcontrollers, and other IC. They implement a symbolic
representation of the numeric machine Codes and other constants needed to program a
particular CPU architecture. This representation is usually defined by the hardware
manufacturer, and is based on abbreviations that help the programmer to remember
individual instructions, registers. An assembler directive is a statement to give direction
to the assembler to perform task of the assembly process.
It control the organization if the program and provide necessary information to the
assembler to understand the assembly language programs to generate necessary machine
codes. They indicate how an operand or a section of the program is to be processed by
the assembler.
An assembler supports directives to define data, to organize segments to control
procedure, to define macros. It consists of two types of statements: instructions and
directives. The instructions are translated to the machine code by the assembler whereas
directives are not translated to the machine codes.
Assembler Directives of the 8086 Microprocessor
(1) The DB directive
(2) The DW directive
(3) The DD directive
(4) The STRUCT (or STRUC) and ENDS directives (counted as one)
(5)The EQU Directive
(6)The COMMENT directive
(7)ASSUME
(8) EXTERN
(9) GLOBAL
(10) SEGMENT
(11)OFFSET
(12) PROC
(13)GROUP
(14) INCLUDE

Data declaration directives:


1. DB – The DB directive is used to declare a BYTE -2-BYTE variable – A BYTE is
made up of 8 bits.
Declaration examples:
Byte1 DB 10h
Byte2 DB 255 ; 0FFh, the max. possible for a BYTE
CRLF DB 0Dh, 0Ah, 24h ;Carriage Return, terminator BYTE
2. DW – The DW directive is used to declare a WORD type variable – A WORD
occupies 16 bits or (2 BYTE).
Declaration examples:
Word DW 1234h
Word2 DW 65535; 0FFFFh, (the max. possible for a WORD)
3. DD – The DD directive is used to declare a DWORD – A DWORD double word is
made up of 32 bits =2 Word’s or 4 BYTE.
Declaration examples:
Dword1 DW 12345678h
Dword2 DW 4294967295 ;0FFFFFFFFh.
4. STRUCT and ENDS directives to define a structure template for grouping data items.
(1) The STRUCT directive tells the assembler that a user defined uninitialized data
structure follows. The uninitialized data structure consists of a combination of the three
supported data types. DB, DW, and DD. The labels serve as zero-based offsets into the
structure. The first element’s offset for any structure is 0. A structure element is
referenced with the base “+” operator before the element’s name.
A Structure ends by using the ENDS directive meaning END of Structure.
Syntax:
STRUCT
Structure_element_name element_data_type?
...
...
...
ENDS
(OR)
STRUC
Structure_element_name element_data_type?
...
...
...
ENDS
DECLARATION:
STRUCT
Byte1 DB?
Byte2 DB?
Word1 DW?
Word2 DW?
Dword1DW?
Dword2 DW?
ENDS
Use OF STRUCT:
The STRUCT directive enables us to change the order of items in the structure when, we
reform a file header and shuffle the data. Shuffle the data items in the file header and
reformat the sequence of data declaration in the STRUCT and off you go. No change in
the code we write that processes the file header is necessary unless you inserted an extra
data element.
(5) The EQU Directive
The EQU directive is used to give name to some value or symbol. Each time the
assembler finds the given names in the program, it will replace the name with the value
or a symbol. The value can be in the range 0 through 65535 and it can be another Equate
declared anywhere above or below.
The following operators can also be used to declare an Equate:
THIS BYTE
THIS WORD
THIS DWORD
A variable – declared with a DB, DW, or DD directive – has an address and has space
reserved at that address for it in the .COM file. But an Equate does not have an address
or space reserved for it in the .COM file.
Example:
A – Byte EQU THIS BYTE
DB 10
A_ word EQU THIS WORD
DW 1000
A_ dword EQU THIS DWORD
DD 4294967295
Buffer Size EQU 1024
Buffer DB 1024 DUP (0)
Buffed_ ptr EQU $ ; actually points to the next byte after the; 1024th byte in buffer.
(6) Extern:
It is used to tell the assembler that the name or label following the directive are I some
other assembly module. For example: if you call a procedure which is in program
module assembled at a different time from that which contains the CALL instructions
,you must tell the assembler that the procedure is external the assembler will put
information in the object code file so that the linker can connect the two module
together.
Example:
PROCEDURE -HERE SEGMENT
EXTERN SMART-DIVIDE: FAR ; found in the segment; PROCEDURES-HERE
PROCEDURES-HERE ENDS
(7) GLOBAL:
The GLOBAL directive can be used in place of PUBLIC directive .for a name defined in
the current assembly module; the GLOBAL directive is used to make the symbol
available to the other modules. Example:
GLOBAL DIVISOR:
WORD tells the assembler that DIVISOR is a variable of type of word which is in
another assembly module or EXTERN.
(8) SEGMENT:
It is used to indicate the start of a logical segment. It is the name given to the the
segment. Example: the code segment is used to indicate to the assembler the start of
logical segment.
(9) PROC: (PROCEDURE)
It is used to identify the start of a procedure. It follows a name we give the procedure.
After the procedure the term NEAR and FAR is used to specify the procedure Example:
SMART-DIVIDE PROC FAR identifies the start of procedure named SMART-DIVIDE
and tells the assembler that the procedure is far.
(10) NAME:
It is used to give a specific name to each assembly module when program consists of
several modules.
Example: PC-BOARD used to name an assembly module which contains the instructions
for controlling a printed circuit board.
(11) INCLUDE:
It is used to tell the assembler to insert a block of source code from the named file into
the current source module. This shortens the source module. An alternative is use of
editor block command to cop the file into the current source module.
(12) OFFSET:
It is an operator which tells the assembler to determine the offset or displacement of a
named data item from the start of the segment which contains it. It is used to load the
offset of a variable into a register so that variable can be accessed with one of the
addressed modes. Example: when the assembler read MOV BX.OFFSET PRICES, it
will determine the offset of the prices.
(13) GROUP:
It can be used to tell the assembler to group the logical segments named after the
directive into one logical group. This allows the contents of all he segments to be
accessed from the same group. Example: SMALL-SYSTEM GROUP CODE, DATA,
STACK-SEG.

2.5 SIMPLE PROGRAMS WITH AN ASSEMBLER


Assembly language is a low-level programming language for a computer or other programmable
device specific to a particular computer architecture in contrast to most high-level programming
languages, which are generally portable across multiple systems. Assembly language is
converted into executable machine code by a utility program referred to as an assembler like
NASM, MASM, etc.

Problem – Write an assembly language program in 8086 microprocessor to divide a 16 bit


number by an 8 bit number.

Example –

Algorithm –
1. Assign value 500 in SI and 600 in DI
2. Move the contents of [SI] in BL and increment SI by 1
3. Move the contents of [SI] and [SI + 1] in AX
4. Use DIV instruction to divide AX by BL
5. Move the contents of AX in [DI].
6. Halt the program.

Assumption – Initial value of each segment register is 00000.

Calculation of physical memory address –


Memory Address = Segment Register * 10(H) + offset,
where Segment Register and Offset is decided on the basis of following table.

OPERATIONS SEGMENT REGISTER OFFSET

Instruction fetching Code Segment Instruction Pointer

Data operation Data Segment Base Register [BX], Displacement [DISP]

Stack operation Stack Segment Stack Pointer (SP), Base Pointer (BP)

String as a source Data Segment Source Indexed (SI)

String as a destination Extra Segment Destination Indexed (DI)

Program –

MEMORY ADDRESS MNEMONICS COMMENT

0400 MOV SI, 500 SI <- 500

0403 MOV DI, 600 DI <- 600

0406 MOV BL, [SI] BL <- [SI]

0408 INC SI SI <- SI + 1

0409 MOV AX, [SI] AX <- [SI]

040B DIV BL AX <- AX / BL

040D MOV [DI], AX [DI] <- AX


040F HLT End of program

Explanation – Registers used AX, BL, SI, DI

1. MOV SI, 500 assigns 500 to SI


2. MOV DI, 600 assigns 600 to DI
3. MOV BL, [SI] moves the content of [SI] to BL register i.e. value of divisor will be stored in BL
4. INC SI increment the content of SI by 1
5. MOV AX, [SI] moves the content of [SI] and [SI + 1] to AX register i.e. value of dividend will be
stored in AX
6. DIV BL divide the content of AX by BL, after execution of this instruction the quotient get stored
in AL and remainder in AH
7. MOV [DI], AX moves the content of AX to [DI]
8. HLT stops executing the program and halts any further execution

2.6 ASSEMBLY LANGUAGE PROGRAM DEVELOPMENT TOOL

1. Editor

- An editor is a program which allows you to create a file containing the assembly language
statements for your program.
Example: PC-Write, Wordstar.

- As you type in your program, the editor stores the ASCII codes for the letters and numbers in
successive RAM locations.

- When you have typed in all your program, you then save the file on the hard disk. This file is
called source file and the extension is .asm.

2. Assembler

- An assembler program is used to translate the assembly language mnemonics for instructions to
corresponding binary codes. When you run the assembler, it reads the source file of your
program from the disk where you have saved it after editing.

- On the first pass through the source program, the assembler determines the displacement of
named data items, the offset of labels, etc. and puts this information in a symbol table.

- On the second pass through the source program, the assembler produces the binary code for
each instruction and inserts the offsets, etc. that it calculated during the first pass.

- The assembler generates 2 files on the floppy disk or hard disk. The first file is called object file
(.obj).

- The second file generated by assembler is called the assembler list file and is given extension
(.lst).

3. Linker

- A linker is a program used to join several object files into one large object file.

- The linker produces a link file which contains the binary codes for all the combined modules.
The linker also produces a link map file which contains the address information about the linked
files (.exe).

4. Locator

- A locator is a program used to assign the specific address of where the segments of object code
are to be loaded into memory.

- A locator program called EXE2BIN comes with the IBM PC Disk Operating System (DOS).
EXE2BIN converts a .exe file to a .bin file which has physical addresses.

5. Debugger

- A debugger is a program which allows you to load your object code program into system
memory, execute the program and troubleshoot or debug it.

- The debugger allows you to look at the contents of registers and memory locations after your
program runs.

- It allows you to change the contents of registers and memory locations and re-run the program.

- Some debuggers allow you to stop execution after each instruction so that you can check or
alter after each register contents.

- A debugger also allows you to set a breakpoint at any point in your program. If you insert a
breakpoint at any point in your program, the debugger will run the program up to the instruction
where you put the breakpoint and then stop the execution.
6. Emulator

- An emulator is a mixture of hardware and software.

- It is used to test and debug the hardware and software of an external system, such as the
prototype of a microprocessor based instrument. Part of the hardware of an emulator is a
multiwire cable which connects the host system to the system being developed.

You might also like