8086 Microprocessor

Download as pdf or txt
Download as pdf or txt
You are on page 1of 48

Unit 2 and 4: 8086 Microprocessor

 Logical Block Diagram of 8086


o Bus Interface Unit(BIU) and Execution Unit(EU)
o Pipelining
o Segment Registers,
 Memory Segmentation
 Programming with Intel 8086 microprocessor
o Macro Assembler
o Assembling and Linking
o Assembler Directives, Comments
o Instructions: LEA, MUL, DIV, LOOP, AAA, DAA
o Addressing modes
o INT 21H Functions - 01H, 02H, 09H, 0AH, 4CH
o INT 10H Functions (Introduction Only) - 00H, 01H, 02H, 06H, 07H, 08H, 09H, 0AH

IMPORTANT FEATURES OF 8086:


 It is a 40 pins dual inline package.
 It requires +5V power Supply.
 It operates on 6-10 MHz Frequency.
 It provides 16-bit registers.
 It has multiplexed Address and Data Bus AD0-AD15 and A16-A19.
 It prefetches up to 6 instruction bytes from memory and Queues them in order to speed up the instruction
execution.
 Memory is a byte addressable –Every byte has a separate address.
1) Buses:
 Address Bus: 8086 has a 20-bit address bus, hence it can access 220 Byte memory i.e. 1MB.
The address range for this memory is 00000H … FFFFFH.
 Data Bus: 8086 has a 16-bit data bus i.e. it can access 16-bit data in one operation. Its ALU
and internal data registers are also 16-bit. Hence 8086 is called as a 16-bit µP.
 Control Bus: The control bus carries the signals responsible for performing various operations
such as RD, WR etc.
2) 8086 supports Pipelining.
 It is the process of “Fetching the next instruction, while executing the current instruction”.
Pipelining improves performance of the system.

3) 8086 has 2 Operating Modes.


i. Minimum Mode … here 8086 is the only processor in the system (uni-processor).
ii. Maximum Mode … 8086 with other processors like 8087-NDP/8089-IOP etc. Maximum
mode is intended for multiprocessor configuration.
4) 8086 provides Memory Banks.
 The entire memory of 1 MB is divided into 2 banks of 512KB each, in order to transfer 16bits in 1
cycle. The banks are called Lower Bank (even) and Higher Bank (odd).
5) 8086 supports Memory Segmentation.
 Segmentation means dividing the memory into logical components. Here the memory is divided
into 4 segments: Code, Stack, Data and Extra Segment.

6) 8086 has 256 interrupts.


 The ISR addresses for these interrupts are stored in the IVT (Interrupt Vector Table).
7) 8086 has a 16-bit IO address ∴ it can access 216 IO ports (216 = 65536 i.e. 64K IO Ports).
ARCHITECTURE OF 8086
 As 8086 does 2-stage pipelining, its architecture is divided into two units:
1. Bus Interface Unit (BIU)
2. Execution Unit (EU)

BUS INTERFACE UNIT (BIU)

1. It provides the interface of 8086 to other devices.


2. It operates w.r.t. Bus cycles.
This means it performs various machine cycles such as Mem Read, IO Write etc to transfer data with
Memory and I/O devices.
3. It performs the following functions:
a) It generates the 20-bit physical address for memory access.
b) Fetches Instruction from memory.
c) Transfers data to and from the memory and IO.
d) Supports Pipelining using the 6-byte instruction queue.
The main components of the BIU are as follows:
a) SEGMENT REGISTERS:

1) CS Register
 CS holds the base (Segment) address for the Code Segment.
 All programs are stored in the Code Segment.
 It is multiplied by 10H (16d), to give the 20-bit physical address of the Code Segment.
Eg: If CS = 4321H then CS × 10H = 43210H Starting address of Code Segment.
 CS register cannot be modified by executing any instruction except branch instructions
2) DS Register
 DS holds the base (Segment) address for the Data Segment.
 It is multiplied by 10H (16d), to give the 20-bit physical address of the Data Segment.
Eg: If DS = 4321H then DS × 10H = 43210H Starting address of Data Segment.

3) SS Register
 SS holds the base (Segment) address for the Stack Segment.
 It is multiplied by 10H (16d), to give the 20-bit physical address of the Stack Segment.
Eg: If SS = 4321H then SS × 10H = 43210H Starting address of Stack Segment.

4) ES Register
 ES holds the base (Segment) address for the Extra Segment.
 It is multiplied by 10H (16d), to give the 20-bit physical address of the Extra Segment. Eg: If
ES = 4321H then ES × 10H = 43210H Starting address of Extra Segment.

b) Instruction Pointer (IP register)


 It is a 16-bit register.
 It holds offset of the next instruction in the Code Segment.
 Address of the next instruction is calculated as CS x 10H + IP.
 IP is incremented after every instruction byte is fetched. IP gets a new value whenever a
branch occurs.

c) Address Generation Circuit


 The BIU has a Physical Address Generation Circuit. It generates the 20-bit physical address
using Segment and Offset addresses using the formula:

Physical address(PA) = Segment Address(SA) x 10h + Offset Address


Note:

 Lower byte of word is stored at lower address


 The word ABC2H stored in the memory starting at 20-bit address 50000H.
50000H- C2H
50001H -ABH
 The double word 452ABDFFH stored in the memory starting at 20-bit address 60000H.
60000H - FFH
60001H - BDH
60002H - 2AH
60003H - 45H

Viva Question: Explain the real procedure to obtain the Physical Address?
The Segment address is left shifted by 4 positions, this multiplies the number by 16 (i.e. 10h) and then the
offset address Is added.

Eg: If Segment address is 1234h and 0ffset address is 0005h, then the physical address (12345h) is
calculated as follows:
 1234h = (0001 0010 0011 0100) binary
 Left shift by four positions and we get (0001 0010 0011 0100 0000) binary i.e. 12340h
 Now add (0000 0000 0000 0101) binary i.e. 0005h and we get (0001 0010 0011 0100 0101) binary i.e.
12345h.

d) 6-Byte Pre-Fetch Queue {Pipelining}


 It is a 6-byte FIFO RAM used to implement Pipelining.
 Fetching the next instruction while executing the current instruction is called Pipelining.
 BIU fetches the next “six instruction-bytes” from the Code Segment and stores it into the queue.
 Execution Unit (EU) removes instructions from the queue and executes them.
 The queue is refilled when at least two bytes are empty as 8086 has a 16-bit data bus.
 Pipelining increases the efficiency of the µP.
 Pipelining fails when a branch occurs, as the pre-fetched instructions are no longer useful. Hence
as soon as 8086 detects a branch operation, it clears/discards the entire queue. Now, the next six
bytes from the new location (branch address) are fetched and stored in the queue and Pipelining
continues.
 NON-PIPELINED PROCESSOR EG: 8085

 PIPELINED PROCESSOR EG: 8086

F1 E1 E2 E3 E4 E5 Time
F2 F3 F4 F5
Overlapping fetching
and execution

Total time taken


Execution Unit (EU)
 It fetches instructions from the Queue in BIU, decodes and executes them.
 It performs arithmetic, logic and internal data transfer operations.
 It sends request signals to the BIU to access the external module.
 It operates w.r.t. T-States (clock cycles).

The main components of the EU are as follows:


a) General Purpose Registers
 8086 has four 16-bit general-purpose registers AX, BX, CX and DX. These are available to the
programmer, for storing values during programs. Each of these can be divided into two 8-bit
registers such as AH, AL; BH, BL; etc. Beside their general use, these registers also have some
specific functions.

 AX Register (16-Bits)
 It holds operands and results during multiplication and division operations.
 All IO data transfers using IN and OUT instructions use A reg (AL/AH or AX). It functions as
accumulator during string operations.
 BX Register (16-Bits)
 Holds the memory address (offset address), in Indirect Addressing modes.
 CX Register (16-Bits)
 Holds count for instructions like: Loop, Rotate, Shift and String Operations.
 DX Register (16-Bits)
 It is used with AX to hold 32 bit values during Multiplication and Division.
 It is used to hold the address of the IO Port in indirect IO addressing mode.

b) Special Purpose Registers


 Stack Pointer (SP 16-Bits)
 It is holds offset address of the top of the Stack. Stack is a set of memory locations operating
in LIFO manner. Stack is present in the memory in Stack Segment.
 SP is used with the SS Reg to calculate physical address for the Stack Segment. It used during
instructions like PUSH, POP, CALL, RET etc. During PUSH instruction, SP is decremented by 2
and during POP it is incremented by 2.
 Base Pointer (BP 16-Bits)
 BP can hold offset address of any location in the stack segment.
 It is used to access random locations of the stack.
 Source Index (SI 16-Bits)
 It is normally used to hold the offset address for Data segment but can also be used for other
segments using Segment Overriding. It holds offset address of source data in Data Seg, during
String Operations.
 Destination Index (DI 16-Bits)
 It is normally used to hold the offset address for Extra segment but can also be used for other
segments using Segment Overriding. It holds offset address of destination in Extra Seg, during
String Operations.

c) ALU (16-Bits)
 It has a 16-bit ALU. It performs 8 and 16-bit arithmetic and logic operations.

d) Operand Register
 It is a 16-bit register used by the control register to hold the operands temporarily. It is not
available to the Programmer.

e) Instruction Register and Instruction Decoder (Present inside the Control Unit)
 The EU fetches an opcode from the queue into the Instruction Register. The Instruction
Decoder decodes it and sends the information to the control circuit for execution.

f) Flag Register (16-Bits)


 It has 9 Flags.
 These flags are of two types: 6-Status (Condition) Flags and 3-Control Flags.
 Status flags are affected by the ALU, after every arithmetic or logic operation. They give the status
of the current result.
 The Control flags are used to control certain operations. They are changed by the programmer.

STATUS FLAGS
1) Carry flag (CY)
 It is set whenever there is a carry {or borrow} out of the MSB of a the result (D7 bit for an 8-bit
operation D15 bit for a 16-bit operation) .
2) Parity Flag (PF)
It is set if the result has even parity.
3) Auxiliary Carry Flag (AC)
It is set if a carry is generated out of the Lower Nibble. It is used only in 8-bit operations like DAA and
DAS.
4) Zero Flag (ZF)
 It is set if the result is zero.

5) Sign Flag (SF)


 It is set if the MSB of the result is 1.
 For signed operations, such a number is treated as –ve.

6) Overflow Flag (OF)


 It will be set if the result of a signed operation is too large to fit in the number of bits available to
represent it. It can be checked using the instruction INTO (Interrupt on Overflow).

Eg: try yourself

CONTROL FLAGS

1) Trap Flag (TF)


 It is used to set the Trace Mode i.e. start Single Stepping Mode.
 Here the µP is interrupted after every instruction so that, the program can be debugged.

2) Interrupt Enable Flag (IF)


 It is used to mask (disable) or unmask (enable) the INTR interrupt.

3) Direction Flag (DF)


 If this flag is set, SI and DI are in auto-decrementing mode in String Operations.

MEMORY SEGMENTATION IN 8086


NEED FOR SEGMENTATION/ CONCEPT OF SEGMENTATION
1) Segmentation means dividing the memory into logically different parts called segments.
2) 8086 has a 20-bit address bus, hence it can access 220 Bytes i.e. 1MB memory.
3) But this also means that Physical address will now be 20 bit.
4) It is not possible to work with a 20-bit address as it is not a byte compatible number.
(20 bits is two and a half bytes).
5) To avoid working with this incompatible number, we create a virtual model of the memory.
6) Here the memory is divided into 4 segments: Code, Stack Data and Extra.
7) The max size of a segment is 64KB and the minimum size is 16 bytes.
8) Now programmer can access each location with a VIRTUAL ADDRESS.
9) The Virtual Address is a combination of Segment Address and Offset Address.
10) Segment Address indicates where the segment is located in the memory (base address)
11) Offset Address gives the offset of the target location within the segment.
12) Since both, Segment Address and Offset Address are 16 bits each, they both are compatible
numbers and can be easily used by the programmer.
13) Moreover, Segment Address is given only in the beginning of the program, to initialize the
segment. Thereafter, we only give offset address.
14) Hence we can access 1 MB memory using only a 16 bit offset address for most part of the
program. This is the advantage of segmentation.
15) Moreover, dividing Code, stack and Data into different segments, makes the memory more
organized and prevents accidental overwrites between them.
16) The Maximum Size of a segment is 64KB because offset addresses are of 16 bits.
216 = 64KB.
17) As max size of a segment is 64KB, programmer can create multiple Code/Stack/Data segments till
the entire 1 MB is utilized, but only one of each type will be currently active.
18) The physical address is calculated by the microprocessor, using the formula:

PHYSICAL ADDRESS = SEGMENT ADDRESS X 10H + OFFSET ADDRESS


Ex: if Segment Address = 1234H and Offset Address is 0005H then
Physical Address = 1234H x 10H + 0005H = 12345H
19) This formula automatically ensures that the minimum size of a segment is 10H bytes (10H = 16
Bytes).

Code Segment
 This segment is used to hold the program to be executed. Instruction are fetched from the Code
Segment.
 CS register holds the 16-bit base address for this segment.
 IP register (Instruction Pointer) holds the 16-bit offset address.

Data Segment
 This segment is used to hold general data.
 This segment also holds the source operands during string operations.
 DS register holds the 16-bit base address for this segment.
 BX register is used to hold the 16-bit offset for this segment.
 SI register (Source Index) holds the 16-bit offset address during String Operations.

Stack Segment
 This segment holds the Stack memory, which operates in LIFO manner.
 SS holds its Base address.
 SP (Stack Pointer) holds the 16-bit offset address of the Top of the Stack.
 BP (Base Pointer) holds the 16-bit offset address during Random Access.

Extra Segment
 This segment is used to hold general data
 Additionally, this segment is used as the destination during String Operations.
 ES holds the Base Address.
 DI holds the offset address during string operations.

Advantages of Segmentation:
 It permits the programmer to access 1MB using only 16-bit address.
 Its divides the memory logically to store Instructions, Data and Stack separately.
Disadvantage of Segmentation:
 Although the total memory is 16*64 KB, at a time only 4*64 KB memory can be accessed.
MEMORY BANKING IN 8086
 As 8086 has a 16-bit data bus, it should be able to access 16-bit data in one cycle.
 To do so it needs to read from 2 memory locations, as one memory location carries only one byte. 16-
bit data is stored in two consecutive memory locations.
 However, if both these memory locations are in the same memory chip then they cannot be accessed at
the same time, as the address bus of the chip cannot contain two address simultaneously.
 Hence, the memory of 8086 is divided into two banks each bank provides 8-bits.
 The division is done in such a manner that any two consecutive locations lie in two different chips.
Hence each chip contains alternate locations.
∴ One bank contains all even addresses called the “Even bank”, while the other is called “Odd
bank” containing all odd addresses.
 Generally, for any 16-bit operation, the Even bank provides the lower byte and the ODD bank
provides the higher byte. Hence the Even bank is also called the Lower bank and the Odd bank is
also called the Higher bank.

Addressing modes of 8085


 Addressing modes describe types of operands and the way in which they are accessed for executing an
instruction.
 An operand address provides source of data for an instruction to process an instruction to process.
 An instruction may have from zero to two operands.
 For two operands first is destination and second is source operand.
 The basic modes of addressing are register, immediate and memory which are described below.

I. IMMEDIATE ADDRESSING MODE


 In this mode, the operand is specified in the instruction itself.
 Instructions are longer but the operands are easily identified.
Eg: MOV CL, 12H ; Moves 12 immediately into CL register
MOV BX, 1234H ; Moves 1234 immediately into BX register

II. REGISTER ADDRESSING MODE


 In this mode, operands are specified using registers.
 Instructions are shorter but operands can’t be identified by looking at the instruction.
Eg: MOV CL, DL ; Moves data of DL register into CL register
MOV AX, BX ; Moves data of BX register into AX register

III. DIRECT ADDRESSING MODE


 In this mode, address of the operand is directly specified in the instruction.
 Here only the offset address is specified, the segment being indicated by the instruction.
Eg: MOV CL, [4321H] ; Moves data from location 4321H in the data
; segment into CL
; The physical address is calculated as
; DS * 10H + 4321
; Assume DS = 5000H
; ∴ P A= 50000 + 4321 = 54321H
; ∴ CL [54321H]

Eg: MOV CX, [4320H] ; Moves data from location 4320H and 4321H
; in the data segment into CL and CH resp.

IV. INDIRECT ADDRESSING MODES

 REGISTER INDIRECT ADDRESSING MODE


 In this mode the µP uses any of the 2 base registers BP, BX or any of the two index registers SI,
DI to provide the offset address for the data byte.
 The segment is indicated by the Base Registers:
BX -- Data Segment, BP --- Stack Segment
Eg: MOV CL, [BX] ; Moves a byte from the address pointed by BX in Data
; Segment into CL.
; Physical Address calculated as DS * 10H + BX
Eg: MOV [BP], CL ; Moves a byte from CL into the location pointed by BP in
; Stack Segment.
; Physical Address calculated as SS * 10H + BP
 REGISTER RELATIVE ADDRESSING MODE
 In this mode the operand address is calculated using one of the base registers and an 8-bit or a
16-bit displacement.

Eg: MOV CL, [BX+4] ; Moves a byte from the address pointed by BX+4 in
; Data Seg to CL.
; Physical Address: DS * 10H + BX + 4H

Eg: MOV 12H [BP], CL ; Moves a byte from CL to location pointed by BP+12H in
; the Stack Seg.
; Physical Address: SS * 10H + BP + 12H
 BASE INDEXED ADDRESSING MODE
 Here, operand address is calculated as Base register plus an Index register.

Eg: MOV CL, [BX+SI] ; Moves a byte from the address pointed by BX+SI
; in Data Segment to CL.
; Physical Address: DS * 10H + BX + SI

Eg: MOV [BP+DI], CL ; Moves a byte from CL into the address pointed by
; BP+DI in Stack Segment.
; Physical Address: SS * 10H + BP + DI

 BASE RELATIVE PLUS INDEX ADDRESSING MODE


 In this mode the address of the operand is calculated as Base register plus Index register plus 8-
bit or 16-bit displacement.

Eg: MOV CL, [BX+DI+20] ; Moves a byte from the address pointed by
; BX+SI+20H in Data Segment to CL.
; Physical Address: DS * 10H + BX + SI+ 20H

Eg: MOV [BP+SI+2000], CL ; Moves a byte from CL into the location pointed by
; BP+SI+2000H in Stack Segment.
; Physical Address: SS * 10H + BP+SI+2000H

V. IMPLIED ADDRESSING MODE


 In this addressing mode the operands are implied and are hence not specified in the instruction.
Eg: STC ; Sets the Carry Flag.
Eg: CLD ; Clears the Direction Flag.

Important points for understanding addressing modes


1) Anything given in square brackets will be an Offset Address also called Effective
Address.
2) MOV instruction by default operates on the Data Segment; unless specified otherwise.
3) BX and BP are called Base Registers.
BX holds Offset Address for Data Segment.
BP holds Offset Address for Stack Segment.
4) SI and DI are called Index Registers
5) The Segment to be operated is decided by the Base Register and NOT by the Index
Register.
Dear Students,
 Software Model, also called Programmers Model Means all the registers available to
the programmer. So if they ask Programmers Model, draw the above diagram and explain
all the registers from the architecture answer. It must include:
 all GPRs: AX, BX, CX, DX.
 Segment Registers: CS, SS, DS, ES.
 All Offset Registers: IP, SP, BP, SI, DI and Flag Register.
 Additionally, give a very brief note on segmentation.

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 AH, NUM1
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 R8 AL AX (16-bit result)
MUL R 16 R 16 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/R (Remainder AH) & (Q AL)
DIV R16 DX:AX/R16 (R DX) & (Q 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
INC NUM1

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  Shifts bit in true direction and fills zero in vacant place
 SHL -logical shift left
 SHR - logical shift right
E.g. SHL reg/mem,
SHL 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

Operand1 Operand 2 CF SF ZF

0 0 0

0 0 1

1 1 0

TEST: Logical Compare Instruction


 The TEST instruction performs a bit by bit logical AND operation on the two operands.
Each bit of the result is then set to 1, if the corresponding bits of both operands are1, else
the result bit is rest to 0.
 The result of this and operation is not available for further use, but flags are affected.
 The affected flags are OF, CF, ZF and PF.
 The operands may be register, memory or immediate data.
 Syntax: TEST reg/mem, reg/mem/immediate

i. TEST mem/reg1, mem/reg2 ;[mem/reg1]  [mem/reg2]

Ex: Test CX, BX


ii. TEST mem/reg, data ;[mem/reg]  data

Ex: TEST CH, 03H

iii. TEST A, data ;[A]  data

A: AL/AX

Ex: TEST AX, 1301H

3) Data Transfer Instructions:

MOV reg./mem, reg./mem. /immediate


Ex: MOV BX, 0210H

MOV AL, BL

MOV [SI], [BX] ----> is not valid

 Memory uses DS as segment register. No memory to memory operation is allowed. It won’t


affect flag bits in the flag register.

Ex: Load DS with 5000H

1) MOV DS, 5000H; Not permitted (invalid)

Thus to transfer an immediate data into the segment register, the convert procedure is
given below:

2) MOV AX, 5000H


MOV DS, AX

Note: Both the source and destination operands cannot be memory locations (Except for
string instructions)

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: Input the port:
 This instruction is used for reading an input port.
 The address of the input port may be specified in the instruction directly or indirectly
AL and AX are the allowed destinations for 8 and 16-bit input operations.
 DX is the only register (implicit), which is allowed to carry the port address.
IN AL, DX ; DX: Port address,
; [AL] <---- [PORT DX]

IN AL, 0300H
IN AX; This instruction reads data from a 16-bit port whose address is in DX (implicit) and
stores it in AX.
OUT: Output to the Port:
 This instruction is used for writing to an output port.
 The address of the output port may be specified in the instruction directly or
implicitly in DX.
 Contents of AX or AL are transferred to a directly or indirectly addressed port after
execution of this instruction. The data to an odd addressed port is transferred on D8
–D15 while that to an even addressed port is transferred on D0-D7.
 The registers AL and AX are the allowed source operands for 8- bit and 16-bit
operations respectively.

OUT DX, AL ;[PORT DX] <---- [AL]

OUT DX, AX ;[PORT DX] <-----[AX]

OUT AX; This sends data available in AX to a port whose address is specified implicitly in
DX.

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

Ex. PUSH AX PUSH DS PUSH [5000]

POP reg16

Ex. POP AX POP DS POP [5000]

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


LOOP: loop unconditionally until complete
Ex: MOV CX,0005H ; Number of times in CX

MOV BX, 0FF7H; Data to BX

Label MOV AX, CODE1

OR BX, AX

AND DX, AX

LOOP Label

The execution proceeds in sequence, after the loop is executed, CX number of times. IF CX is already
00H, the execution continues sequentially. No flags are affected by this instruction.
LOOPE: Loop while equal
LOOPZ: loop while zero
LOOPNE: loop while not equal
LOOPNZ: loop while not zero
7. Branching instruction
a) Conditional
JA: Jump if Above
JAE: Jump if above/equal
JB: Jump if below
JBE: Jump if below/equal
JC: Jump if carry
JNC: Jump if no carry
JE: Jump if equal
JNE: Jump if no equal
JZ: Jump if zero
JNZ: Jump if no zero
JG: Jump if greater
JNG: Jump if no greater
JL: Jump if less
JNL: Jump if no less
JO: jump if overflow
JS: Jump if sign
JNS: Jump if no sign
JP: jump if plus
JPE: Jump if parity even
JNP: Jump if no parity
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, String Byte or String Word:
DS: SI source
DS: DI destination
CX: String length

 The MOVSB/MOVSW instruction thus, moves a string of bytes/words pointed to by DS:SI


pair (source) to the memory location pointed to by ES: DI pair (destination)
 After the MOVS instruction is executed once, the index registers are automatically updated
and CX is decremented.
 The incrementing or decrementing of the pointers, i.e. SI and DI depend upon the direction
flag DF. If DF is 0, the index registers are incremented, otherwise, they are decremented,
in case of all string manipulation instructions.

b) CMPS/ CMPSB/CMPW ; Compare string

Ex: MOV AX, SEG1 ; Segment address of String1, i.e. SEG1 is moved to AX.

MOV DS, AX ; Load it to DS.


MOV AX, SEG2 ; segment address of STRING2, i.e. SEG@ is moved to AX.
MOV ES, AX ; Load it to ES.
MOV SI, OFFSET STRING1 ; Offset of STRING1 Is moved to SI.
MOV DI, OFFSET STRING2 ; Offset of string2 is moved to DI.
MOV CX, 0110H ; Length of string is moved to CX.
CLD ; clear DF, i.e. set auto increment mode.

REPE CMPSW ; Compare 010H words of STRING1 And STRING2, while


they are equal, IF a mismatch is found, modify the flags and
proceed with further execution.
If both strings are completely equal, i.e. CX becomes zero, the ZF is set, otherwise ZF is reset.

c) LODS /LODSB/LODW ; Load string Byte or String word:


; Store String Byte or String Word
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.
MOV AL HIGH Num ; ( [AL] 13 )

7) OFFSET: returns offset address of a variable


8) SEG: returns segment address of a variable
9) PTR: The PTR operator is used to declare the type of a label, variable or memory operator.
E.g. MOV AL, BYTE PTR [SI]
MOV BX, WORD PTR [2000H]

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
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).

Assembly language features:


The main features of ALP are program comments, reserved words, identifies, statements and directives which
provide the basic rules and framework for the language.

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:
- Certain names in assembly language are reserved for their own purpose to be used only under special
conditions and includes
- 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
 Identifiers operation operand comment Directive: COUNT DB 1; initialize count

Instruction: L30: MOV AX, 0 ; assign AX with 0


ASSEMBLER Directives:
 Assembler directives are the commands to the assembler that direct the assembly process.
 The Assembler 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 and title directives help to control the format of a listing of an assembled program.
 This is their only purpose and they have no effect on subsequent execution of the program.
o The page directive defines the maximum number of lines to list as a page and the maximum
number of characters as a line.
PAGE [Length] [Width]
Default : PAGE [50][80]
o TITLE gives title and place the title on second line of each page of the program.
TITLE text [comment]
2.SEGMENT directive
 The segment directive marks the starting of a logical segment
 It gives the start of a segment for stack, data and code.
SEG-NAME SEGMENT
………
SEG-NAME ENDS
Ex: CODE SEGMENT
:
CODE ENDS
 Segment name must be present, must be unique and must follow assembly language naming
conventions.
 An ENDS statement indicates the end of the segment.

3) PROC Directives
 The PROC directive marks the start of a named procedure in the statement.
 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
 Ex: RESULT PROC NEAR
…….
RESULT ENDP
- FAR is used for the first executing procedure and rest procedures call will be NEAR.
- Procedure should be within segment.

4) END and ENDS 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.
 Ex: Procedure Start
:
Start ENDP
5) ASSUME Directive
 The ASSUME directive is used to inform the assembler the names of the logical segments to be
assumed for different segments used in the program .
 Syntax: ASSUME segreg: segname….…, segreg:segname
 Ex: ASSUME CS: CODE
ASSUME CS: CODE, DS:DATA, SS: STACK
6) Processor directive
 Most assemblers assume that the source program is to run on a basic 8086 level computer.
 Processor directive is used to notify the assembler that the instructions or features introduced by the
other processors are used in the program. E.g. .386 - program for 386 protected mode.
7). Dn Directive (Defining data types)
 Assembly language has directives to define data
 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
 Syntax: name Dn expression
 The DB directive is used to reserve byte or bytes of memory locations in the available memory.
Syntax: Name of variable DB initialization value.
Ex: MARKS DB 35H,30H,35H,40H
NAME DB “VARDHAMAN”
Ex: VAL1 DB 21H
ARR DB 25H, 23H, 27H, 53H
MOV AL, ARR [2] or MOV AL, ARR + 2 ; Moves 27 to AL register
8). The EQU directive
- The directive EQU is used to assign a label with a value or symbol.
i.e. It can be used to assign a name to constants.
- E.g. FACTOR EQU 12
MOV BX, FACTOR ; MOV BX, 12
- It is short form of equivalent.
- Do not generate any data storage; instead the assembler uses the defined value to substitute in.
9) DUP Directive
 It can be used to initialize several locations to zero.
e. g. SUM DW 4 DUP (0)
 Reserves four words starting at the offset sum in DS and initializes them to Zero.

- Also used to reserve several locations that need not be initialized. In this case (?) is used with DUP
directives.
E. g. PRICE DB 100 DUP(?)
- Reserves 100 bytes of uninitialized data space to an offset PRICE.
Program written in Conventional full segment directive
Page 60,132
TITLE SUM program to add two numbers
;-----------------------------------------------------
STACK SEGMENT
DW 32 DUP (0)
STACK ENDS
;----------------------------------------------------
DATA SEGMENT
NUM1 DW 3291H
NUM 2 DW 582H
SUM DW ?
DATA ENDS
;------------------------------------------------------
CODE SEGMENT
MAIN PROC FAR
ASSUME SS: STACK-SEG, DS: DATA-SEG, CS: CODE-SEG
MOV AX, @DATA
MOV DS, AX
MOV AX, NUM1
ADD AX, NUM2
MOV AX, 4C00H
INT 21H
MAIN ENDP
CODESEG ENDS
END MAIN
Description for conventional program:

- STACK contains one entry, DW (define word), that defines 32 words initialized to zero, an adequate
size for small programs.
- DATA-SEG defines 3 words NUM1, NUM2 initialized with 3291H and 582H and sum uninitialized.
- CODE-SEG contains the executable instructions for the program, PROC and ASSUME generate no
executable code.
- The ASSUME directive tells the assembler to perform these tasks.
- Assign STACK to SS register so that the processor uses the address in SS for addressing STACK.
- Assign DATA-SEG to DS register so that the processor uses the address in DS for addressing DATA-
SEG.
- Assign CODE-SEG to the CS register so that the processor uses the address in CS for addressing
CODE-SEG. When the loading a program for disk into memory for execution, the program loader sets
the correct segment addresses in SS and CS.

Program written using simplified segment directives:


. Model memory model
 Memory model can be: TINY, SMALL, MEDIUM, COMPACT, LARGE, HUGE or FLAT
 TINY for .com program
 FLAT for program up to 4 GB

- Assume is automatically generated


. STACK [size in bytes] ==== Creates stack segment
. DATA: start of data segment
. CODE: start of code segment

- DS register can be initialized as


MOV AX, @DATA
MOV DS, AX
ALP written in simplified segment directives:

Page 60, 132


TITLE Sum program to add two numbers.
. MODEL SMALL
. STACK 64
.DATA
NUM1 DW 3241H
NUM 2 DW 0572H
SUM DW ?
. CODE
MAIN PROC FAR
MOV AX, @ DATA ; Set address of data segment in DS
MOV DS, AX
MOV AX, NUM1
ADD AX, NUM2
MOV SUM, AX
MOV AX, 4C00H ; End processing
INT 21H
MAIN ENDP ; End of procedure
END MAIN ; End of program

DOS Debug(TASM)

1) Save the code text in .ASM format and save it to the same folder where masm and link files
are stored.
2) Open dos mode and reach within that folder.
3) \>tasm filename.asm makes.obj
4) \>tlink filename makes .exe
5) \>filename.exe run the code
6) \>td filename.exe debug the code [use F7 and F8]

Assembling, Linking and Executing 1)


Assembling:

- Assembling converts source program into object program if syntactically correct and generates an
intermediate .obj file or module.
- It calculates the offset address for every data item in data segment and every instruction in code
segment.
- A header is created which contains the incomplete address in front of the generated obj module during
the assembling.
- Assembler complains about the syntax error if any and does not generate the object module.
- Assembler creates .obj .lst and .crf files and last two are optional files that can be created at run time.
- For short programs, assembling can be done manually where the programmer translates each mnemonic
into the machine language using lookup table.
- Assembler reads each assembly instruction of a program as ASCII character and translates them into
respective machine code.

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 references only.

- The jump instruction uses an address that appears later in the program during scan, for that case the
programmer defines such addresses after the program is assembled.

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.
- 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.
- It completes the address left by the assembler.
It combines separately assembled object files.

- Linking creates .EXE, .LIB,. MAP files among which last two are optional files.
3) Loading and Executing:

- It Loads the program in memory for execution.


- It resolves remaining address.
- This process creates the program segment prefix (PSP) before loading.
- It executes to generate the result.

Sample program assembling object Program linking executable program


Writing .COM programs:
- It fits for memory resident programs.
- Code size limited to 64K.
- .com combines PSP, CS, DS in the same segment
- SP is kept at the end of the segment (FFFF), if 64k is not enough, DOS Places stack at the end of
the memory.
- The advantage of .com program is that they are smaller than .exe program.
- A program written as .com requires ORG 100H immediately following the code segment’s
SEGMENT statement. The statement sets the offset address to the beginning of execution following
the PSP.
.MODEL TINY
.CODE
ORG 100H ; start at end of PSP
BEGIN: JMP MAIN ;Jump Past data
VAL1 DW 5491H
VAL2 DW 0372H
SUM DW ?
MAIN: PROC NEAR
MOV Ax, VALL
ADD AX, VAL2
MOV SUM, AX
MOV AX, 4C00H
INT 21H
MAIN ENDP
END BEGIN

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:
o To simplify and reduce the amount of repetitive coding.
o To reduce errors caused by repetitive coding. o To make an assembly language program
more readable. o Macro executes faster because there is no need to call and return.
o Basic format of macro definition:
Macro name MACRO [Parameter list] ; Define macro
……………………….
……………………….
[Instructions] ; Macro body
……………………….
……………………….
ENDM ; End of macro

E.g. Addition MACRO


IN AX, PORT
ADD AX, BX
OUT PORT, AX
ENDM
Passing argument to MACRO:
- To make a macro more flexible, we can define parameters as dummy argument

Addition MACRO VALL1, VAL2


MOV AX, VAL1
ADD AX, VAL2
MOV SUM, AX
ENDM
. MODEL SMALL
. STACK 64
.DATA
VAL1 DW 3241H
VAL2 DW 0571H
SUM DW ?
. CODE
MAIN PROC FAR
MOV AX, @ DATA
MOV DS, AX
Addition VAL1, VAL2
MOV AX, 4C00H
INT 21H
MAIN ENDP
END MAIN
TITLE Program to add ten numbers
. MODEL SMALL
. STACK 64
.DATA
ARR DB 73, 91, 12, 15, 79, 94, 55, 89 SUM DW ?
. CODE
MAIN PROC FAR
MOV AX, @DATA
MOV DS, AX
MOV CX, 10
MOV AX, 0
LEA BX, ARR
L2: ADD Al, [BX]
JNC L1
INC AH
L1: INC BX
LOOP L2
MOV SUM, AX
MOV AX, 4C00H
INT 21H
MAIN ENDP
END MAIN
DOS FUNCTIONS AND INTERRUPTS
(KEYBOARD AND VIDEO PROCESSING)
 The Intel CPU recognizes two types of interrupts namely hardware interrupt when a peripheral
devices needs attention from the CPU and software interrupt that is call to a subroutine located in
the operating system. 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:
# 00H- It terminates the current program.
- Generally, not used, function 4CH is used instead.
# 01H- Read a character with echo
- Wait for a character if buffer is empty
- Character read is returned in AL in ASCII value
# 02H- Display single character

- Sends the characters in DL to display


- MOV AH, 02H
- MOV DL, ‘A’ ; move Dl, 65
- INT 21H
# 03H and 04H – Auxiliary input/output -
INT 14H is preferred.
# 05H – Printer service

- Sends the character in DL to printer


# 06H- Direct keyboard and display - Displays the character in DL.
# 07H- waits for a character from standard input
- does not echo
# 08H- keyboard input without echo - Same as function 01H but not echoed.
# 09H- string display
- Displays string until ‘$’ is reached.
- DX should have the address of the string to be displayed.
# 0AH – Read string
# OBH- Check keyboard status
- Returns FF in AL if input character is available in keyboard buffer. - Returns 00 if not.
# 0CH- Clear keyboard buffer and invoke input functions such as 01, 06, 07, 08 or 0A. - AL
will contain the input function.
INT 21H Detailed for Useful Functions

# 01H
MOV, AH 01H; request keyboard input INT 21H

- Returns character in AL. IF AL= nonzero value, operation echoes on the screen. If Al= zero means
that user has pressed an extended function key such as F1 OR home.
# 02H
MOV AH, 02H; request display character
MOV DL, CHAR; character to display
INT 21H

- Display character in D2 at current cursor position. The tab, carriage return and line feed characters
act normally and the operation automatically advances the cursor.

# 09H
MOV Ah, 09H; request display
LEA DX, CUST_MSG; local address of prompt
INNT 21H
CUST_MSG DB “Hello world”, ‘$’

- Displays string in the data area, immediately followed by a dollar sign ($ or 24H), which uses to
end the display.

# OAH
MOV AH, 0AH ; request keyboard input
LEA DX, PARA_ LIST ; load address of parameter list
INT 21H

Parameter list for keyboard input area :


PARA_LIST LABEL BYTE; start of parameter list
MAX_LEN DB 20; max. no. of input character
ACT _ LEN DB ? ; actual no of input characters
KB-DATA DB 20 DUP (‘); characters entered from keyboard

- LABEL directive tells the assembler to align on a byte boundary and gives location the name PARA
_LIST.
- PARA_LIST & MAX_LEN refer same memory location, MAX_LEN defines the maximum no of
defined characters.
- ACT_LEN provides a space for the operation to insert the actual no of characters entered.
- KB_DATA reserves spaces (here 20) for the characters.

Example:
TITLE to display a string
.MODEL SMALL
.STACK 64
.DATA
STR DB ‘programming is fun’, ‘$’
.CODE
MAIN PROC FAR
MOV AX, @DATA
MOV DS, AX
MOV AH, 09H ;display string
LEA DX, STR
INT 21H
MOV AX, 4C00H
INT 21H
MAIN ENDP
END MAIN

INT 10H
It is called video display control. It controls the screen format, color, text style, making windows,
scrolling etc. The control functions are:
# 00H – set video mode
MOV AH, 00H ; set mode
MOV AL, 03H ; standard color text
INT 10H ; call interrupt service

# 01H- set cursor size


MOV AH, 01H
MOV CH, 00H ; Start scan line
MOV CL, 14H ; End scan line
INT 10H ; (Default size 13:14)

# 02H – Set cursor position:


MOV AH, 02H
MOV BH, 00H ; page no
MOV DH, 12H ; row/y (12)
MOV DL, 30H ; column/x (30)
INT 10H

# 03H – return cursor status


MOV AH, 03H
MOV BH, 00H;
INT 10H
Returns: CH- starting scan line, CL-end scan line, DH- row, DL-column

# 04H- light pen function

# 05H- select active page


MOV AH, 05H
MOV AL,page-no. ; page number
INT 10H
# 06H- scroll up screen
MOV AX, 060FH ; request scroll up one line (text)
MOV BH, 61H ; brown background, blue foreground
MOV CX, 0000H ; from 00:00 through
MOV DX, 184F H ; to 24:79 (full screen)
INT 10H
AL= number of rows (00 for full screen)
BH= Attribute or pixel value
CX= starting row: column
DX= ending row: column

# 07H-Scroll down screen


Same as 06H except for down scroll

# 08H (Read character and Attribute at cursor) MOV


AH, 08H
MOV BH, 00H ; page number 0(normal)
INT 10H
AL= character
BH= Attribute

# 09H -display character and attribute at cursor


MOV AH, 09H
MOV AL, 01H ; ASCII for happy face display
MOV BH, 00H ; page number
MOV BL, 16H ; Blue background, brown foreground
MOV CX, 60 ; No of repeated character
INT 10H

# 0AH-display character at cursor


MOV AH, 0AH
MOV Al, Char
MOV BH, page _no
MOV BL, value
MOV CX, repetition
INT 10H

# 0BH- Set color palette


 Sets the color palette in graphics mode
 Value in BH (00 or 01) determines purpose of BL
 BH= 00H, select background color, BL contains 00 to 0FH (16 colors)
 BH = 01H , select palette, Bl, contains palette
MOV AH, 0BH
MOV AH, 0BH
MOV BH, 00H; background MOV BH, 01H ; select palette
MOV BL, 04H; red MOV BL, 00H ; black
INT 21H INT 21H

#0CH- write pixel Dot

- Display a selected color


AL=color of the pixel CX= column
BH=page number DX= row

MOV AH, 0CH


MOV Al, 03
MOV BH,0
MOV CX, 200
MOV DX, 50
INT 10H
It sets pixel at column 200, row 50

#0DH- Read pixel dot

- Reads a dot to determine its color value which returns in AL


MOV AH, 0DH
MOV BH, 0 ; page no
MOV CX, 80 ; column
MOV DX, 110 ; row
INT 10H

#OEH- Display in teletype mode

- Use the monitor as a terminal for simple display


MOV AH, 0EH
MOV AL, char
MOV BL, color; foreground color
INT 10H

#OF H- Get current video mode


Returns values from the BIOS video .
AL= current video mode MOV AH, 0FH
AH= no of screen columns INT 10H
BH = active video page

TITLE To Convert letters into lower case


.MODEL SMALL
.STACK 99H
.CODE
MAIN PROC
MOV AX, @ DATA
MOV DS, AX
MOV SI, OFFSER
STR
M: MOV DL, [SI]
MOV CL, DL
CMP DL, ‘ $’
JE N
CMP DL, 60H
JL L
K: MOV DL, CL
MOV AH, 02H
INT 21H
INC SI
JMP M
L: MOV DL, CL
ADD DL, 20H
MOV AH, 02H
INT 21H
INC SI
JMP M
N: MOV AX, 4C00H
INT 21H
MAIN ENDP
.DATA
STR DB ‘I am MR Rahul “, ‘$’
END MAIN

TITLE to reverse the string


.MODEL SMALL
.STACK 100H
.DATA
STR1 DB “ My name is Rahul” , ‘$’
STR2 db 50 dup (‘$’)
.CODE
MAIN PROC FAR
MOV BL,00H
MOV AX, @ DATA
MOV DS, AX
MOV SI, OFFSER STR1
MOV DI, OFFSET
STR2
L2: MOV DL, [SI]
CMP Dl, ‘$’
JE L1
INC SI
INC BL
JMP L2
L1: MOV CL, BL
MOV CH, 00H
DEC SI
L3: MOV AL, [SI]
MOV [DI], AL
DEC SI
INC DI
LOOP L3
MOV AH,09H
MOV DX, OFFSET
STR2
INT 21H
MOV AX, 4C00H
INT 21H
MAIN ENDP
END MAIN

TITLE to input characters until ‘q’ and display


.MODEL SMALL
.STACK 100H
.DATA
STR db 50 DUP (‘$’)
.CODE
MAIN PROC FAR
MOV AX, @ DATA
MOV DS, AX
MOV SI, OFFSET STR
L2: MOV AH, 01H
INT 21H
CMP AL, ‘q’
JE L1
MOV [SI] , AL
INC SI
JMP L2
L1: MOV AH, 09H
MOV DX, OFFSET
STR
INT 21H
MOV AX, 4C00H
INT 21H
MAIN ENDP
END MAIN

Calling procedure/subroutine

Procname PROC FAR


……………………………..
……………………………..
Procname ENDP

Here the code segment consists only one procedure. The FAR operand in this case
informs the assembler and linker that the defined procedure name is the entry point for
program execution, whereas the ENDP directive defines the end of the procedure. A code
segment however, may contain any number of procedures, each distinguished by its own
PROC and ENDP directives.
A called procedure is a section of code that performs a clearly defined task known as
subroutine which provides following benefits.
• Reduces the amount of code because a common procedure can be called from any
number of places in the code segment.
• Encourage better program organization.
• Facilitates debugging of a program because defects can be more clearly isolated.
• Helps in the ongoing maintenance of programs because procedures are readily
identified for modification.
A CALL to a procedure within the same code segment is NEAR CALL<. A FAR CALL calls a
procedure labeled FAR, possibly in another code segment.

DISPLAY PROC NEAR


MOV AH, 09H
MOV DX, OFFSET STR
INT 21H
RET
DISPLAY ENDP

To display number contained in [BX]


DISPLAY PROC NEAR
MOV Dl, [BX]
ADD Dl, 30
MOV AH, 02H
INT 21H
RET
DISPLAY ENDP

INT 10H Video service:

<Video –modes>

Text mode Color No.of Resolution colors


Row column
Pages
00 Color 8 16 colors
25 40 360 400
01 Color 8 16 colors
25 40 360 400
02 Color 4 16 colors
25 80 720 400
03(by default) color 4 16 colors
25 80 720 400

07 Mono-hrome 0 16 colors
25 80 720 400
Graphic mode Color Pages Resolution No of colors
04 Color 8 4
320 200
05 Color 8 4
320 200
06 Color 8 2
640 200
0D Color 8 16
320 200
0E Color 4 16
640 200
0F Mono chrome 2 1
640 350
10 Color 2 16
640 350
11 Color 1 2
640 480
12 Color 1 16
640 480
13 Color 1 256
320 200

Attribute
Background Foreground
Attribute: BL R G B I RG B
Bit number: 7 6 54 32 1 0
I – Intensity, BL - Blink

Color Hex Value


Black 0
Blue 1
Green 2
Cyan 3
Red 4
Magnet 5
Brown 6
White 7
Gray 8
Light Blue 9
Light Green A
Light cyan B
Light red C
Light magenta D
Yellow E
Bright white F
TITLE sorting the numbers – descending order
DOSSEG
. MODEL SMALL
. STACK 100H
. CODE
MAIN PROC FAR
MOV AX, @ DATA
MOV DS, AX
MOV DX, 4H
DOPASS: MOV CX, 4H
MOV SI, 00H
CHECK: MOV AL, ARR [SI]
CMP ARP [SI+1] , AL
JC NOSWAP
MOV BL, ARR [SI + 1]
MOV ARR[SI +1] , AL
MOV ARR [SI], BL
NOSWAP: INC SI
LOOP CHECK
DEC DX
JNZ DOPASS
MOV AX, 4C00H
INT 21H
MAIN ENDP
.DATA
ARR DB 8,2,9,4,7
END MAIN

Note: Display if numbers are with 1 digit


MOV CX, 05H
MOV SI, 00H
L: MOV DL, ARR[SI]
ADD DL, 30H
MOV AH, 02H
INT 21H
MOV DL,’ ’
MOV AH, 02H
INT 21H
INC SI
LOOP L
MOV AX, 4C00H
INT 21H
MAIN ENDP

TITLE addition of 100 natural even numbers


.MODEL SMALL
.STACK 100H
.DATA
TEN DW 10
.CODE
MAIN PROC FAR
MOV AX, @ DATA
MOV DS, AX
MOV CX, 63H
MOV AX, 02H
MOV DX, 04H
L1: ADD AX, DX
ADD DX, 02H
LOOP L1
L2: MOV DX, 0000H
DIV TEN ; DX: AX /10
INC CX
ADD DX, 30H ; remainder
PUSH DX
CMP AX, 00H ; quotient
JE L3
JMP L2
L3: POP DX
MOV AH, 02H
INT 21H
LOOP L3
MOV AX, 4C00H
INT 21H
MAIN ENDP
END MAIN

TITLE to display string at (10,40) with green background and red foreground dosseg .Model small
.Stack 100H
.Code
MAIN PROC FAR
MOV AX, @ DATA
MOV DX, AX
MOV SI, OFFSET VAR1
L2: MOV AH, 02H ; Set cursor position
MOV DH, ROW
MOV DL, COL
INT 10H
MOV AL, [SI]
CMP AL, ‘$’
JE L1
MOV AH, 09H
MOV DH, ROW
MOV DL, COL
MOV BL, 24H ;background & foreground
MOV BH, 00h ; page
MOV CX, 01H ; no. of repeated characters
INT 10H
INC SI
INC COL
JMP L2
L1: MOV AX, 4C00H
INT 21H
MAIN ENDP
.DATA
ROW DB 10
COL DB 40
VAR1 DB “video model”, ‘$’
END MAIN
TITLE TO GENERATE MULTIPLICATION TABLE
.MODEL SMALL
.STACK 32
.DATA
NUM1 DB 5
NUM2 DB 1
TAB DB 10 DUP (?)
.CODE
MAIN PROC FAR
MOV AX, @ DATA
MOV DS, AX
MOV BX, 0
MOV CX, 10
L1: MOV AL, NUM1
MUL NUM2
MOV TAB [BX], AL
INC BX
INC NUM2
LOOP L1
MOV AX, 4C00H
INT 21H
MAIN ENDP
END MAIN
TITLE to add 10 sixteen bit Numbers in memory table
.MODEL SMALL
.STACK 32
.DATA
NUM DW DUP (2)
NUM DW DUP (3)
SUMH DW 0
SUML DW 0
Note: To access the data of the
.CODE memory i.e. table.
We use e.g. NUM[BX]
MAIN PROC FAR Increasing the BX register by 2
MOV AX, @ DATA
MOV DS, AX
MOV CX, 10
MOV AX, 0
MOV BX, 0
L1: ADD AX, NUM
[BX]
MOV SUML, AX
JNC L2
INC SUMH
L2: ADD BX, 2
LOOP L1
MOV AX, 4C00H
INT 21H
MAIN ENDP
END MAIN
SUBROUTINE TO CLEAR THE SCREEN
SCR_CLEAR PROC NEAR
MOV AX, 0600H ; Request scroll
MOV BH, 61H ; blue on brown for attribute on pixel(generally (07H) white on black
MOV CX, 0000 ; Full screen
MOV DX, 184FH
INT 10H
RET
SCR_CLEAR ENDP
AH-06h: Scroll upward of lines in a specified area of the screen.
AL- 00H caused entire screen to scroll up, effectively clearing it. Setting a nonzero value in AL
causes the number of lines to scroll up.

Differentiate between
MIN MODE MAX MODE
It is a multiprocessor mode. Along
It is a uniprocessor mode. 8086 is
1 with 8086, there can be other processors
the only processor in the circuit.
like 8087 and 8089 in the circuit.

2
Here MN/ MX is connected to Vcc. Here MN/ MX is connected to Ground.
ALE for the latch is given by 8086 As there are multiple processors, ALE for the
3
itself. latch is given by 8288 bus controller.
As there are multiple processors, DEN
DEN and DT/ R for the
4
transreceivers are given by 8086 and DT/ R for the transreceivers is given by
itself. 8288 bus controller.

Direct control signals like M/ IO ,


Instead of control signals, all processors
5
produce status signals S2 , S1 and S0
RD and WR are produced by
8086 itself.

Control signals M/ IO , RD and


6 Status signals S2 , S1 and S0 require special
WR are decocded by a 3:8 decoder decoding are decoded by 8288 bus controller.
IC 74138.

7 INTA for interrupt acknowledgement INTA for interrupt acknowledgement is


is produced by 8086. produced by 8288 Bus Controller.

Bus request are grant is handled using


Bus request are grant is handled using
8
HOLD and HLDA signals.
RQ / GT signals.

Since 74138 does not independently Since 8288 independently generates control
9 generate any signals, it does not need a signals, it needs a CLK from 8284 clock
CLK. generator.

The circuit is simpler but does not The circuit is more complex but supports
10
support multiprocessing. multiprocessing.
Differentiate between
8085 8086

8-bit processor with: 8-bit 16-bit processor with: 16-bit ALU


1 ALU and and
8-bit data bus. 16-bit data bus.

2 Memory banking not needed. Memory is divided into two banks.

3 16-bit address bus. 20-bit address bus.

4 Accesses 64 KB Memory. Accesses 1 MB Memory.

5 Segmentation not performed. Segmentation is performed.

6 Has 5 status flags. Has 6 status flags and 3 control flags.

7 Pipelining is not performed. 2 stage Pipelining is performed.

8 Has 5 hardware interrupts. Has 2 hardware interrupts.

9 Does not support multiprocessing. Supports multiprocessing in Max Mode.

ALU cannot perform powerful arithmetic ALU can perform powerful arithmetic like
10
like MUL and DIV. MUL and DIV.
Differentiate between
8088 8086

16-bit processor with: 16-bit processor with: 16-bit


1 16-bit ALU and 8-bit ALU and
data bus. 16-bit data bus.

Memory banking not needed. Hence Memory is divided into two banks. Hence
2
circuit is simpler. circuit is more complex.

Since data bus is 8-bits, it can transfer 1 Since data bus is 16-bits, it can transfer 2
3 byte in 1 cycle. bytes in 1 cycle.
Hence is slower. Hence is faster.

BHE is not needed.


4
Instead, has a signal called SSO used for BHE is needed to enable the higher bank.
Single Stepping.

5 Prefetch queue is of 4 bytes. Prefetch queue is of 6 bytes.

6 Uses M/ IO to differentiate between


Uses IO/ M compatible with 8085.
memory and I/O operations.

You might also like