You are on page 1of 26

UNIT -2

8086 PROGRAMMING
PROGRAM DEVELOPMENT STEPS:
The following are six steps in the Program Development Cycle:
1. Analyze the problem: The computer user must figure out the problem, then decide how to resolve the
problem choose a program.
2. Design the program: A flow chart is important to use during this step of the PDLC. This is a visual diagram
of the flow containing the program. This step will help you break down the problem.
3. Code the program: This is using the language of programming to write the lines of code. The code is called
the listing or the source code. The computer user will run an object code for this step.
4. Debug the program: The computer user must debug. This is the process of finding the "bugs" on the
computer. The bugs are important to find because this is known as errors in a program.
5. Formalize the solution: One must run the program to make sure there are no syntax and logic errors. Syntax
are grammatical errors and logic errors are incorrect results.
6. Document and maintain the program: This step is the final step of gathering everything together. Internal
documentation is involved in this step because it explains the reason one might have made a change in the
program or how to write a program.

FLOW CHART SYMBOLS:

INSTRUCTION FORMAT IN 8086:


• An Instruction is a command which will instruct the processor to perform the particular operation mentioned
by it.
• In 8086, the instruction format was shown below.

Label Opcode Operands Comment


• A general 8086 instruction have 4 fields, those are
Mandatory/
S.No Field Function
optional
• It is a name, used to identify particular instruction the
1 Label Optional
program
• It is also called as operational code
• It is the short code form representation of the operation
2 Opcode Mandatory
• It tells the processor to do a particular operation
• Ex: ADD, SUB, MOV .. etc.,
P. V. N. D. K. KISHORE, Asst. Prof, ECE Dept., ACOE
• This is the data on which the operation being performed
• In 8086, minimum of zero operands, maximum of 2
3 Operands Optional
operands.
• The operands may be Data, Address, Registers
4 Comment • It is just a text, which will describe the instruction Optional

MEACHINE LANGUAGE INSTRUCTION FORMATS OF 8086 (or) TYPES OF INSTRUCTIONS:


• The 8086 Instruction 8086 Instruction Format vary from 1Byte to 6 Bytes in length.
• Depending the operands, we are used in the instruction the length will varies from 1-Bye to 6-Bytes
S.No Operands Length of the instruction
1 • No operand 1 -Byte
• only register
2 • Register to Register 2- Bytes
• Register to/from memory with no displacement
3 • Register to/from memory with 8-bit displacement 3- Bytes
• Immediate operand to register 8-bit
4 • Register to/from memory with 16-bit displacement 4-Bytes
• Immediate operand to register 16-bit
5 • Immediate operand to register 16-bit with 16-bit displacement 6-Bytes

1-Byte

2-Byte

3-Byte

4-Byte

3-Byte

4-Byte

6-Byte

P. V. N. D. K. KISHORE, Asst. Prof, ECE Dept., ACOE


ADDRESSING MODES OF 8086:
• The way of representing the operands in the instruction is called as Addressing modes.
• 8086 has 11 – different addressing modes, those are
1. Immediate addressing mode
2. Register addressing mode
3. Direct addressing mode
4. Register indirect addressing mode
5. Based addressing mode
6. Indexed addressing mode
7. Based-index addressing mode
8. Based indexed with displacement mode
9. String addressing mode
10. Input/Output addressing mode
11. Relative addressing mode

1. Immediate addressing mode:


• In this addressing mode the immediate data is mentioned in source operand.
• The data may be 8-bit (or) 16-bit.
• Ex:
MOV AX, 1234 H AX  1234 H
MOV CL, 8A H CL  8A H
2. Register addressing mode:
• In this addressing mode all the data is represented only in the registers.
• The registers may be 8-bit registers (or) 16-bit registers.
• Ex:
MOV AX, BX AX  BX
XOR AX, DX AX  AX XOR BX
ADD AL, BL AL  AL + BL
3. Direct addressing mode:
• In this addressing mode the data is stored in some memory location, the address of the data is mentioned
in the instruction
• Ex:
ADD AX, [1234] AX  AX + [1234]
MOV AX, [0500] AX  [0500]
4. Register indirect addressing mode:
• In this addressing mode, the address of the data is mentioned indirectly using the register.
• The value in the register is treated as address location of the data, that data will be processed.
• Ex:
MOV AX, [DI] AX  [DI]
ADD AL, [BX] AL  AL + [BX]
MOV AX, [SI] AX  [SI]
5. 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.
• Ex:
MOV DX, [BX+04] DX  [BX+04]
ADD CL, [BX+3428] CL  CL + [BX+3428]
6. 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.
• Ex:
MOV [DI+7865], BX [DI+7865]  BX
ADD AL, [SI+16] AL  AL + [SI+16]

P. V. N. D. K. KISHORE, Asst. Prof, ECE Dept., ACOE


7. 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.
• Ex:
ADD CX, [BX+SI] CX  CX + [BX+SI]
MOV AX, [BP+DI] AX  [BP+DI]
8. 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.
• Ex:
MOV DX, [BX+DI+08] DX  [BX+DI+08]
ADD AX, [BX+SI+1698] AX  AX + [BX+SI+1698]
9. String addressing mode:
• This addressing mode is related to string instructions. In this the value of SI and DI are auto incremented
and decremented depending upon the value of directional flag.
• Ex:
MOVS B
MOVS W
10. Input/Output addressing mode:
• This addressing mode is related with input output operations.
• Ex:
IN A, 45
OUT A, 50
11. Relative addressing mode:
• In this the effective address is calculated with reference to instruction pointer.
• Ex:
JNZ 8-bit address; in this instruction the new IP value is = IP+8-bit address

ASSEMBLER DIRECTIVES:
• Assembler directives are the instructions used by the assembler at the time of assembling a source program.
More specifically, we can say, assembler directives are the commands or instructions that control the
operation of the assembler.
• Assembler directives are the instructions provided to the assembler, not the processor as the processor has
nothing to do with these instructions. These instructions are also known as pseudo-instructions or pseudo-
opcode.
• The following the some of the assembler directives of the 8086 programming:
1. ASSUME
2. DB → Define Byte (8-bit) Same as DB & DW, but the difference is
3. DW → Define word (16-bit)
4. DD → Define double word (32-bit) • In DD → we are defining variable with 32 bits
5. DQ → Define quad word (64-bit) • In DQ → we are defining variable with 64 bits
6. DT → Define 10 bytes (80-bits) • In DT → we are defining variable with 80 bits
7. END
8. ENDP
9. ENDS
10. EQU
11. EVEN
12. EXTRN
13. GROUP
14. LABLE
15. NAME
16. OFFSET
17. ORG
18. PROC
19. PTR
20. PUBLC
21. SEGMENT 22. SHORT 23. TYPE

P. V. N. D. K. KISHORE, Asst. Prof, ECE Dept., ACOE


1. ASSUME Directive - The ASSUME directive is used to tell the assembler that the name of the logical
segment should be used for a specified segment. The 8086 works directly with only 4 physical segments:
a Code segment, a data segment, a stack segment, and an extra segment.
Ex:
ASUME CS: CODE
ASUME DS: DATA
2. DB - DB directive is used to declare a byte- type variable or to store a byte in memory location.
Ex:
HELLO DB 67h
PRICE DB 49h, 98h, 29h
3. DW - The DW directive is used to define a variable of type word or to reserve storage location of type
word in memory.
Ex:
MULTIPLIER DW 437Ah
EXP1 DW 1234h, 3456h, 5678h
STOR1 DW 100 DUP (0)
4. END - END directive is placed after the last statement of a program to tell the assembler that this is the
end of the program module. The assembler will ignore any statement after an END directive. Carriage return
is required after the END directive.
5. ENDP - ENDP directive is used along with the name of the procedure to indicate the end of a procedure to
the assembler
6. ENDS - This ENDS directive is used with name of the segment to indicate the end of that logic segment.
Ex:
CODE SEGMENT ; Hear it Start the logic segment containing code
.
; Some instructions statements to perform the logical operation
.
CODE ENDS ; End of segment named as CODE
7. EQU - This EQU directive is used to give a name to some value or to a symbol. Each time the assembler
finds the name in the program, it will replace the name with the value or symbol you given to that name.
Ex:
FACTOR EQU 03H ; you have to write this statement at the starting of your program and later in
the program you can use this as follows
ADD AL, FACTOR ; When it codes this instruction, the assembler will code it as ADDAL, 03H,
The advantage of using EQU in this manner is, if FACTOR is used many no of times in a program and you
want to change the value, all you had to do is change the EQU statement at beginning, it will changes the rest
of all.

8. EVEN -
• This EVEN directive instructs the assembler to increment the location of the counter to the next
even address if it is not already in the even address. If the word is at even address 8086 can read a
memory in 1 bus cycle.
• If the word starts at an odd address, the 8086 will take 2 bus cycles to get the data. A series of words
can be read much more quickly if they are at even address. When EVEN is used the location counter
will simply incremented to next address and NOP instruction is inserted in that incremented location.
9. GROUP - The GROUP directive is used to group the logical segments named after the directive into
one logical group segment.
10. INCLUDE - This INCLUDE directive is used to insert a block of source code from the named file into the
current source module.
11. PROC - The PROC directive is used to identify the start of a procedure. The term near or far is used to
specify the type of the procedure.
Ex:
SMART PROC FAR ; This identifies that the start of a procedure named as SMART and instructs
the assembler that the procedure is far.
SMART ENDP ; This PROC is used with ENDP to indicate the break of the procedure.

P. V. N. D. K. KISHORE, Asst. Prof, ECE Dept., ACOE


12. PTR - This PTR operator is used to assign a specific type of a variable or to a label
Ex:
INC [BX] ; This instruction will not know whether to increment the byte
pointed to by BX or a word pointed to by BX.
INC BYTE PTR [BX] ; increment the byte pointed to by BX
13. PUBLIC - The PUBLIC directive is used to instruct the assembler that a specified name or label will be
accessed from other modules
Ex:
PUBLIC DIVISOR, DIVIDEND ; these two variables are public so these are available to all
modules.

14. TYPE - TYPE operator instructs the assembler to determine the type of a variable and determines the number
of bytes specified to that variable
Ex:
Byte type variable – assembler will give a value 1
Word type variable – assembler will give a value 2
Double word type variable – assembler will give a value 4

15. DOS Function Calls:


AH 00H : Terminate a Program
AH 01H : Read the Keyboard
AH 02H : Write to a Standard Output Device
AH 08H : Read a Standard Input without Echo
AH 09H : Display a Character String
AH 0AH : Buffered keyboard Input
INT 21H : Call DOS Function

ASSEMBLY LANGUAGE PROGRAM DEVELOPMENT TOOLS:


• The fallowing are the assembly language development tools, those are
1. Editor
2. Assembler
3. Linker
4. Locators
5. Debuggers
6. Emulators
1. Editor:
• An Editor is a program which allows us to create a file containing the assembly language statements for
the program. Examples of some editors are PC write Wordstar.
• As we type the program the editor stores the ACSII codes for the letters and numbers in successive RAM
locations.
• If any typing mistake is done editor will alert us to correct it.
• If we leave out a program statement an editor will let you move everything down and insert a line.
• After typing all the program, we have to save the program for a hard disk.
• This we call it as source file.
• The next step is to process the source file with an assembler.
• While using TASM or MASM we should give a file name and extension .ASM. Ex: Sample. asm
2. Assembler:
• An Assembler is used to translate the assembly language mnemonics into machine language (i.e binary
codes).
• When you run the assembler, it reads the source file of your program from where you have saved it.
• The assembler generates two files. The first file is the Object file with the extension .OBJ.
• The object file consists of the binary codes for the instructions and information about the addresses of the
instructions.
• After further processing, the contents of the file will be loaded in to memory and run. The second file is the
assembler list file with the extension. LST.

P. V. N. D. K. KISHORE, Asst. Prof, ECE Dept., ACOE


3. Linker:
• A linker is a program used to connect several object files into one large object file.
• While writing large programs it is better to divide the large program into smaller modules.
• Each module can be individually written, tested and debugged.
• Then all the object modules are linked together to form one, functioning program.
• These object modules can also be kept in library file and linked into other programs as needed.
• A 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.
• The linkers which come with TASM or MASM assemblers produce link files with the .EXE extension.
4. Locator:
• A locator is a program used to assign the specific addresses 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 to load your object code program into system memory, execute the
program, and troubleshoot or debug it.
• The debugger allows to look into the contents of registers and memory locations after the program runs.
We can also change the contents of registers and memory locations and rerun the program.
• Some debuggers allow to stop the program after each instruction so that you can check or alter memory
and register contents. This is called single step debug.
• A debugger also allows to set a breakpoint at any point in the program.
• If we insert a break point, the debugger will run the program up to the instruction where the breakpoint is
put and then stop the execution.
6. Emulator:
• An emulator is a mixture of hard ware and software.
• It is usually used to test and debug the hardware and software of an external system such as the prototype
of a microprocessor-based instrument.

The fallowing flow chart will give the how the program is developed and executed

P. V. N. D. K. KISHORE, Asst. Prof, ECE Dept., ACOE


4. STRING INSTRUCTIONS:
• These instructions are used to perform the sting related operations i.e., on group of data.
o REP Used to repeat the given instruction till CX ≠ 0.
o REPE/REPZ Used to repeat the given instruction until CX = 0 or zero flag ZF = 1.
o REPNE/REPNZ Used to repeat the given instruction until CX = 0 or zero flag ZF = 0.
o MOVS/MOVSB/MOVSW Used to move the byte/word from one string to another.
o COMS/COMPSB/COMPSW Used to compare two string bytes/words.
o INS/INSB/INSW Used as an input string/byte/word from the I/O port to the provided
memory location.
o OUTS/OUTSB/OUTSW Used as an output string/byte/word from the provided memory
location to the I/O port.
o 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.
o LODS/LODSB/LODSW Used to store the string byte into AL or string word into AX.

5. PROGRAM EXECUTION TRANSFER INSTRUCTIONS (BRANCH AND LOOP INSTRUCTIONS):


• These instructions are used to transfer the execution of the program from one location to another location.
• The program branching instructions are classified as bellow
PROGRAM
BRANCHING
INSTRUCTIONS

JUMPING CALLING
INSTRUCTIONS INSTRUCTIONS

CONDITIONAL
JUMP
INSTRUCTIONS

UN CONDITIONAL
JUMP
INSTRUCTIONS

• CALLING INSTRUCTIONS:
▪ These instructions are used to call a sub program located at some address.
▪ Whenever CALL instructions are execute, the present values of the registers, PSW, PC will saved in
the Stack memory.
▪ And now the Sub program will execute.
▪ After completion of the execution of the Sub program, again the controller return back to main program
by using the instruction RET.
▪ While retuning to the main program, the previously saved values of the registers, PSW, PC will be
retrieved from the stack memory.
▪ The fallowing are comes under the category of call instructions.
o CALL → Call a Subprogram at the specified address.
o RET → Return from the Subprogram to main program.
• JUMPING INSTRUCTIONS:
▪ These instructions are used to jump the execution from one instruction to another instruction.
▪ The Jumping instructions are again divided into two, those are
i. Unconditional Jumping Instructions
ii. Conditional Jumping Instructions.
i. UNCONDITIONAL JUMPING INSTRUCTIONS:
▪ These instructions are used to jump from one location to another location without checking any
condition.
▪ The following instructions will come under the category of unconditional jump instructions, those are
o JMP → Jump to the specified address location.

P. V. N. D. K. KISHORE, Asst. Prof, ECE Dept., ACOE


ii. CONDITIONAL JUMPING INSTRUCTIONS:
▪ These instructions are used to jump to an instruction, by checking a condition.
▪ The fallowing are the Conditional jump instructions, those are
JC <Relative Address> First checks the Carry flag,
▪ If CF = 1, then jump to specified address
▪ If CF = 0, then execute the next instruction.
JNC <Relative Address> First checks the Carry flag,
▪ If CF = 0, then jump to specified address
▪ If CF = 1, then execute the next instruction.
JE/JZ <Relative Address> First checks the Zero flag,
▪ If ZF = 1, then jump to specified address
▪ If ZF = 0, then execute the next instruction.
JNE/JNZ <Relative Address> First checks the Zero flag,
▪ If ZF = 0, then jump to specified address
▪ If ZF = 1, then execute the next instruction.
JO <Relative Address> First checks the Overflow flag,
▪ If OF = 1, then jump to specified address
▪ If OF = 0, then execute the next instruction.
JNO <Relative Address> First checks the Overflow flag,
▪ If OF = 0, then jump to specified address
▪ If OF = 1, then execute the next instruction.
JP/JPE <Relative Address> First checks the parity flag,
▪ If PF = 1, then jump to specified address
▪ If PF = 0, then execute the next instruction.
JNP/JPO <Relative Address> First checks the Parity flag,
▪ If PF = 0, then jump to specified address
▪ If PF = 1, then execute the next instruction.
JS <Relative Address> First checks the Sign flag,
▪ If SF = 1, then jump to specified address
▪ If SF = 0, then execute the next instruction.
JNS <Relative Address> First checks the Sign flag,
▪ If SF = 0, then jump to specified address
▪ If SF = 1, then execute the next instruction.
JG/JNLE <Relative Address> Jump if first operand is Greater then second operand after CMP instruction
▪ if (ZF = 0) and (SF = OF) then jump
▪ Otherwise next instruction will be executed
JGE/JNL <Relative Address> Jump if first operand is Greater then or equal the second operand after CMP
instruction
▪ if SF = OF then jump
▪ Otherwise next instruction will be executed
JL/JNGE <Relative Address> Jump if first operand is Less then second operand after CMP instruction
▪ if SF <> OF then jump
▪ Otherwise next instruction will be executed
JLE/JNG <Relative Address> Jump if first operand is Less then or equal the second operand after CMP
instruction
▪ if SF <> OF or ZF = 1 then jump
▪ Otherwise next instruction will be executed
JA/JNBE <Relative Address> Jump if first operand is Above second operand after CMP instruction
▪ if (CF = 0) and (ZF = O) then jump
▪ Otherwise next instruction will be executed
JAE/JNB <Relative Address> Jump if first operand is is Above or Equal to second operand after CMP
instruction
▪ if CF = 0 then jump
▪ Otherwise next instruction will be executed
JBE/JNA <Relative Address> Jump if first operand is is s Below or Equal to second operand after CMP
instruction
▪ if CF = 1 or ZF = 1 then jump
▪ Otherwise next instruction will be executed

P. V. N. D. K. KISHORE, Asst. Prof, ECE Dept., ACOE


6. PROCESSOR CONTROL INSTRUCTIONS:
• These instructions are used to control the processor action by setting/resetting the flag values.
o STC − Used to set carry flag CF to 1
o CLC − Used to clear/reset carry flag CF to 0
o CMC − Used to put complement at the state of carry flag CF.
o STD − Used to set the direction flag DF to 1
o CLD − Used to clear/reset the direction flag DF to 0
o STI − Used to set the interrupt enable flag to 1, i.e., enable INTR input.
o CLI − Used to clear the interrupt enable flag to 0, i.e., disable INTR input.
7. ITERATION CONTROL INSTRUCTIONS:
• These instructions are used to execute the given instructions for number of times.
o LOOP − Used to loop a group of instructions until the condition satisfies, i.e., CX = 0
o LOOPE/LOOPZ − Used to loop a group of instructions till it satisfies ZF = 1 & CX = 0
o LOOPNE/LOOPNZ − Used to loop a group of instructions till it satisfies ZF = 0 & CX = 0
o JCXZ − Used to jump to the provided address if CX = 0
8. INTERRUPT INSTRUCTIONS:
• These instructions are used to call the interrupt during program execution.
o INT − Used to interrupt the program during execution and calling service specified.
o INTO − Used to interrupt the program during execution if OF = 1
o IRET − Used to return from interrupt service to the main program

P. V. N. D. K. KISHORE, Asst. Prof, ECE Dept., ACOE

You might also like