Chap2 - 48086 Architecture and Its Programming

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 65

Intel 8086

Features of 8086 microprocessor


• Intel 8086 is a widely used 16 bit microprocessor.
• The 8086 can directly address 1MB of memory.
• The internal architecture of the 8086 microprocessor is an example of register based
microprocessor and it uses segmented memory.
• It pre-fetches up to 6 instruction bytes from the memory and queues them in order to speed up
the instruction execution.
• It has data bus of width 16 bits and address bus of width 20 bits. So it always accesses a 16 bit
word to or from memory.
• The 8086 microprocessor is divided internally into two separate units which are Bus interface
unit (BIU) and the execution unit (EU).
• The BIU fetches instructions, reads operands and write results.
• The EU executes instructions that have already been fetched by BIU so that instructions fetch
overlaps with execution.
• A 16 bit ALU in the EU maintains the MP status and control flags, manipulates general register
and instruction operands.
Bus Interface Unit(BIU) and its Components
• The BIU sends out addresses, fetches instructions from memory reads data from
memory or ports and writes data to memory or ports. So it handles all transfers of
data and address on the buses for EU. It has main 2 parts: instruction queue and
segment registers.
• The BIU can store up to 6 bytes of instructions with FIFO (First in First Out)
manner in a register set called a queue. When EU is ready for next instruction, it
simply reads the instruction from the queue in the BIU. This is done in order to
speed up program execution by overlapping instruction fetch with execution. This
mechanism is known as pipelining.
• The BIU contains a dedicated address, which is used to produce 20 bit address.
Four segment registers in the BIU are used to hold the upper 16 bits of the starting
address of four memory segments that the 8086 is working at a particular time.
These are code segment, data segment, stack segment and extra segment. The
8086’s 1 MB memory is divided into segments up to 64KB each.
• Code segment register and instruction pointer (IP): The CS contains the
base or start of the current code segment. The IP contains the distance or offset
from this address to the next instruction byte to be fetched. Code segment
address plus an offset value in the IP indicates the address of an instruction to
be fetched for execution.

• Data Segment: Data segment Contains the starting address of a program’s


data segment. Instructions use this address to locate data. This address plus an
offset value in an instruction, causes a reference to a specific byte location in
the data segment.
Stack segment (SS) and Stack Pointer (SP)
• Stack segment Contains the starting address of a program’s stack segment. This
segment address plus an offset value in the stack pointer indicates the current word in
the stack being addressed.

Extra Segment(ES)
• It is used by some string (character data) to handle memory addressing. The string
instructions always use the ES and destination index (DI) to determine 20 bit physical
address.

Execution Unit (EU)


• The EU decodes and executes the instructions. The EU contains arithmetic and logic
(ALU), a control unit, and a number of registers. These features provide for execution
of instructions and arithmetic and logical operations. It has nine 16 bit registers which
are AX, BX, CX, DX, SP, BP, SI, DI and flag. First four can be used as 8 bit register
(AH, AL, BH, BL, CH, DH, DL)
-AX Register
• AX register is called 16 bit accumulator and AL is called 8 bit accumulator. The
I/O (IN or OUT) instructions always use the AX or AL for inputting/Outputting
16 or 8 bit data from or to I/O port.

- BX Register
• BX is known as the base register since it is the only general purpose register
that can be used as an index to extend addressing. The BX register is similar to
the 8085’s H, L register. BX can also be combined with DI or SI as C base
register for special addressing.

-CX register:
• The CX register is known as the counter register because some instructions
such as SHIFT, ROTATE and LOOP use the contents of CX as a Counter.
- DX register:
• The DX register is known as data register. Some I/O operations require its use
and multiply and divide operations that involve large values assume the use of
DX and AX together as a pair. DX comprises the rightmost 16 bits of the 32-bit
EDX.
-Stack Pointer (SP) and Base Pointer (BP):
• Both are used to access data in the stack segment. The SP is used as an offset
from the current stack segment during execution of instructions. The SP’s
contents are automatically updated (increment/decrement) during execution of
a POP and PUSH instructions.
• The BP contains the offset address in the current stack segment. This offset is
used by instructions utilizing the based addressing mode.

- Index register:
• The two index registers SI (Source index) and DI (Destination Index) are used
in indexed addressing. The instructions that process data strings use the SI and
DI index register together with DS and ES respectively, in order to distinguish
between the source and destination address.
-Flag register:
• The 8086 has nine 1 bit flags. Out of 9 six are status and three are control
flags. The control bits in the flag register can be set or reset by the
programmer.

D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0


O D I T S Z A P C
• O- Overflow flag This flag is set if an arithmetic overflow occurs, i.e. if the
result of a signed operation is large enough to be accommodated in a
destination register.
• D-Direction Flag This is used by string manipulation instructions. If this flag
bit is ‘0’, the string is processed beginning from the lowest address to the
higher address, i.e. auto incrementing mode otherwise the string is processed
from the highest address towards the lowest address, i.e. auto decrementing
mode.
• I-Interrupt flag If this flag is set the maskable interrupts are recognized by
the CPU, otherwise they are ignored.
• T- Trap flag If this flag is set the processor enters the single step execution
mode. In other words, a trap interrupt is generated after execution of each
instruction. The processor executes the current instruction and the control is
transferred to the Trap interrupt service routine.
• S - Sign flag: This flag is set when the result of any computation is negative.
For signed computations, the sign flag equals the MSB of the result.
• Z- Zero This flag is set when the result of the computation is or comparison
performed by the previous instruction is zero. 1 for zero result, 0 for nonzero
result
• A- Auxiliary Carry This is set if there is a carry from the lowest nibble, i.e.
bit three during the addition or borrow for the lowest nibble i.e. bit three,
during subtraction.
• P- Parity flag This flag is set to 1 if the lower byte of the result contains even
number of 1s otherwise reset.
• C-Carry flag This flag is set when there is a carry out of MSB in case of
addition or a borrow in case of subtraction.
SEGMENT AND OFFSET ADDRESS:
• Segments are special areas defined in a program for containing the code, data and
stack. A segment begins on a paragraph boundary. A segment register is of 16 bits
in size and contains the starting address of a segment.
• A segment begins on a paragraph boundary, which is an address divisible by
decimal 16 or hex 10. Consider a DS that begins at location 038EOH. In all cases,
the rightmost hex digit is zero, the computer designers decided that it would be
unnecessary to store the zero the zero digit in the segment register. Thus 038E0H is
stores in register as 038EH.
• The distance in bytes from the segment address to another location within the
segment is expressed as an offset or displacement. Suppose the offset of 0032H for
above example of data segment. Processor combines the address of the data
segment with the offset as:
• SA: OA (segment address: offset address)
• 038E: 0032 H = 038E * 10 +0032 = 038E0 + 0032 Physical address = 03912H
Instructions in 8086
1) Arithmetic Instructions
a) ADD reg8 /mem8 , reg8/mem8/ Immediate8
• ADD reg16/mem16 , reg16/ mem16/ Immediate16
• E.g. ADD AH, 15 ; It adds binary number
• ADD Al, [BX]
• ADD [BX], CX
• ADD AX, [BX]

b) ADC: Addition with Carry


• ADC reg/ mem, reg/mem/Immediate data
c) SUB: Subtract 8 bit or 16 bit binary numbers
• SUB reg/mem, reg/mem/Immediate

d) SBB: Subtract with borrow


• SBB reg/mem, reg/mem/Immediate

e) MUL : unsigned multiplication


• MUL reg8/mem8 (8 bit accumulator – AL)
• MUL reg16/ mem16 (16 bit accumulator -AX)
• E.g. MUL R8(multiplier) =R8*AL AX (16 bit result)
MUL R16(multiplier) =R16*AL  DX:AX (32 bit result)

• IMUL – signed multiplication


• Same operation as MUL but takes sign into account
f) DIV reg/mem
• E.g. DIV R8 = AX/R8 (Remainder AH) & (Quotient AL)
DIV R16 = DX:AX/R16 (Remainder DX) & (Quotient AX)

• IDIV- Signed division


• Same operation as DIV but takes sign into account.

g) INC/DEC (Increment/Decrement by 1)
• INC/DEC reg./mem. (8 bit or 16bit)
• E.g. INC AL
DEC BX
h) NEG- Negate (2’s complement)

i) ASCII-BCD Conversion
AAA: ASCII Adjust after addition
AAS: ASCII Adjust after subtraction
AAM: Adjust after multiplication
AAD: Adjust after division
DAA: Decimal adjust after addition
DAS: Decimal adjust after subtraction
2) Logical/shifting/comparison instructions
a) Logical
• AND/OR/XOR reg/mem, reg/mem/immediate
• NOT reg/mem
• E.g. AND AL, AH
XOR [BX], CL

b) Rotation
• ROL- rotate left, ROR-rotate right
• E.g. ROL AX, 1 ; rotated by 1
ROL AX, CL ; if we need to rotate more than one bit

• RCL-rotate left through carry


• RCR-rotate right through carry
• E.g. RCL AX, 1
RCL AX, CL ; Only CL can be used
c) Shifting
• SHL -logical shift left
• SHR - logical shift right
• Shifts bit in true direction and fills zero in vacant place
• E.g. SHL reg/mem, 1/CL

• SAL- arithmetic shift left


• SAR- arithmetic shift right
• Shifts bit/word in true direction, in former case place zero in vacant place
and in later case place previous sign in vacant place.
• E.g. 1 011010 [1  11011010
d) Comparison
• CMP –compare
• CMP reg/mem, reg/mem/immediate
• E.g. CMP BH, AL
3) Data Transfer Instructions:
• MOV reg./mem , reg./mem./immediate
• LDS: Load data segment register
• LEA: load effective address
• LES: Load extra segment register
• LSS: Load stack segment register
• E.g. LEA BX, ARR = MOV BX, OFFSET ARR
LDS BX, NUM1
Segment address  DS
Offset address  BX
• XCHG reg/mem, reg/mem
• E.g. XCHG AX, BX
XCHG AL, BL
XCHG CL,[BX]
• IN AL, DX ; DX: Port address, AH also in AL
• OUT DX, AL/AH
4) Flag Operation
• CLC: Clear carry flag
• CLD: Clear direction flag
• CLI: Clear interrupt flag
• STC: Set Carry flag
• STD: Set direction flag
• STI: Set Interrupt flag
• CMC: Complement Carry flag
• LAHF: Load AH from flags (lower byte)
• SAHF: Store AH to flags
• PUSHF: Push flags into stack
• POPF: Pop flags off stack
5) STACK Operations
• PUSH reg16
• POP reg16

6) Looping instruction (CX is automatically used as a counter)


• LOOP: loop until complete
• LOOPE: Loop while equal
• LOOPNE: loop while not equal
• LOOPZ: loop while zero
• LOOPNZ: loop while not zero
7) Branching instruction
a) Conditional
• JA: Jump if Above • JNG: Jump if no greater
• JAE: Jump if above/equal • JL: Jump if less
• JB: Jump if below • JNL: Jump if no less
• JBE: Jump if below/equal • JO: jump if overflow
• JC: Jump if carry
• JS: Jump if sign
• JNC: Jump if no carry
• JNS: Jump if no sign
• JE: Jump if equal
• JP: jump if plus
• JNE: Jump if no equal
• JZ: Jump if zero
• JPE: Jump if parity even
• JNZ: Jump if no zero • JNP: Jump if no parity
• JG: Jump if greater • JPO: Jump if parity odd
b) Unconditional
• CALL: call a procedure
• RET: Return
• INT: Interrupt
• IRET: interrupt return
• JMP: Unconditional Jump
• RETN/RETF: Return near/Far
8) Type conversion
• CBW: Convert byte to word
• CWD: Convert word to double word

9) String instructions
a) MOVS/ MOVSB/MOVSW ; Move string
• DS: SI source
• DS: DI destination
• CX: String length
b) CMPS/ CMPSB/CMPW ; Compare string
c) LODS /LODSB/LODW ; Load string
d) REP ; Repeat string
• Operators in 8086
• Operator can be applied in the operand which uses the immediate data/address.
• Being active during assembling and no machine language code is generated.
• Different types of operators are:
1) Arithmetic: + , - , * , /
2) Logical : AND, OR, XOR, NOT
3) SHL and SHR: Shift during assembly
4) [ ]: index
5) HIGH: returns higher byte of an expression
6) LOW: returns lower byte of an expression.
• E.g. NUM EQU 1374 H
MOV AL HIGH Num ; ( [AL] 13 )
7) OFFSET: returns offset address of a variable
8) SEG: returns segment address of a variable

9) PTR: used with type specifications


• BYTE, WORD, RWORD, DWORD, QWORD E.g. INC BYTE PTR [BX]

10) Segment override


• MOV AH, ES: [BX]
11) LENGTH: returns the size of the referred variable

12) SIZE: returns length times type


• E.g.: BYTE VAR DB?
• WTABLE DW 10 DUP (?)
• MOV AX, TYPE BYTEVAR ; AX = 0001H
• MOV AX, TYPE WTABLE ; AX = 0002H
• MOV CX, LENGTH WTABLE ; CX = 000AH
• MOV CX, SIZE WTABLE ; CX = 0014H
Addressing modes in 8086:
• Addressing modes describe types of operands and the way in which they are
accessed for executing an instruction .
• The basic modes of addressing are register, immediate and memory which are
described below.

1) Register Addressing:
• For this mode, a register may contain source operand, destination operand or both.
• E.g.
• MOV AH, BL
• MOV DX, CX
2) Immediate Addressing
• In this type of addressing, immediate data is a part of instruction, and
appears in the form of successive byte or bytes. This mode contains a
constant value or an expression.
• E.g.
• MOV AH, 35H
• MOV BX, 7A25H
3) Direct memory addressing:
• In this type of addressing mode, a 16-bit memory address (offset) is
directly specified in the instruction as a part of it. One of the operand
is the direct memory and other operand is the register.
• E.g. ADD AX, [5000H]
• Note: Here data resides in a memory location in the data segment,
whose effective address may be computed using 5000H as the Offset
address and content of DS as segment address. The effective address,
here, is 10H*DS + 5000H
4) Direct offset addressing
• In this addressing, a variation of direct addressing uses arithmetic operators to
modify an address.
• E.g.
• ARR DB 15, 17, 18, 21
• MOV AL, ARR [2] ; MOV AL, 18
• ADD BH, ARR+3 ; ADD BH, 21

5) Indirect memory addressing:


• Indirect addressing takes advantage of computer’s capability for segment: offset
addressing. The registers used for this purpose are base register (BX and BP) and
index register (DI and SI)
• E.g.
• MOV [BX], AL
• ADD CX, [SI]
6) Base displacement addressing:
• This addressing mode also uses base registers (BX and BP) and index
register (SI and DI), but combined with a displacement (a number or offset
value) to form an effective address.
• E.g.
• MOV BX, OFFSET ARR
• =LEA BX, ARR
• MOV AL, [BX +2]
• ADD TBL [BX], CL
• TBL [BX] -----> [BX + TBL] e.g. [BX + 4]
7) Base index addressing:
• This addressing mode combines a base registers (BX or BP) with an index
register (SI or DI) to form an effective address.
• E.g.
• MOV AX, [BX +SI]
• ADD [BX+DI], CL

8) Base index with displacement addressing


• This addressing mode, a variation on base- index combines a base register, an
index register, and a displacement to form an effective address.
• E.g.
• MOV AL, [Bx+SI+2]
• ADD TBL [BX +SI], CH
9) String addressing:
• This mode uses index registers, where SI is used to point to the first byte or
word of the source string and DI is used to point to the first byte or word of
the destination string, when string instruction is executed. The SI or DI is
automatically incremented or decremented to point to the next byte or word
depending on the direction flag (DF).
• E.g. MOVS, MOVSB, MOVSW
Coding in Assembly language
• Assembly language programming language has taken its place in
between the machine language (low level) and the high level language.
• - High level language’s one statement may generate many machine
instructions.
• - Low level language consists of either binary or hexadecimal
operation. One symbolic statement generates one machine level
instructions.
• Advantage of ALP
• - They generate small and compact execution module.
• - They have more control over hardware.
• - They generate executable module and run faster.

• Disadvantages of ALP:
• - Machine dependent.
• - Lengthy code
• - Error prone (likely to generate errors).
Example
TITLE to display a string MAIN PROC FAR
.MODEL SMALL MOV AX, @DATA
MOV DS, AX
.STACK 64 MOV AH, 09H ;display string
.DATA LEA DX, STR
STR DB ‘programming is fun’, ‘$’ INT 21H
MOV AX, 4C00H
.CODE INT 21H
MAIN ENDP
END MAIN
Assembly language features:
• Program comments:
• The use of comments throughout a program can improve its clarity.
• It starts with semicolon (;) and terminates with a new line.
• E.g. ADD AX, BX ; Adds AX & BX
• Reserved words:
• Instructions : Such as MOV and ADD (operations to execute)
• Directives: Such as END, SEGMENT (information to assembler)
• Operators: Such as FAR, SIZE
• Predefined symbols: such as @DATA, @ MODEL
Identifiers:
- An identifier (or symbol) is a name that applies to an item in the program
that expects to reference.
- Two types of identifiers are Name and Label.
- Name refers to the address of a data item such as NUM1 DB 5, COUNT
DB 0
- Label refers to the address of an instruction.
- E. g: MAIN PROC FAR
- L1: ADD BL, 73
Statements:
- ALP consists of a set of statements with two types
- Instructions, e. g. MOV, ADD
- Directives, e. g. define a data item

• Directives:
• The directives are the number of statements that enables us to control the
way in which the source program assembles and lists.
• These statements called directives act only during the assembly of program
and generate no machine-executable code.
• The different types of directives are:
1) The page and title listing directives:
• The page directive defines the maximum number of lines to list as a
page and the maximum number of characters as a line.
• TITLE gives title and place the title on second line of each page of the
program.

• 2) SEGMENT directive
• It gives the start of a segment for stack, data and code.
3) PROC Directives
• The code segment contains the executable code for a program, which
consists of one or more procedures, defined initially with the PROC
directives and ended with the ENDP directive.
• PROC - name PROC [FAR/NEAR]
• PROC - name ENDP
• FAR is used for the first executing procedure and rest procedures call
will be NEAR.
• - Procedure should be within segment.
4) END Directive
• - An END directive ends the entire program and appears as the last
statement.
• - ENDS directive ends a segment and ENDP directive ends a procedure.
END PROC-Name
7) Dn Directive (Defining data types)
• Assembly language has directives to define data syntax [name] Dn
expression.
• The Dn directive can be any one of the following:
• DB Define byte 1 byte
• DW Define word 2 bytes
• DD Define double 4 bytes
• DF defined farword 6 bytes
• DQ Define quadword 8 bytes
• DT Define 10 bytes 10 bytes
8) The EQU directive
- It can be used to assign a name to constants.
- E.g. FACTOR EQU 12

9) DUP Directive
- It can be used to initialize several locations to zero.
- e. g. SUM DW 4 DUP(0)
DOS FUNCTIONS AND INTERRUPTS
• The common software interrupts used here are INT 10H for video services
and INT 21H for DOS services.
• INT 21H: It is called the DOS function call for keyboard operations follow
the function number. The service functions are listed below:
• # 02H- Display single character
- Sends the characters in DL to display.

• # 09H- string display


- Displays string until ‘$’ is reached.
- DX should have the address of the string to be displayed.
• INT 10H
-It is called video display control.
-It controls the screen format, color, text style, making windows, scrolling
etc.
Q2. Wap to display Hello World.
Wap to reverse given string for 8086.
• .model small • ADD SI, CX
• org 100h • Back: MOV DL, [SI]
• .data
• MOV AH, 02H
• String1 db 'assembly language program$'
• INT 21H
• Length dw $-String1-1
• .code • DEC SI
• Main proc • LOOP Back
• MOV AX, @data • MOV AH, 4CH
• MOV DS, AX • INT 21H
• MOV SI, offset String1 • Main endp
• MOV CX, Length
• End Main
Wap to multiply two 16-bit numbers.
Wap to find the greatest number in a block of data.
Wap to count the number of vowels in a given string.
Assembling, Linking and Executing
1. Assembling:
• Assembling converts source program into object program if syntactically correct and
generates an intermediate .obj file or module.
• Assembler converts assembly language into machine language.
• Eg: TASM( Turbo assembler), MASM(Microsoft Assembler).

• Assembler Types: There are two types of assemblers:


a) One pass assembler:
• This assembler scans the assembly language program once and converts to object code at
the same time.
• This assembler has the program of defining forward reference i.e. a JUMP instruction
using as address that appears later in the program must be defined by the programmer.
b) Two pass assembler
• This type of assembler scans the assembly language twice.
• First pass generates symbol table of names and labels used in the program and
calculates their relative address.
• This table can be seen at the end of the list file and here user need not define
anything.
• Second pass uses the table constructed in first pass and completes the object
code creation.
• No address calculations are needed to be done by the programmer for JUMP
instruction.
• This assembler is more efficient and easier than earlier.
2) Linking:
• This involves the converting of .OBJ module into .EXE(executable)
module i.e. executable machine code

3) Loading and Executing:


• - It Loads the program in memory for execution.
• - It executes to generate the result.
Macro Assembler:
• A macro is an instruction sequence that appears repeatedly in a program
assigned with a specific name.
• The macro assembler replaces a macro name with the appropriate instruction
sequence each time it encounters a macro name.
• When same instruction sequence is to be executed repeatedly, macro
assemblers allow the macro name to be typed instead of all instructions
provided the macro is defined.
• Macro are useful for the following purposes:
• To simplify and reduce the amount of repetitive coding.
• To reduce errors caused by repetitive coding.
• To make an assembly language program more readable.
• Macro executes faster because there is no need to call and return.
• Addition MACRO
IN AX, PORT
ADD AX, BX
OUT PORT, AX
ENDM
8085 vs 8086
Parameters 8085 8086
Size 8-bit 16-bit
Address bus 16-bit 20-bit
Memory 64KB 1MB
Instruction queue No Has
Pipelining No Supports
I/O’s 256 65536
Memory segmentation No segmented

You might also like