Instruction 8086 Senthil
Instruction 8086 Senthil
ADDRESSING MODES,
INSTRUCTION SET, AND
PROGRAMMING OF 8086
Example:
(a) MOV AL, BL ; Move the content of BL to AL.
(b) MOV CX, BX ; Move the content of BX to CX.
(c) ADD CL, BL ; Add the contents of CL and BL and store the
result in CL.
(d) ADC BX, DX ; Add the contents of BX , the carry flag, and
DX, and store the result in BX.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 4 33
In the last example, [SI] represents the memory location in the data segment at
the offset address specified by the SI register.
Register CS DS SS ES BX BP SI DI
Stored value 1000H 3000H 4000H 6000H 2000H 1000H 1000H 3OOOH
Example:
(a)MOV CL, [BX]
EA = (BX) = 2000H
Memory address = DS x 10 4- (BX) = 32OOOH. The byte from the memory address
32OOOH is read and stored in CL.
434 MICROPROCESSORS AND MICROCONTROLLERS
Example:
(a)MOV BL, [SI]
EA = (SI) = 1000H
Memory address = DS x 10H + SI
= 30000H + 1000H = 31000H
A byte from the memory address 31000H is read and stored in BL.
(b)MOV CX, [DI]
EA = (DI) = 3000H
Memory address = DS x 10H + (DI)
= 30000H + 3000H = 33000H
A word from the memory address 33OOOH is read and stored in CX.
(v) Index relative addressing: This mode is the same as the base relative addressing
mode, except that instead of the BP or BX register, the SI or DI register is used.
Example:
(a)MOV BX, [SI - 100H]
EA = (SI) - 100H
Memory address = DS x 10H + (SI) - 100H
= 30000H + 1000H - 100H = 30F00H
A word from the memory address 30F00H is read and stored in BX.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 435
Base relative plus index addressing is used to access a byte or a word in a particular
record of a specific file in the memory. An application program may process many
files stored in the data segment. Each file contains many records and a record
contains a few bytes or words of data. In base relative plus index addressing, the
base register may be used to hold the offset address of a particular file in the data
segment; the index register may be used to hold the offset address of a particular
record within that file; the relative value is used to indicate the offset address of
particular byte or word within that record.
where it has to fetch an instruction using the relation CS x 10H + IP, the address
32000H is obtained using the given CS and IP values.
This type of jump is known as intersegment jump, using which the
microprocessor can jump to any memory location within the memory system (i.e.,
within 1 MB). It is also known as far jump. The inter-segment or FAR CALL
instruction also uses direct program memory addressing. While using the assembler
to develop the 8086 program, the assembler directive FAR PTR is sometimes used
to indicate the inter-segment jump instruction.
Example:
(a) JMP FAR PTR COMPUTE
(b) JMP FAR PTR SIMULATE
In these examples, COMPUTE and SIMULATE are the labels of memory
locations that are present in code segments other than the ones in which these
instructions are present.
(ii) Relative addressing: The term relative here means relative to the instruction
pointer (IP). Relative JMP and CALL instructions contain either an 8-bit or a
16-bit signed displacement, which is added to the current instruction pointer.
Based on the new value of IP thus obtained, the address of the next instruction to
be executed is calculated using the relation CS x 10H + IP.
The 8-bit or 16-bit signed displacement allows a forward or a reverse memory
reference, depending on the sign of the displacement. If the displacement is positive,
PC is incremented by the displacement value and if it is negative, PC is decremented
by the magnitude of the displacement value. A one-byte displacement is used in the
short jump and call instructions, and a two-byte displacement is used in the near
jump and call instructions. Both types are considered intrasegmentjumps, since the
program control is transferred anywhere within the cunent code segment.
An 8-bit displacement has a jump range between +127 and -128 bytes from the
next instruction, while a 16-bit displacement has a jump range between -32,768
and +32,767 bytes from the instruction following the jump instruction in the
program. The opcode of the relative short jump and near jump instructions are
EBH and E9H, respectively.
While using an assembler to develop the 8086 program, the assembler
directives SHORT and NEAR PTR are used to indicate the short jump and near
jump instructions, respectively.
Example:
(a) JMP SHORT OVER
(b) JMP NEAR PTR FIND
In these examples, OVER and FIND are the labels of memory locations that
are present in the same code segment in which these instructions are present.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 437
(iii) Indirect addressing: The indirect jump or CALL instructions use a 16-bit
register (AX, BX, CX, DX, SP, BP, SI, or DI), a relative register ([BP], [BX],
[DI], or [SI]), or a relative register with displacement. The opcode of the indirect
jump instruction is FFH. It can be either an inter-segment indirect jump or an
intra-segment indirect jump.
If a 16-bit register holds the jump address in an indirect JMP instruction, the
operation is a near jump. If the CX register contains 2000H and the JMP CX
instruction present in a code segment is executed, the microprocessor jumps to the
offset address 2000H in the current code segment to take the next instruction for
execution (this is done by loading the IP with the content of CX, without changing
the content of CS).
When the instruction JMP [DI] is executed, the microprocessor first reads a
word in the current data segment from the offset address specified by DI and
places that word in the IP register. Now, with this new value of IP, the 8086
calculates the address of the memory location to which it has to jump, using the
relation CS x 10H + IP.
Example:
Let us assume that the registers DS, DI, and CS have the values 1000H, 2000H,
and 3000H, respectively. When JMP [DI], present at the offset address 1500H
in the code segment 3000H is executed, the microprocessor reads a word from
the address given by DS x 10H + DI (= 12000H) in the memory, and loads
it in the instruction pointer (IP). Let us assume that the word that is stored
in the address 12000H is 4000H. Hence, the program counter will be loaded
with the value 4000H. Now, the microprocessor fetches the next instruction for
execution from the address given by CS x 10H + IP (= 3000H x 10H + 4000H
= 34000H).
Since SP gets decremented for every push operation, the stack segment is said
to be growing downwards, as for successive push operations, data are stored in the
lower memory addresses in the stack segment. Due to this, SP is initialized with
the highest offset address, according to the user’s requirement, at the beginning of
the program.
Example:
(a) PUSH AX ; Push the content of AX into the stack.
(b)PUSH DS ; Push the content of DS into the stack.
(c) PUSH [BX] ; Push the content of the memory location at
the offset address specified by BX in the
current data segment, into the stack.
The PUSHF instruction is used to push the flag register’s content into the
stack.
Whenever a word is popped from the stack, the lower-order eight bits of the
word are removed from the memory location specified by SP and the higher-order
eight bits of the word are removed from the memory location specified by SP + 1
in the current stack segment. SP is then incremented by two.
Example:
(a) POP BX : Pop the content of BX from the stack.
(b) POP ES ; Pop the content of ES from the stack.
(c) POP [BP] ; Pop the content of the memory 1 ocation at
the offset address specified by BP in the
current stack segment, from the stack.
The POPF instruction is used to pop a word stored in the stack and move it to
the flag register.
(ii) PUSH: The PUSH instruction is used to store the word in a register or a
memory location into the stack, as explained in the stack addressing mode. SP is
decremented by two after the execution of PUSH.
Example:
(a)PUSH CX : PUSH the content of CX into the stack.
(b)PUSH DS ; PUSH the content of DS into the stack.
(c)PUSH [BX] ; PUSH the word in the memory at [BX] into
the stack.
(iii) POP: The POP instruction copies the top word from the stack to a destination
specified in the instruction. The destination can be a general-purpose register, a
segment register, or a memory location. After the word is copied to the specified
destination, SP is incremented by two.
440 MICROPROCESSORS AND MICROCONTROLLERS
Example:
(a) POP BX ; Pop the content of BX from the stack.
(b)POP DS : Pop the content of DS from the stack.
(c) POP [SI] : Pop a word from the stack and store it in
the memory at [SI],
Note: [SI] indicates the memory location in the data segment at the offset address
specified by SL
(iv) XCHG: The XCHG instruction exchanges the contents of a register with the
contents of a memory location. It cannot exchange the contents of two memory
locations directly. The source and destination must both be either words or bytes.
The segment registers cannot be used in this instruction.
Example:
(a) XCHG AL, BL : Exchanges the content of AL and BL.
(b)XCHG CX, BX : Exchanges the content of CX and BX.
(c) XCHG AX, [BX] ; Exchanges the content of AXwith the content
of the memory at [BX].
(v) XLAT: The XLAT instruction is used to translate a byte in AL from one code
to another code. The instruction replaces a byte in the AL register with a byte in
the memory at [BX], which is one of the data items present in a look-up table.
Before XLAT is executed, the look-up table containing the desired codes must
be put in the data segment and the offset address of the starting location of the
look-up table is stored in BX. The code byte to be translated is put in AL. When
XLAT is executed now, it adds the content of the AL with BX to find the offset
address of the data in the look-up table. Further, the byte in that offset address will
get copied to AL.
(vi) IN: The IN instruction copies data from a port to the AL or AX register. If an
8-bit port is read, the data is stored in AL and if a 16-bit port is read, the data is
stored in AX. The IN instruction has two formats—fixed port and variable port.
In the fixed port type IN instruction, the 8-bit address of a port is specified
directly in the instruction. With this form, any one of 256 possible ports can be
addressed.
Example:
IN AL, 80H : Input a byte from the port with address 80H to AL.
IN AX, 40H ; Input a word from the port with address 40H to AX.
For the variable port type IN instruction, the port address is loaded into the DX
register before the IN instruction. Since DX is a 16-bit register, the port address
can be any number between 0000H and FFFFH. Hence, we will be able to address
up to 65,536 ports in this mode. The following example shows a part of a program
having the IN instruction. The operations done when the instructions are executed
are given in the corresponding comment fields.
Example:
MOV DX, 0FE50H ; Initialize DX with the port address of FE50H.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 441
(viii) LEA (load effective address): The general format of the LEA instruction is
LEA register, source. This instruction determines the offset address of the variable
or memory location called the source and puts this offset address in the indicated
16-bit register.
Example:
(a) LEA BX, COST ; Load BX with the offset address of COST in the
data segment, where COST is the name assigned
to a memory location in the data segment.
(b)LEA CX, [BX] [SI] ; Load CX with the value equal to (BX) + (SI),
where (BX) and (SI) represent the content of
BX and SI, respectively.
(ix) LDS: This instruction loads the register and DS with words from the memory.
The general form of this instruction is LDS register, memory address of first
word.
The LDS instruction copies a word from the memory location specified in the
instruction into the register, and then copies a word from the next memory location
into the DS register. LDS is useful in initializing the SI and DS registers at the start
of a string before using one of the string instructions.
442 MICROPROCESSORS AND MICROCONTROLLERS
Example:
LDS SI, E2000HJ ; Copy the content of the memory at the
offset address 2000H in the data segment to
the lower-order byte of SI, and the content
of 2001H to the higher-order byte of SI. Copy
the content at the offset address 2002H in
the data segment to the lower-order byte of
DS and the content of 2003H to the higher-
order byte of DS.
(x) LES and LSS: The LES and LSS instructions are similar to the LDS instruction,
except that instead of the DS register, the ES and SS registers, respectively, are
loaded, along with the register specified in the instruction.
(xi) LAHF: This instruction copies the lower-order byte of the flag register into
AH.
(xii) SAHF: This instruction stores the content of AH in the lower-order byte of
the flag register.
Except the SAHF and POPF instructions, no other data transfer instruction
affects the flag register.
Example:
(a) ADD BL, 80H : Add the immediate data 80H to BL.
(b)ADD CX, 12B0H ; Add the immediate data 12B0H to CX.
(c) ADD AX, CX ; Add the content of AX and CX and store the
result in AX.
(d)ADD AL, [BX] ; Add the content of AL and the byte from the
memory at [BX] and store the result in AL.
(e) ADD CX, [SI] ; Add the content of CX and the word from the
memory at [SI] and store the result in CX.
(f) ADD [BX] , DL ; Add the content of DL with the byte from the
memory at [BX] and store the result in the
memory at [BX].
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 443
The flags AF, CF, OF, PF, SF, and ZF are affected by the execution of the
ADD instruction.
(ii) ADC: This instruction adds the data in the source and destination with the
content of the carry flag and stores the result in the destination. The general format
of this instruction is ADC destination, source.
All the rules specified for ADD are applicable to the ADC instruction.
(iii) SUB: The general form of the subtract (SUB) instruction is SUB destination,
source. It subtracts the number in the source from the number in the destination
and stores the result in the destination. Like the ADD instruction, the source may
be an immediate number, a register, or a memory location. The destination can be
a register or a memory location. However, the source and destination cannot both
be memory locations. The data from the source and destination must be of the
same type (either bytes or words).
For subtraction, the carry flag (CF) functions as the borrow flag. If the result is
negative after subtraction, CF is set. Otherwise, it is reset. The flags AF, CF, OF,
PF, SF, and ZF are affected by the SUB instruction.
Example:
(a) SUB AL, BL Subtract BL from AL and store the result i n
AL.
(b)SUB CX, BX ; Subtract BX from CX and store the result i n
CX.
(c) SUB BX, [DI] ; Subtract thei word in the memory at [DI]
from BX and store the result in BX.
(d)SUB [BP] , DL ; Subtract DL from the byte in the memory at
[BP] and store the result in the memory at
[BP],
(iv) SBB: Subtract with borrow—The general form of this instruction is SBB
destination, source. The SBB instruction subtracts the content of the source
and the carry flag from the content of the destination and stores the result in the
destination. The rules for the source and the destination are same as that for the
SUB instruction. AF, CF, OF, PF, SF, and ZF are affected by this instruction.
(v) INC: The increment (INC) instruction adds 1 to the content of a specified
register or a memory location. The data incremented may be a byte or word. While
the carry flag is not affected by this instruction, the flags AF, OF, PF, SF, and ZF
are affected.
Example:
(a)INC CL : Increment the content of CL by 1.
(b)INC AX : Increment the content of AX by 1.
(c) INC BYTE PTR [BX] ; Increment the byte in the memory at [BX] by 1.
(d)INC WORD PTR [SI] ; Increment the word in the memory at [SI] by 1.
In these examples, the terms BYTE PTR and WORD PTR are assembler
directives, which are used to specify the type of data (byte or word) to be
incremented in the memory.
444 MICROPROCESSORS AND MICROCONTROLLERS
(vi) DEC: The decrement (DEC) instruction subtracts 1 from the content of a specified
register or memory location. The data decremented may be a byte or a word. CF is
not affected, but AF, OF, PF, SF, and ZF flags are affected by this instruction.
(vii) NEG: The negate (NEG) instruction replaces the byte or word in the specified
register or memory location by its 2’s complement (i.e., changes the sign of the
data). The CF, AF, SF, PF, ZF, and OF flags are affected by this instruction.
Example:
(a)NEG AL ; Take 2’s complement of the data in AL and
store it in AL.
(b)NEG CX ; Take 2’s complement of the data in CX and
store it in CX.
(c) NEG BYTE PTR [BX] ; Take 2’s complement of the byte in the memory
at [BX] and store the result in the same
pl ace.
(d)NEG WORD PTR [SI] ; Take 2’s complement of the word in the memory
at [SI] and store the result in the same place.
(viii) CMP: The general form of the compare (CMP) instruction is CMP
destination, source. This instruction compares a byte or word in the source with a
byte or word in the destination and affects only the flags, according to the result.
The content of the source and destination are not affected by the execution of this
instruction. The comparison is done by subtracting the content of the source from
that of the destination. The AF, OF, SF, ZF, PF, and CF flags are affected by the
instruction. The rules for the source and destination are the same as those for the
SUB instruction.
Example:
After the instruction CMP AX, DX is executed, the status of CF, ZF, and SF will
be as follows:
CF ZF SF
IfAX = DX 0 1 0
IfAX>DX 0 0 0
IfAX<DX 1 0 1
(ix) MUL: The multiply (MUL) instruction is used for multiplying two unsigned
bytes or words. The general form of the MUL instruction is MUL source. The
source can be a byte or a word from a register or memory location, which is
considered as the multiplier. The multiplicand is taken by default from AL and AX
for byte and word type data, respectively. The result of multiplication is stored in
AX and DX-AX (i.e., the most significant word of the result in DX and the least
significant word of the result in AX) for byte and word type data, respectively.
(Note: Multiplying two 8-bit data gives a 16-bit result and multiplying two 16-bit
data gives a 32-bit result.)
Example:
(a)MUL CH : Multiply AL and CH and store the result in
AX.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 445
(xi) DIV: The divide (DIV) instruction is used for dividing unsigned data. The
general form of the DIV instruction is DIV source, where ‘source’ is the divisor.
It can be a byte or word in a register or memory location. The dividend is taken by
default from AX and DX-AX for byte and word type data division, respectively.
Table 14.3 shows the complete details of the DIV instruction.
represented as an 8-bit number in which the upper four bits are always zero. For
example, the decimal digit 3 is represented as 03H in unpacked BCD form.
(xv) AAA: The AAA (ASCII adjust after addition) instruction must always follow
the addition of two unpacked BCD operands in AL. When AAA is executed, the
content of AL is changed to a valid unpacked BCD number; the upper four bits of
AL are cleared. CF is set and AH is incremented if a decimal carry-out from AL
is generated.
Example:
Let AL = 05 (decimal) = 00000101
BH = 06 (decimal) = 00000110
AH = OOH
Consider the execution of the following instructions:
ADD AL, BH ; AL = 11 (decimal) and CF = 0
AAA : AL = 01 and AH = 01 and CF = 1
Addition of 5 and 6 gives a decimal result of 11, which is equal to 0101H in
unpacked BCD form. It is stored in AX. When this result is to be sent to the printer,
the ASCII code of each decimal digit is easily found by adding 30H to each byte.
(xvi) AAS: ASCII adjust after subtraction—This instruction always follows the
subtraction of one unpacked BCD operand from another in AL. It changes the
content of AL to a valid unpacked BCD number and clears the top four bits of AL.
CF is set and AH is decremented if a decimal borrow occurs.
Example:
(a) Let AL = 09 BCD = 00001001
CL = 05 BCD = 00000101
AH = OOH
Consider the execution of the following instructions:
SUB AL, CL ; AL = 04 BCD
AAS ; AL = 04 BCD and CF = 0
; AH = OOH
(b)Let AL = 05 BCD
CL = 09 BCD
AH = OOH
Consider the execution of the following instructions:
SUB AL, CL ; AL = -4 BCD (in 2’s complement form AL = FCH) and
CF = 1
AAS ; AL = 04 BCD
: CF = 1 indicating that a borrow is needed and
AH = FFH = 2’s complement of -1
AAA and AAS affect the AF and CF flags and OF, PF, SF, and ZF are left
undefined. Another salient feature of these two instructions is that it is possible
to take input data in the ASCII form of the unpacked decimal number, obtain
the result as an unpacked decimal number, and then convert it to ASCII form by
adding 30H to it.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 449
(xvii) AAD: ASCII-adjust before the division instruction modifies the dividend in
AH and AL, to prepare for the division of two valid unpacked BCD operands. After
the execution of AAD, AH is cleared and AL contains the binary equivalent of the
original unpacked two-digit numbers. Initially, AH contains the most significant
unpacked digit and AL contains the least significant unpacked digit.
Example:
To perform the operation 32 (decimal)/08 (decimal)
Let AH = 03H ; Upper decimal digit in the dividend
AL = 02H ; Lower decimal digit in the dividend
CL = 08H ; Divisor
Consider the execution of the following instructions:
AAD ; AX = 0020H (binary equivalent of the decimal value
32 in 16-bit form)
DIV CL ; Divide AX by CL. AL contains the quotient and AH the
remainder.
AAD affects the PF, SF, and ZF flags. AF, CF, and OF are undefined after
execution of AAD.
(xviii) AAM: The AAM (ASCII adjust AX after multiplication) instruction corrects
the value obtained by multiplication of two valid unpacked decimal numbers. The
higher-order digit is placed in AH and the lower-order digit in AL.
Example:
Let AL = 05 (decimal)
CL = 09 (decimal)
Consider the execution of the following instructions:
MUL CH ; AX = 002DH = 45 (decimal)
AAM ; AH = 04 and AL = 05 (unpacked BCD form of the decimal
number 45)
OR AX, 3030H ; To get the ASCII code of the result In AH and AL
(Note: This instruction is used only when the result
is needed in ASCII form.)
AAM affects the same flags as AAD.
(v) TEST: This instruction ANDs the content of a source byte or word with the
content of the specified destination byte or word. The flags are updated, but
neither operand is changed. The TEST instruction is often used to set flags before
a conditional jump instruction. The general form of TEST instruction is TEST
destination, source. The rules for the source and destination and the way flags are
affected are the same as the AND instruction.
Example:
Let AL = 0111 1111 =7FH
TEST AL, 80H ; AL = 7FH (unchanged)
ZF = 1 since (AL) AND (80H) = OOH; SF = 0; PF = 1
Mnemonics Function
Mnemonics Function
PUSHF Push the flag register’s content onto the stack
POPF Pop the top word of the stack onto the flag register
CMC Complement the carry flag (CF = complement of CF)
CLC Clear the carry flag (CF = 0)
STC Set the carry flag (CF = 1)
CLD Clear the direction flag (DF = 0)
STD Set the direction flag (DF = 1)
CLI Clear the interrupt flag (IF = 0)
STI Set the interrupt flag (IF = 1)
Mnemonics Description
Unconditional transfers
Mnemonics Description
In this table, ‘addr’ is the target address in the memory, to which the 8086 has
to jump, if the condition is satisfied while executing conditional jump instructions,
‘addr’ is also the target address to which the 8086 has to jump while executing
unconditional jump instructions. In the CALL instruction, ‘addr’ indicates the
address where the subroutine is located. In the case of conditional jump instructions,
the target address must be located at a relative address, which is in the range of
+127 bytes to -128 bytes from the instruction following the conditional jump
instruction.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 4 53
Mnemonics Description
LOOP addr Decrement CX. Go to addr if CX # 0.
LOOPE addr Loop while equal (Decrement CX. Go to addr if CX # 0 and
ZF= 1.)
LOOPZ addr Same as LOOPE
LOOPNE addr Loop while not equal (Decrement CX. Go to addr if CX # 0 and
ZF = 0.)
LOOPNZ addr Same as LOOPNE
In this table, ‘addr’ is the target address, which must be located at a relative
address in the range of+127 bytes to -128 bytes from the instruction following the
LOOP instruction.
The use of the LOOP instruction in a program is explained here with an
example:
MOV CX, 100
AGAIN : MOV AL, BL
Mnemonics Description
(ii) SAR: The general format of the SAR instruction is SAR destination, count.
The destination can be a register or a memory location and a byte or a word. This
instruction shifts each bit in the destination a specified number of bit positions to
the right. As a bit is shifted out of the MSB position, a copy of the old MSB is put
in the MSB position (i.e., the sign bit is copied into the MSB). The LSB will be
shifted into the carry flag (CF) as follows:
MSB------► MSB-------- > LSB ----- > CF
The rules for the count value in the instruction are the same as those for the SAL
instruction. CF, SF, and ZF are affected according to the result. PF has meaning
only when AL is used as the destination.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 455
(iii) SHR: The general format of the SHR instruction is SHR destination, count.
The destination can be a register or a memory location and a byte or a word. This
instruction shifts each bit in the destination a specified number of bit positions
to the right. As a bit is shifted out of the MSB position, a 0 is placed in the MSB
position. The LSB is shifted into the carry flag (CF) as follows:
0----- > MSB-------- > LSB ----- > CF
The rules for the count value in the instruction are same as those for the SHL
instruction. CF, SF, and ZF are affected according to the result. PF has meaning
only when an 8-bit destination is used.
(iv) ROR: This instruction rotates all the bits of the specified byte or word by
a specified number of bit positions to the right. The operation done when ROR is
executed is as follows:
The general format of the ROR instruction is ROR destination, count. The data
bit moved out of the LSB is copied into CF. ROR affects only CF and OF. In the
single-bit rotate operation, if the sign bit (i.e., the MSB) changes after the execution
of ROR, OF is set. This is applicable only for the single-bit rotate operation. ROR
is used to swap nibbles in a byte and to swap bytes in a word. It can also be used
to rotate a bit in a byte/word into CF, where it can be checked and acted upon by
the JC and JNC instructions. CF contains the bit most recently rotated out of the
LSB, in the case of a multiple bit rotate operation. The rules for the count value
are same as those for the shift instruction.
Example:
(a) ROR CH, 1 ; Rotate right the byte in CH by one bit
posi ti on.
(b)ROR BX, CL ; Rotate right the word in BX by the
number of bit positions given by CL.
(c) ROR BYTE PTR [SI], 1 ; Rotate right the byte in the memory at
offset [SI] by one bit position.
(d) ROR WORD PTR [BX], CL ; Rotate right the word in the memory at
offset [BX] by the number of bit
positions given by CL.
(v) ROL: ROL rotates all the bits in a byte or word in the destination to the left, by
one or more bit positions, using CL, as follows:
rotate operation. ROL is used to swap nibbles in a byte or swap bytes in a word.
It can also be used to rotate a bit in a byte/word into CF, where it can be checked
and acted upon by the JC and JNC instructions. CF contains the bit most recently
rotated out of the LSB, in the case of the multiple bit rotate operation.
(vi) RCR: RCR rotates the byte or word in the destination right, through the carry
flag (CF), either by one bit position or by the number of bit positions given by CL,
as follows:
The flags affected are the same as those affected during the execution of
ROR.
(vii) RCL: RCL rotates the byte or word in the destination left through the carry
flag (CF), either by one bit position or by the number of bit positions given by CL,
as follows:
Mnemonics Function
Mnemonics Function
CMPSW Compare string words (done by subtracting the word at
ES: [DI] from the word at DS: [SI]). Only flags are affected;
the content of the words compared is unaffected.
LODSB Load the string byte at DS:[SI] into AL.
LODSW Load the string word at DS:[SI] into AX.
STOSB Store the string byte in AL at ES:[DI].
STOSW Store the string word in AX at ES:[DI],
SCASB Compare string bytes (done by subtracting the byte at ES: [DI]
from the byte at AL). Only flags are affected; the content of the
bytes compared is unaffected.
SCASW Compare string words (done by subtracting the word at ES:
[DI] from the byte at AX). Only flags are affected; the content
of the words compared is unaffected.
REP Decrement CX and repeat the following string operation if
CX # 0.
REPE or REPZ Decrement CX and repeat the following string operation if
CX # 0andZF= I.
REPNE or REPNZ Decrement CX and repeat the following string operation if
CX 0 and ZF = 0.
The REP (repeat) prefix placed before a string instruction causes the string
instruction to be executed CX times.
Example:
(a)MOV CX, 32H ; Load 32H (= decimal 50) in CX.
(b)REP MOVSW ; Execute MOVSW instruction 50 times.
Execution of these two instructions causes the moving of a string having 50
words from the data segment to the extra segment.
Note:
(i) To initialize a segment register with a value, the value is first loaded in one of
the general-purpose registers such as AX or BX. It is then moved to the segment
register. In this example, AX is used to load 3000H into DS.
(ii) Sometimes, instead of using the HLT instruction at the end, the software
interrupt instruction (INT) may be used to return the control to the monitor program
after executing the program.
Example 14.2:
Write a program to subtract the byte content of the memory location 3000H: 4000H
from the byte content of the memory location 4000H: 5000H and store the result at
the location 2000H: 3000H. Assume that the input data and the result lie between
-128 and +127, and that the negative numbers are represented in 2’s complement
form. (Note: 3000H: 4000H represents the segment address of 3000H and the
offset address 4000H in that segment).
460 MICROPROCESSORS AND MICROCONTROLLERS
Solution:
MOV BX, 3000H
MOV DS, BX ; Initialize DS with the segment address
3000H.
MOV CL, [4000H] ; Get the subtrahend from the offset address
4000H.
MOV BX, 4000H
MOV DS, BX ; Initialize DS with the segment address
4000H.
MOV AL, L5000H] ; Get the minuend at the offset address 5000H
to AL.
SUB AL, CL : AL <- AL - CL
MOV BX, 2000H
MOV DS, BX ; Initialize DS with the segment address
2000H.
MOV [3000H], AL ; Store AL at the offset address 3000H.
HLT : Stop.
Note: After the execution of the program, if the result is positive, its MSB and
carry are 0. If the result is negative, it is represented in 2’s complement form; its
MSB and carry are 1.
Example 14.3:
Write a program to move a word string 200 bytes (i.e., 100 words) long from the
offset address 1000H to the offset address 3000H in the segment 5000H.
Solution:
Using the REP prefix with the MOVSW instruction, the program size can be
reduced in comparison with a program that uses the MOV instruction for the same
task.
MOV AX, 5000H
MOV DS, AX ; Initialize DS with the segment address 5000H.
MOV ES, AX ; Initialize ES with the segment address 5000H.
MOV SI, 1000H ; Initialize SI with the offset address of the
source (i.e., 1000H).
MOV DI, 3000H ; Initialize DI with the offset address of the
destination (i.e., 3000H).
MOV CX, 100 ; Initialize CX with the numberof words in the string
(decimal value of 100 or 64H).
CLD ; Clear the D flag for auto-increment mode.
REP MOVSW ; Execute MOVSW instruction CX times.
HLT : Stop.
Note:
(i) The MOVSB instruction can be used in this program instead of MOVSW, but
CX must be loaded with the value 200.
(ii) As D is 0, every time MOVSW is executed, the SI and DI registers are
incremented by 2, to point the next word in the string.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 461
Example 14.4:
Write a program to find the smallest word in an array of 100 words stored
sequentially in the memory, starting at the offset address 1000H in the segment
address 5000H. Store the result at the offset address 2000H in the same segment.
Solution:
MOV CX, 99 ; Initialize CX with the number of
comparisons (= 100 - 1).
MOV AX, 5000H
MOV OS, AX : Initialize DS with the segment address
5000H.
MOV SI, 1000H ; Initialize SI with the offset address
1000H.
MOV AX, [SI] : Move the first word to AX.
START: INC SI
INC SI : Increment SI twice to point the next
word.
CMP AX, [SI] ; Compare the next word with the word in AX.
JC REPEAT ; If AX is smaller, jump to REPEAT.
MOV AX, [SI] ; Replace the word in AX with the smaller
word.
REPEAT: LOOP START ; Repeat the operation from START.
MOV [2000H], AX : Store the smallest number in AX at the
offset address 2000H.
HLT ; Stop.
Examples 14.5:
Write a program to find the number of positive and negative data items in an array
of 100 bytes of data stored from the memory location 3000H: 4000H. Store the
result in the offset addresses 1000H and 1001H in the same segment. Assume that
the negative numbers are represented in 2’s complement form.
(Note: The basic principle used here is that the MSB for a positive number is 0 and
that for a negative number is 1.)
Solution:
MOV AX, 3000H
MOV DS, AX ; Initialize DS with 3000H.
MOV CX, 100 ; Move the number of data items to CX.
MOV BX, 4000H ; Move the starting offset address of the
array to BX.
MOV DH, OOH ; Initialize DH with OOH to store the number of
positive data items.
MOV DL, OOH ; Initialize DL with OOH to store the number of
negative data items.
L2: MOV AL, [BX] ; Move a byte data from the array to AL.
ROL AL, 01 ; Rotate AL left by one bit. Now the MSB in AL
goes to the carry flag and also to the LSB of
AL.
462 MICROPROCESSORS AND MICROCONTROLLERS
Example 14.6:
Write a program to find the seven-segment code of a digit between 0 and 9 or a
character between A and F. Assume that the seven-segment code of the characters
is stored in the memory starting at the address 2000H: 1000H. The result must be
stored at the offset address 2000H in the same segment.
Solution:
MOV AX, 2000H
MOV DS, AX ; Initialize DS with the value 2000H.
MOV BX, 1000H : Initialize BX with the starting offset address
of the table containing the seven - segment
codes.
MOV AL, 03 ; Load the number (here ‘3’) whose seven - segment
code is to be found in AL.
XLAT ; Using XLAT instruction, move the seven
segment code of 03 to AL.
MOV [2000H], AL ; Store the result at the offset address
2000H.
HLT ; Stop.
Memory Address
Seven-segment code of 0 2000H: 1OOOH
Seven-segment code of 1 2000H: 1001H
Seven-segment code of 2 2000H: 1002H
Note: When the XLAT instruction is executed in this example, the content of BX
(= 1000H) is added to the content of AL (= 03H) to form an offset address (=
1003H) and the data in that offset address (seven-segment code of 03H) is moved
to AL. This technique is called look-up table technique.
Example 14.7:
Write a program to convert the 8-bit packed BCD number stored in the memory
location 3000H : 2000H into a binary number and store it in the offset address
2001H in the same segment.
Solution:
MOV AX, 3000H
MOV DS, AX ; Initialize DS with 3000H.
MOV AL, E2000HJ ; Move the 8-bit BCD number to AL.
MOV BL, AL ; Store a copy of the BCD number in BL.
AND AL, OFOH ; Mask the lower-order nibble in AL.
MOV CL, 04
ROR AL, CL ; Rotate AL right four times, to get the upper
nibble of the BCD number.
MOV BH, OAH ; Move OAH to BH.
MUL BH ; Multiply AL and BH, and store the result in
AL.
AND BL, OFH ; Mask the upper nibble in BL.
ADD AL, BL ; Add the contents of AL and BL.
MOV [2001H], AL ; Store the result in AL at the offset address
2001H.
HLT ; Stop.
Note:
(i) When the most significant digit of a hexadecimal data is any one of the digits
between A and F, it is preceded by 0, while writing in the program.
(ii) The binary number corresponding to an 8-bit packed BCD number is obtained
by multiplying the decimal value 10 (= OAH) with the upper digit of the BCD
number and adding the result with the lower digit of the BCD number. Since the
maximum 8-bit BCD number is 99 and the corresponding binary number is 63H
(= 9 x OAH + 9), the result in this program is also 8 bits. The result in AH is ignored
in the MUL BH instruction in this program, as AH = OOH after multiplication.
Example 14.8:
Write a program to convert the given 8-bit binary number into ASCII codes. The 8-
bit binary number is present in the memory location 2000H: 5000H and the result
is to be stored at the offset addresses 1000H and 1001H in the same segment.
Solution:
MOV AX, 2000H
MOV DS, AX : Initialize DS with 2000H.
MOV AL, [5000H] ; Move the binary data to AL.
MOV BL, AL ; Save a ropy of AL in BL.
464 MICROPROCESSORS AND MICROCONTROLLERS
Note: The ASCII code of the 8-bit binary number, say F8H, is obtained by first
splitting the binary number into two digits, F and 8, and then finding the ASCII
codes of F and 8 separately. The ASCII code of a digit between 0 and 9 is obtained
by adding 30H to the digit and the ASCII code of a digit between A and F is
obtained by adding 37H to the digit. The ASCII codes of F and 8 are 46H and
38H, respectively.
Example 14.9:
Write a program to add the two BCD data 29H and 98H and store the result in
BCD form in the memory locations 2000H: 3000H and 2000H: 3001H.
Solution:
MOV AL, 29H ; Move the first BCD data to AL.
ADD AL, 98H ; Add the second BCD data with AL.
DAA ; Decimal-adjust AL to get the result in
BCD form.
MOV BX, 2000H
MOV DS, BX ; Initialize DS with 2000H.
MOV E3000H], AL; Store the content of AL, which is the lower
byte of the result in the memory.
JC LI ; If the carry flag is 1, go to LI.
MOV E3001HJ, OOH ; Store OOH in the memory, since the carry is
0.
JMP L2 ; Go to L2.
LI: MOV E3001H], 01H Store 01H in memory, since the carry is 1.
L2: HLT ; Stop.
Example 14.10:
Write a program to convert the 8-bit binary number FFH into a BCD number. The
result is to be stored at memory locations 3000H: 2000H and 3000H: 2OO1H.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 465
Solution:
MOV AX, OOFFH : Move the data FFH to AX, with the upper byte
as OOH.
MOV BL, 100 ; Store the decimal value 100 (or 64H) in BL.
DIV BL ; Divide AX by BL to find the number of hundreds
in the binary number.
MOV DL, AL ; Move the quotient in AL (number of hundreds)
to DL.
MOV AL, AH ; Move the remainder in AH to AL.
MOV AH, 00 ; Clear AH.
MOV BL, 10 ; Store the decimal value 10 (or OAH) in BL.
DIV BL ; Divide AX by BL to find the number of tens in
the binary number. AH has the remainder, which
is the number of ones in the binary number.
MOV CL, 04
ROR AL, CL ; Rotate content of AL right by 4 bits to
place number of tens in upper nibble of AL.
OR AL, AH ; Perform OR operation on AL and AH to
concatenate the number of tens and ones.
MOV BX, 3000H
MOV DS, BX : Initialize DS with 3000H.
MOV L2000H], DL ; Move the value of DL to the memory.
MOV E2001H], AL : Move the value of AL to the memory.
HLT ; Stop.
Note: The binary number FFH when converted to BCD gives the result 255, as
there are two hundreds, five tens, and five ones in it. In this program, 02H is stored
in the offset address 2000H and 55H is stored in the offset address 2001H in the
data segment.
In addition, there are a few operators that perform the addition or subtraction
operation on constants or labels. The assembler directives commonly used in
Microsoft Macro Assembler or Turbo Assembler are as follows:
Assembler Directives for Variable and Constant Definition
The assember directives for variable and constant definition are as follows:
(i) DB, DW, DD, DQ, and DT: The directives DB (define byte), DW (define word),
DD (define double word), DQ (define quad word), and DT (define ten bytes) are
used to reserve one byte, one word (i.e., 2 bytes), one double word (i.e., 2 words),
one quad word (i.e., 4 words), and ten bytes in the memory, respectively, for
storing constants, variables, or strings.
Example:
(a) DATA1 DB 20H ; Reserve one byte for storing
DATA1 and assign the value 20H
to it.
(b) ARRAY1 DB 10H, 20H, 30H ; Reserve three bytes for storing
ARRAY1 and initialize it with
the values 10H, 20H, and 30H.
(c) CITY DB “MADURAI” ; Store the ASCII code of the
characters specified within
double quotes in the array or
list named CITY.
(d) DATA2 DW 1020H ; Reserve one word for storing
DATA2 and assign the value
1020H to it.
(e) ARRAY2 DW 1030H, 2000H,
3000H, 4000H : Reserve four words for storing
ARRAY2 and initialize them with
the specified values.
(f) DATA3 DD 1234ABCDH ; Initialize DATA3 as a double
word with 123ABCDH.
(g) DATA4 DQ 1234ABCD5678EFBBH ; Initialize DATA4 as a quad word
with 1234ABCD5678EFBBH.
(h) DATA5 DT 123456789ABCDEFl2345H; Initialize DATA5 as a series
of 10 bytes having the value
123456789ABCDE Fl2345H.
The directive DUP (duplicate) is used to reserve a series of bytes, words,
double words, or ten bytes and is used with DB, DW, DD, and DT, respectively.
The reserved area can be either filled with a specific value or left uninitialized.
Example:
(a) Array DB 20 DUP (0) ; Reserves 20 .bytes in the memory
for the array named ARRAY and
initializes all the elements
of the array to 0 (due to the
presence of 0 within the bracket
near the DUP directive).
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 467
(ii) EQU: The directive EQU (equivalent) is used to assign a value to a data
name.
Example:
(a) NUMBER EQU 50H ; Assign the value 50H to NUMBER.
(b) NAME EQU “RAMESH” ; Assign the string “RAMESH” to NAME.
Example:
The EVEN directive can also be used at the beginning of a procedure, so that the
instructions in it can be fetched quickly by the 8086 during execution.
EVEN
RESULT PROC NEAR
; instructions in the RESULT procedure
RESULT ENDP
Here the procedure RESULT, which is of type NEAR, is stored starting at an
even address in the code segment. The ENDP directive indicates the end of the
RESULT procedure.
(iii) LENGTH: This directive is used to determine the length of an array or string
in bytes.
Example:
MOV CX, LENGTH ARRAY
CX is loaded with the number of bytes in the ARRAY.
(iv) OFFSET: This operator is used to determine the offset of a data item in a
segment containing it.
Example:
MOV BX, OFFSET TABLE
If the data item named TABLE is present in the data segment, this statement places
the offset address of TABLE, in the BX register.
(v) LABEL: The LABEL directive is used to assign a name to the current value
in the location counter. It is used to specify the destination of the branch-related
instructions such as jump and call. When LABEL is used to specify the destination,
it is necessary to specify whether it is NEAR or FAR. When the destination is in
the same segment, the label is specified as NEAR and when the destination is in
another segment, it is specified as FAR.
Example:
REPEAT LABEL NEAR
CALCULATE LABEL FAR
LABEL can also be used to specify a data item. When it is used to specify a
data item, the type of the data item must be specified. The data may have the type
—byte or word.
Example:
A stack segment having 100 words of data is defined using the following
statements:
STACK SEGMENT
DW 100 DUP (0)
STACK_T0P LABEL WORD ; reserve 100 words for stack
STACK ENDS
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 469
The second statement reserves 100 words in the stack segment and fills them
with 0. The third statement assigns the name STACK_TOP to the location present
just after the hundredth word. The offset address of this label can then be assigned
to the stack pointer in the code segment using the following statement:
MOV SP, OFFSET STACK_TOP
Assembler Directives for Segment Declaration
The assember directives for segment declaration are as follows:
(i) SEGMENT and ENDS: The SEGMENT and ENDS directives indicate the start
and end of a segment, respectively. In some cases, the segment may be assigned a
type such as PUBLIC (i.e., it can be used by other modules of the program while
linking) or GLOBAL (i.e., it can be accessed by any other module).
Large assembly language programs are usually developed as separate assembly
modules. Each assembly module is individually assembled, tested, and debugged.
When all the assembly modules are working correctly, their object code files are
linked together to form the complete program. For the modules to link together
correctly, any segment, label, or variable name referred to in other modules must be
declared PUBLIC in the module in which it is defined. For example, the statement
DATA1 SEGMENT WORD PUBLIC makes the segment named DATA1
available to other assembly modules. Here, the term WORD is used to inform
the linker to locate the segment in the first available even address. Similarly, the
statement PUBLIC XI, X2 makes the two variables XI and X2 available to other
assembly modules. If an instruction in an assembly module refers to a variable or
label which is present in another assembly module, the assembler must be told that
it is external, using the EXTRN directive.
The GLOBAL directive can be used in place of the PUBLIC or EXTRN
directive. For a symbol or name defined in the current assembly module, the
GLOBAL directive is used to make that symbol or name available to other
assembly modules. For example, the statement GLOBAL MULTIPLIER makes
the variable MULTIPLIER public so that it can be accessed from other assembly
modules. The statement GLOBAL MULTIPLIER: WORD informs the assembler
that MULTIPLIER is a variable of type ‘word’, which is in another assembly
module.
Example:
CODEI SEGMENT
; instructions of CODE 1 segment
CODEI ENDS
This example indicates the declaration of a code segment named CODEI.
(ii) ASSUME: The ASSUME directive is used to inform the assembler, the name of
the logical segments to be assumed for different segments used in the program.
Example:
ASSUME CS: CODEI , DS: DATA1
470 MICROPROCESSORS AND MICROCONTROLLERS
This statement informs the assembler that the segment address where the logical
segments CODEI and DATA1 are loaded in memory during execution is to be
stored in the CS and DS registers, respectively.
(iii) GROUP: This directive is used to form a logical group of segments with a
similar purpose. The assembler passes information to the linker/loader to form
the code, such that the group declared segments or operands lie within a 64 KB
memory segment. All such segments can be addressed using the same segment
address.
Example:
PROGRAMI GROUP CODEI. DATA1, STACK1
This statement directs the loader/linker to prepare an executable (EXE) file such
that the CODEI, DATA1, and STACK 1 segments lie within a 64 KB memory
segment that is named PROGRAM 1. Now, for the ASSUME statement, we can use
the label PROGRAMI rather than CODEI, DATA1, and STACK1, as follows:
ASSUME CS: PROGRAMI, DS: PROGRAMI, SS: PROGRAMI
(iv) SEG: The segment operator is used to decide the segment address of the label,
variable, or procedure and substitute the segment address in place of the SEG
label.
Example:
MOV AX, SEG ARRAY1 ; Load the segment address in which ARRAY1 is
present, in AX.
MOV DS, AX ; Move the content of AX to DS.
Assembler Directives for Declaring Procedures
The assember directives for declaring procedures are as follows:
(i) PROC: The PROC directive indicates the start of a named procedure. The
NEAR and FAR directives specify the type of the procedure.
Example:
SQUARE_R00T PROC NEAR
This statement indicates the beginning of a procedure named SQUARE_ROOT,
which is to be called by a program located in the same segment. The FAR directive
is used for the procedures to be called by the programs present in code segments
other than the one in which this procedure is present. For example, SALARY
PROC FAR indicates the beginning of a FAR type procedure named SALARY.
(ii) ENDP: The ENDP directive is used to indicate the end of a procedure. To
mark the end of a particular procedure, the name of the procedure may appear as
a prefix with the directive ENDP.
Example:
SALARY PROC NEAR
: code of SALARY procedure
SALARY ENDP
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 471
(iii) EXTRN and PUBLIC: The directive EXTRN (external) informs the assembler
that the procedures, label/labels, and names declared after this directive has/have
already been defined in some other segments and in the segments where they
actually appear, they must be declared public, using the PUBLIC directive.
Example:
M0DULE1 SEGMENT
PUBLIC SQUARE-ROOT
SQUARE-ROOT PROC FAR
; code of SQUARE-ROOT procedure
SQUARE-ROOT ENDP
M0DULE1 ENDS
M0DULE2 SEGMENT
EXTRN SQUARE-ROOT FAR
; code of M0DULE2
CALL SQUARE-ROOT
M0DULE2 ENDS
If one wants to call the procedure named SQUARE_ROOT appearing in
MODULE 1 from MODULE2, it must be declared public using the statement
PUBLIC SQUARE-ROOT in MODULE1 and it must be declared external using
the statement EXTRN SQUARE-ROOT in MODULE2. If a jump or call address
is external, it must be represented as NEAR or FAR. If data are defined as external,
their size must be represented as BYTE, WORD, or DWORD.
Other Assembler Directives
(i) PTR: The PTR (pointer) operator is used to declare the type of a label, variable,
or memory operand. The operator PTR is prefixed by either BYTE or WORD. If
the prefix is BYTE, the particular label, variable, or memory operand is treated as
an 8-bit quantity, while if the prefix is WORD, it is treated as a 16-bit quantity.
Example:
(a) INC BYTE PTR LSI] ; Increment the byte contents of the
memory location addressed by SI.
(b) INC WORD PTR LBX] ; Increment the word contents of the
memory location addressed by BX.
The PTR directive is also used to declare a label either as FAR or NEAR type.
The FAR PTR directive indicates to the assembler that the label following FAR
PTR is not available within the same segment and the address of the label is of size
32 bits (2 bytes offset, followed by 2 bytes segment address).
Example:
(a) JMP FAR PTR DIVIDE
472 MICROPROCESSORS AND MICROCONTROLLERS
Example:
The following statement declares the variables DATA1, DATA2, and ARRAY1
as GLOBAL variables.
GLOBAL DATA1, DATA2, ARRAY1
(iv) NAME: The NAME directive is used to assign a name to an assembly language
program module. The module may now be referred to by its declared name. The
names, if selected properly, may indicate the function of the different modules,
and hence help in good documentation.
(v) SHORT: The SHORT operator indicates to the assembler that only one byte is
required to code the displacement for a jump (i.e., the displacement is within -128
to +127 bytes from the address of the byte present next to the JMP opcode). This
method of specifying the jump address saves memory. Otherwise, the assembler
may reserve 2 bytes for the displacement in the jump instructions.
Example:
JMP SHORT MULTIPLY
where MULTIPLY is a label.
(vi) TYPE: The TYPE operator directs the assembler to decide the data type of
the specified label and replaces the TYPE label with the decided data type. For the
word type variable, the data type is 2. For the double word type, it is 4, and for the
byte type, it i® 1.
Example:
If DATA 1 is an array having word type data, the instruction MOV BX, TYPE DATA1
moves the value 0002H to BX.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 473
After the cross reference file name is entered, the assembly process starts. If the
program contains syntax errors, they are displayed using the error code number
and the corresponding line numbers at which the errors have occurred. Once
these errors are corrected by the programmer, the assembly process is completed
successfully.
The DOS linking program LINK.EXE is used to link the different object
modules of a source program and the function library routines to produce an
integrated executable code for the source program. The linker is invoked using the
following command:
C :\> LINK MSI.OBJ
After entering this command, the linker asks for the name of the following
files:
Run file [.EXE]:
List files [NUL.MAP]:
Libraries [LIB]:
If no file names are entered for these files, by default, the source file name is
considered. The optional input ‘Libraries’ expects the name of a special library (if
any) from which the functions were used by the source program. The output of the
linker program is an executable file with either the file name entered by the user or
the default file name, and .EXE extension. The executable file name can be entered
at the DOS prompt to execute the file as follows:
C :\> MSI.EXE
In the advanced version of MASM, both assembling and linking are combined
under a single menu-invocable compile function.
DEBUG.com is a DOS utility program that is used for debuggihg and
troubleshooting 8086 assembly language programs. The DEBUG utility enables
us to have control over the hardware resources and the memory in the computer
(PC) up to a certain extent, as the PC uses one of the INTEL processors (80486,
Pentium, etc.) as the CPU. DEBUG enables us to use the PC as a low-level 8086
microprocessor kit. Typing the DEBUG command at the DOS prompt and pressing
the enter key invokes the debugging facility. A (dash) appears DEBUG is
successfully invoked, as follows:
C :\> DEBUG
Now, by typing ‘R’ at the line and pressing the enter key, we can see
the content of the different registers and flags present in the CPU of the PC, as
follows:
-R
AX = 0000H BX = 0005H CX = OOODH DX = SOOOH
SP = 8500H BP = 9800H SI = 2000H DI = 7000H
DS = SOOOH ES = 3000H SS = 4000H CS = 2000H
IP = 2000H FLAGS = 0024H
7'he remaining DEBUG commands can be referred to from any book that
discusses assembly language programming in personal computers. In this section,
476 MICROPROCESSORS AND MICROCONTROLLERS
a few examples for writing 8086 assembly language programs while using an
assembler are given.
Example 14.11:
Write a program to add two 8-bit data (FOH and 50H) in the 8086 and to store the
result in the memory, when the assembler is used.
Solution:
ASSUME CS: CODE, DS: DATA
DATA SEGMENT ; Beginning of data segment
0PER1 DB FOH ; First operand
0PER2 DB 50H ; Second operand
RESULT DB 01 DUP (?) : A byte of memory is reserved for the
result.
CARRY DB 01 DUP (?) A byte is reserved for storing the
ca rry.
DATA ENDS End of data segment.
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV SI, OFFSET ARRAY : Move the offset of the array to SI.
MOV CL, COUNT : Load COUNT in CL.
DEC CL : Decrement CL as the number of
comparisons is one less than the
count.
MOV AX, ESI] : Move the first word to AX.
AGAIN: ADD SI, 02 : Add 2 to SI to get the next word.
MOV BX, [SI] : Move the next word to BX.
CMP AX, BX : Compare the word in AX with BX.
JC NEXT : If AX i s sma11, go to NEXT.
MOV AX, BX : Move the small word in BX to AX.
478 MICROPROCESSORS AND MICROCONTROLLERS
Example 14.13:
Write a program to find the number of even and odd data bytes present in the given
array having one hundred byte type data.
Solution:
ASSUME CS: CODE, DS: DATA
DATA SEGMENT
ARRAY DB 40H, 31H, 23H, 52H... ; Enter all the data items of ARRAY
here.
COUNT EQU 64H ; Initialize COUNT with the value 100.
EVEN_NOS DB OOH ; Reserve a byte for storing the number
of even data items.
ODD_NOS DB OOH : Reserve a byte for storing the number
of odd data items.
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV BL, OOH ; Initialize BL with OOH, to store
the number of even data items.
MOV DL, OOH ; Initialize DL with OOH, to store the
number of odd data items.
MOV CL, COUNT ; Initialize CL with COUNT.
MOV SI, OFFSET ARRAY ; Move the offset address of ARRAY to
the SI register.
AGAIN: MOV AL, [SI] ; Move one byte from ARRAY to AL.
RCR AL, 1 ; Rotate AL right through the carry
by 1 bit.
JC ODD ; If carry = 1, the number is odd. So go
to ODD.
INC BL ; Otherwise, the number is even and
hence increment BL.
JMP LI ; Jump to LI.
ODD: INC DL ; Increment DL by 1 as the number is
odd.
LI: INC SI ; Increment SI to point to the next
data.
LOOP AGAIN ; Go to AGAIN, CL times.
MOV EVEN-NOS, BL ; Store the content of BL in EVEN_NOS.
ADDRESSING MODES, INSTRUCTION SET, AND PROGRAMMING OF 8086 479
Example 14.14:
Write a program to arrange the given array having ten word type data in ascending
order.
Solution:
ASSUME CS: CODE, DS: DATA
DATA SEGMENT
ARRAY DW 1234H , 4050H, 0035H... ; Enter all the data items of ARRAY
here.
COUNT EQU OAH
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV CL, COUNT ; Load the number of data items in
CL.
DEC CL ; Decrement CL as the number of
passes is one less than the
number of data.
NEXT_PASS: MOV BL, CL ; Initialize BL with the number of
comparisons to be done in each
pass.
MOV SI, OFFSET ARRAY : Move the offset address of ARRAY to
the SI register.
COMPARE: MOV AX, [SI] ; Move one data item to AX.
ADD SI, 02H ; Add 02 to SI to point to the next
data.
CMP AX, [SI] ; Compare the next data with the
content of AX.
JC LI ; If the first data item is lesser
than the second, go to Ll.
XCHG AX, [SI] ; Otherwise, exchange the data in
AX and the memory.
SUB SI, 02H ; Subtract 02 from SI to point to
the previous memory location.
MOV [SI], AX ; Store the content of AX (smaller
data) in the memory.
ADD SI, 02 ; Increment SI by 2 to compare the
next data with AX.
Ll; DEC BL : Decrement the number of comparisons
in BL by 1.
480 MICROPROCESSORS AND MICROCONTROLLERS
The algorithm used here is explained with the following simple example. Let
us consider arranging four words stored in an array in ascending order. Since there
are 4 (= N) words, 3 (= N - 1) passes have to be done. In the first pass, 3 (= N - 1)
comparisons are made and the highest number is brought to the end of the array. In
the second pass, 2 (= N - 2) comparisons are made since only the top three words
of the array need to be compared and in the third pass, only one comparison is
needed to compare the first two data in the array.
Let us assume that the data in the array is as follows:
3200H
4F35H
2350H
1FC2H
The comparisons done in each pass and the exchange of data for arranging
them in ascending order are shown here:
PASS I:
3200H <— 3200H 3200H 3200H
4F35H । 4F35H <-। 2350H 2350H
2350H 2350H <- 4F35H <-] 1FC2H
1FC2H 1FC2H 1FC2H 4F35H
PASS II:
3200H 2350H 2350H
2350H 3200H <-1 1FC2H
1FC2H 1FC2H ' 3200H
4F35H 4F35H 4F35H
PASS III:
2350H IFC2H
1FC2H 2350H
3200H 3200H
4 F35H 4F35H
Sorted array