unit II
MICROPROCESSOR PROGRAMMING-I
The Instruction set of 8086
1 Data-Transfer Instructions
2 Arithmetic Instructions
3 Logic Instructions
4 Shift Instructions
5 Rotate Instructions
1. Data-Transfer Instructions:
The data-transfer functions provide the ability to
move data either between its internal registers or
between an internal register and a storage location in
memory
The data-transfer functions include
MOV (Move byte or word)
XCHG (Exchange byte or word)
XLAT (Translate byte)
LEA (Load effective address)
LDS (Load data segment)
LES (Load extra segment)
The MOVE Instruction
The move (MOV) instruction is used to transfer a
byte or a word of data from a source operand to
a destination operand
e.g. MOV DX, CS
MOV [SUM], AX
a) MOV data transfer instructions:
Note that the MOV instruction cannot transfer
data directly between external memory
B)Allowed Operands:
MOV DX, CS before fetch and execution
MOV DX, CS after execution
EXAMPLE:
What is the effect of executing the instruction?
MOV CX, [SOURCE_MEM],
where SOURCE_MEM equal to 2016 is a memory location
offset relative to the current data segment starting at
1A00016
Solution:
((DS)0+2016) (CL)
((DS)0+2016+116) (CH)
Therefore CL is loaded with the contents held at memory
address
1A00016 + 2016 = 1A02016
and CH is loaded with the contents of memory address
1A00016 + 2016 +116 = 1A02116
The XCHG Instruction:
The exchange (XCHG) instruction can be used to
swap data between two general-purpose
registers or between a general purpose register
and a storage location in memory
e.g. XCHG AX, DX
XCHG data transfer instruction:
Allowed Operands:
EXAMPLE:
What is the result of executing the following
instruction?
XCHG [SUM], BX
where SUM = 123416, (DS)=120016
Solution:
((DS)0+SUM) (BX)
PA = 1200016 + 123416=1323416
Execution of the instruction performs the following 16-bit
swap:
(1323416) (BL)
(1323516) (BH)
So we get (BX) = 00FF16, (SUM) = 11AA16
XCHG [SUM], BX before fetch and execution:
After Execution:
The XLAT Instruction
The translate (XLAT) instruction is used to
simplify implementation of the lookup-table
operation.
Execution of the XLAT replaces the contents
of AL by the contents of the accessed
lookup-table location
XLAT data transfer instructions:
e.g. PA = (DS)0 + (BX) + (AL)
= 0300016 + 010016 + 0D16 = 0310D16
(0310D16) (AL)
The LEA, LDS, and LES Instructions:
The LEA, LDS, LES instructions provide the ability
to manipulate memory addresses by loading
either a 16-bit offset address into a general-
purpose register or a register together with a
segment address into either DS or ES
The LEA, LDS, and LES data transfer Instructions:
e.g. LEA SI, [DI+BX+5H]
LDS SI, [200H] before fetch and execution
LDS SI, [200H] after execution:
EXAMPLE:
Initializing the internal registers of the 8088 from
a table in memory
Solution:
a) Initializing the internal registers of the 8088from a
table in memory
b) Table of initialization information:
Arithmetic Instructions
The arithmetic instructions include
Addition
Subtraction
Multiplication
Division
Data formats
Unsigned binary bytes
Signed binary bytes
Unsigned binary words
Signed binary words
Unpacked decimal bytes
Packed decimal bytes
ASCII numbers
Addition Instructions: ADD, ADC, INC, AAA, DAA
Addition Instructions: ADD, ADC, INC, AAA, DAA
Allowed operands for ADD and Allowed
operands
for INC instruction ADC
instructions
EXAMPLE:
Assume that the AX and BX registers contain
110016 and 0ABC16, respectively. What is the
result of executing the instruction ADD AX, BX?
Solution:
(BX)+(AX)= 0ABC16 + 110016=1BBC16
The sum ends up in destination register AX. That is
(AX) = 1BBC16
Addition Instructions: ADD, ADC, INC, AAA, DAA
ADD AX, BX
Addition Instructions: ADD, ADC, INC, AAA, DAA
ADD AX, BX
EXAMPLE:
The original contents of AX, BL, word-size
memory location SUM, and carry flag (CF) are
123416, AB16, 00CD16, and 016, respectively.
Describe the results of executing the following
sequence of instruction?
ADD AX, [SUM]
ADC BL, 05H
INC WORD PTR [SUM]
Solution:
(AX) (AX)+(SUM) = 123416 + 00CD16
=130116
(BL) (BL)+imm8+(CF) = AB16 + 516+016 =
B016
(SUM) (SUM)+ 116 = 00CD16 + 116 = 00CE16
EXAMPLE:
What is the result of executing the following instruction
sequence?
ADD AL, BL
AAA
Assuming that AL contains 3216 (ASCII code for 2) and BL
contains 3416 (ASCII code 4), and that AH has been cleared
Solution:
(AL) (AL)+(BL)= 3216 + 3416=6616
The result after the AAA instruction is
(AL) = 0616
(AH) = 0016
with both AF and CF remain cleared
EXAMPLE:
Perform a 32-bit binary add operation on the
contents of the processor’s register.
Solution:
(DX,CX) (DX,CX)+(BX,AX)
(DX,CX) = FEDCBA9816
(BX,AX) = 0123456716
MOV DX, 0FEDCH
MOV CX, 0BA98H
MOV BX, 01234H
MOV AX, 04567H
ADD CX, AX
ADC DX, BX ; Add with carry
Subtraction Instructions: SUB, SBB, DEC, AAS, DAS,
and NEG
Subtraction Instructions: SUB, SBB, DEC, AAS, DAS,
and NEG
Allowed operands for Allowed operands Allowed
operands
SUB and SBB instructions for DEC instruction for NEG
instruction
EXAMPLE:
Assuming that the contents of register BX and CX
are 123416 and 012316, respectively, and the carry
flag is 0, what is the result of executing the
instruction SBB BX, CX?
Solution:
(BX)-(CX)-(CF) (BX)
We get
(BX) = 123416 – 012316 – 016
= 111116
the carry flag remains cleared
EXAMPLE:
Assuming that the register BX contains 003A16,
what is the result of executing the following
instruction?
NEG BX
Solution:
(BX) = 000016-(BX)=000016+2’complement of 003A16
= 000016+FFC616
= FFC616
Since no carry is generated in this add operation, the
carry flag is complemented to give (CF) = 1
EXAMPLE:
Perform a 32-bit binary subtraction for variable X
and Y
Solution:
Multiplication Instructions: MUL, DIV, IMUL, IDIV, AAM,
AAD, CBW, and CWD
EXAMPLE:
The 2’s-complement signed data contents of AL
are –1 and that of CL are –2. What result is
produced in AX by executing the following
instruction?
MUL CL and IMUL CL
Solution:
(AL) = -1 (as 2’s complement) = 111111112 = FF16
(CL) = -2 (as 2’s complement) = 111111102 = FE16
Executing the MUL instruction gives
(AX) =
Executing the IMUL instruction gives
(AX) = -116 x -216 = 216 = 000216
EXAMPLE:
What is the result of executing the following
instructions?
MOV AL, 0A1H
CBW
CWD
Solution:
(AL) = A116 = 101000012
Executing the CBW instruction extends the MSB of AL
(AH) = 111111112 = FF16
or (AX) = 11111111101000012
Executing the CWD instruction, we get
(DX) = 11111111111111112 = FFFF16
That is, (AX) = FFA116 (DX) = FFFF16
Logic Instructions
The logic instructions include
AND
OR
XOR (Exclusive-OR)
NOT
Logic instructions : AND, OR, XOR, NOT
Allowed operands for AND, OR, and Allowed
operands
XOR instructions for NOT
instruction
EXAMPLE:
Describe the results of executing the following
instructions?
MOV AL, 01010101B
AND AL, 00011111B
OR AL, 11000000B
XOR AL, 00001111B
NOT AL
Solution:
(AL)=010101012 × 000111112= 000101012=1516
Executing the OR instruction, we get
(AL)= 000101012 +110000002= 110101012=D516
Executing the XOR instruction, we get
(AL)= 110101012 Å 000011112= 110110102=DA16
Executing the NOT instruction, we get
(AL)= (NOT)110110102 = 001001012=2516
EXAMPLE:
Masking and setting bits in a register
Solution:
Mask off the upper 12 bits of the word of data in AX
AND AX, 000F16
Setting B4 of the byte at the offset address
CONTROL_FLAGS
MOV AL, [CONTROL_FLAGS]
OR AL, 10H
MOV [CONTROL_FLAGS], AL
Executing the above instructions, we get
(AL)=XXXXXXXX2 +000100002=
XXX1XXXX2
Shift Instructions
The four shift instructions of the 8088
can perform two basic types of shift instructions: The
logical shift and the arithmetic shift.
Each of these operations can
performed to the right to or the left.
The shift instructions are shift logical
left (SHL), shift arithmetic left (SAL), Shift logical right
(SHR), and arithmetic right (SAR).
These instructions are used to align
data, to isolate bits of a byte or word so that it can be
tested and to perform simple multiply and divide
computations.
Shift instructions: SHL, SHR, SAL, SAR
Allowed operands for shift instructions
a) Results of executing SHL AX,1:
SHL
AX, 1
b) Results of executing SHR AX,CL with (CL)=02:
SHR
AX, CL
(CL)=2
c) Results of executing SAR AX,CL with(CL)-02:
SAR
AX, CL
(CL)=2
EXAMPLE:
Assume that CL contains 0216 and AX contains
091A16.
Determine the new contents of AX and the carry
flag after the instruction SAR AX, CL is executed
Solution:
(AX)=00000010010001102=024616
and the carry flag is (CF)=12
EXAMPLE:
Isolate the bit B3 of the byte at the offset
address CONTROL_FLAGS.
Solution:
MOV AL, [CONTROL_FLAGS]
MOV CL, 04H
SHR AL, CL
Executing the instructions, we get
(AL)=0000B7B6B5B4 and (CF)=B3
Rotate Instructions:
Rotate instructions similar to the
shift instructions.
Rotate instructions: ROL, ROR, RCL, RCR
Allowed operands for Rotate instructions: ROL, ROR,
RCL, RCR
a)Results for executing ROL AX,1:
ROL
AX, 1
b)Results of executing ROR AX,CL with (CL)=4:
ROR AX, CL
(CL)=4
For RCL, RCR, the bits are rotate through the carry flag
Rotation caused by execution of the RCL instructions:
EXAMPLE:
What is the result in BX and CF after execution of
the following instructions?
RCR BX, CL
Assume that, prior to execution of the instruction,
(CL)=0416, (BX)=123416, and (CF)=0
Solution:
The original contents of BX are
(BX) = 00010010001101002 = 123416
Execution of the RCR command causes a 4-bit
rotate right through carry to take place on the data
in BX, the results are
(BX) = 10000001001000112 = 812316
(CF) = 02
EXAMPLE:
Disassembly and addition of 2 hexadecimal digits
stored as a byte in memory.
Solution:
MOV AL, [HEX_DIGITS]
MOV BL, AL
MOV CL, 04H
ROR BL, CL
AND AL, 0FH
AND BL, 0FH
ADD AL, BL