Module 2 06012025
Module 2 06012025
Assuming that (BX)=100H, DI=200H, DS=1200H, SI= F002H, AX= 0105H, and the following
memory content. what is the result of executing the following instructions?
a. LEA SI , [ DI + BX +2H]
b. MOV SI , [DI+BX+2H]
c. LDS CX, [300]
d. LES BX , [DI+AX]
DATA TRANSFER INSTRUCTIONS
GENERAL PURPOSE DATA TRANSFER INSTRUCTIONS
IN/OUT Instructions
IN transfers a byte or word from an input port to the AL register or AX register.
OUT transfers a byte or a word from AL register or AX register respectively, to an output port.
IN/OUT instruction has two formats:
Fixed port: port number is specified directly in the instruction (port no: 0-255).
Variable port: port number is loaded into the DX before IN/OUT instruction (port no : 0 – 65535).
Example:
IN AL,0C8H ;Input a byte from port 0C8H to AL
IN AX, 34H ;Input a word from port 34H to AX
OUT 3BH, AL ;Copy the contents of the AL to port 3Bh
OUT 2CH, AX ;Copy the contents of the AX to port 2Ch
MOV DX, 0FF78H ;Initialize DX point to port MOV DX, 0FFF8H ;Load desired port address in DX
IN AL, DX ;Input a byte from a 8 bit port 0FF78H to AL OUT DX, AL ; Copy the contents of AL to FFF8h
IN AX, DX ;Input a word from 16 bit port to 0FF78H to AX. OUT DX, AX ;Copy content of AX to port FFF8H
FLAG TRANSFER INSTRUCTIONS
LAHF Instruction - Load Register AH From Flags : It copies
the value of SF, ZF, AF, PF, and CF, into bits of 7, 6, 4, 2, 0
respectively of AH register.
POPF Instruction - Pop word from top of stack to flag register: It copies a
word from the two memory location at the top of the stack to flag register
and increments the stack pointer by 2.
Restoring a previously saved flag status
Example
MOV AX, 1234H ; Load AX with some value
ADD AX, 5678H ; Perform addition, which affects FLAGS
PUSHF ; Save FLAGS register onto the stack ...
In Binary all the Signed Numbers have a '1' in the Most Significant Bit (MSB) position which
represents a negative number and a '0' in the Most Significant Bit (MSB) position which
represents a positive number.
Also, in Binary, the 2's Complement of a number is the negative equivalent of the positive
number. So, +2 = 0000 0010 and the 2's Complement is 1111 1110 which is - 2.
A 16 bit number system can be used to create 65536 combinations (from 0 to 65535),
and the first 32768 combinations (0 to 32767) represent positive numbers and next
32768 combinations (32768 to 65536) represent negative numbers.
INSTRUCTION SET – ARITHMETIC INSTRUCTIONS
ADDITION INSTRUCTIONS
ADD Instructions
This instruction adds the contents of source operand with the contents of destination
operand. The result is stored in destination operand.
The source may be immediate data, memory location or register.
The destination may be memory location or register.
Mnemonic Meaning Format Operation Flags Effected
ADD Addition ADD D, S (S)+(D) → (D), carry → (CF) O, S, Z, A, P, C
Example:
ADC CL, BL
Assume that CL = 01110011 =115 decimal, BL = 01001111 = 79 decimal and CF=1
Example:
INC AX
Assume AX = 7FFFh. After this instruction AX = 7FFFh + 1 = 8000h
INSTRUCTION SET – ARITHMETIC INSTRUCTIONS
ADDITION INSTRUCTIONS
DAA Instruction
Convert the result of the addition operation of 2 packed BCD numbers to a valid BCD.
It is used immediately after normal addition instruction operating on BCD codes.
It assumes the AL register as the source and the destination, and hence it requires no operand.
Example:
SUBB CL, BL
Assume that CL = 01110011 =115 decimal, BL = 01001111 = 79 decimal and CF=0
Example:
DEC AX
Assume AX = 7FFFh. After this instruction AX = 7FFFh - 1 = 7FFEh
INSTRUCTION SET – ARITHMETIC INSTRUCTIONS
SUBTRACTION INSTRUCTIONS
DAS Instruction
Convert the result of the subtraction operation of 2 packed BCD numbers to a valid BCD.
It is used immediately after normal subtraction instruction operating on BCD codes.
It assumes the AL register as the source and the destination, and hence it requires no operand.
75h=117; 46h=70
INSTRUCTION SET – ARITHMETIC INSTRUCTIONS
SUBTRACTION INSTRUCTIONS
AAS Instruction
Used to adjust the result of the two ASCII values that range from 30h (“0”) to 39h (“9”)
after subtraction
Numerical data coming into a computer from a terminal is usually in ASCII code, the numbers 0 to 9 are
represented by the ASCII codes 30H to 39H.
AAA instruction should be executed immediately after the SUB instruction that subtracts ASCII data
It assumes the AL register as the source and the destination, and hence it requires no operand.
Example:
MOV AX , 2CBh
NEG AX ;after executing NEG result AX =FD35h.
INSTRUCTION SET – ARITHMETIC INSTRUCTIONS
SUBTRACTION INSTRUCTIONS
CMP Instruction
Compares a byte/word in the specified source with a byte/word in the specified
Destination
The source can be an immediate number, a register, or a memory location and the destination can be a
register or a memory location. But, the source and the destination cannot both be memory locations.
The comparison is done by subtracting the source byte/word from the destination byte/word.
The source/destination are not changed, but the flags are set to indicate the results of the comparison.
Mnemonic Meaning Format Operation Flags Effected
CMP Compare CMP D,S (D)-(S), Affects only flags O, S, Z, A, P, C
Example:
CMP CX, BX
INSTRUCTION SET – ARITHMETIC INSTRUCTIONS
MULTIPLICATION INSTRUCTIONS
MUL Instruction
Multiple unsigned operand 8-bit/16-bit with AL/AX and store the result in AX/DX-AX.
The unsigned byte or word may be in any one of the general purpose registers or memory locations.
Immediate operand is not allowed. All flags are modified depending upon the result.
If operand is of 8-bit then multiply it with contents of AL. If 16-bit then multiply it with contents of AX.
Result is stored in accumulator AX in 8 bit operation and DX-AX in 16bit operation.
Mnemonic Meaning Format Operation Flags Effected
(AL)*(S8) → (AX)
MUL Multiply (Unsigned) MUL S Undefined
(AX)*(S16) → (DX)(AX)
IDIV Instruction
Divide a signed word by a signed byte, or to divide a signed double word by a signed word.
The locations of quotients, remainders, dividends and divisors are the same as in the DIV instruction
The sign of the remainder is the same as that of the dividend.
8086 will automatically generate a type 0 interrupt:
If an attempt is made to divide by 0
Quotient > +127 (7FH) or <–127 (81H) for signed word by a signed byte
Quotient > +32,767 (7FFFH) or <–32,767 (8001H) for signed double word by a signed word
Example:
MOV AX, 0206H ; AX=0206H
MOV BL, 05H ; BL=09
AAD ; AL = (AL + (10 * AH)) = (06+(10*02))=1AH (26 in decimal), AH=00H
DIV BL ; DIV BL =AX/BL =001AH/05H = 01H (Reminder) →AH, 05H (Quotient) → AL.
INSTRUCTION SET – ARITHMETIC INSTRUCTIONS
DIVISION INSTRUCTIONS
CBW Instruction
Converts the signed value in the AL register into an equivalent 16 bit signed value in the
AX register by duplicating the sign bit to the left.
This instruction copies the sign of a byte in AL to all the bits in AH.
Example:
CBW
Example:
CWD
Logical instructions to perform bit by bit logic operation on the specified source and
destination operands.
Shift instructions can perform two basic types of shift operations; the logical shift and the
arithmetic shift. Also, each of these operations can be performed to the right or to the left.
Rotate instructions, rotate the contents from 1 to 255 bit positions to the left or to the right.
LOGICAL INSTRUCTIONS
NOT Instruction
Inverts each bit (forms the 1’s complement) of a byte or word in the specified destination.
The destination can be a register or a memory location.
Example:
NOT AL
TEST Instruction
Similar to AND instruction but result is not stored anywhere, affects only flag bits.
LOGICAL INSTRUCTIONS
OR Instruction
Performs bit-wise OR operation on Destination and Source operand.
Used to set certain bits. Ex.: Set the lower three bits of BL register
OR BL, 07H ; (xxxx xxxx OR 0000 0111 = xxxx x111)
Mnemonic Meaning Format Operation Flags Effected
OR Logical OR OR D, S (D) + (S) → (D) O, S, Z, P, C
Note that the amount of shift specified in the source operand can be defined explicitly if it
is one bit or should be stored in CL if more than 1.
SHIFT INSTRUCTIONS
MODULE-2
SHIFT INSTRUCTIONS
Along with above string instructions 8086 uses auto-indexing and prefix instructions in
order to perform effective block transfer of memory content from DS to ES.
STRING INSTRUCTIONS
STRING INSTRUCTIONS
Mnemonic Meaning Format Operation Flags Effected
((DS)*10+(SI))→((ES)*10+ (DI))
MOVSB/
MOVS Move string (SI)± 1 → (SI); (DI) ± 1 → (DI) [byte] None
MOVSW
(SI) ± 2 → (SI); (DI) ± 2 → (DI) [word]
((DS)*10+ (SI))→(AL) or (AX)
LODSB/
LODS Load string (SI) ± 1 →(SI) [byte] None
LODSW
(SI) ± 2 → (SI) [word]
(AL) or (AX) →((ES)*10+ (DI))
STOSB/
STOS Store string (DI) ± 1 → (DI) [byte] None
STOSW
(DI) ± 2 → (DI) [word]
((DS)*10+(SI))-((ES)*10+ (DI))
CMPSB/
CMPS Compare string (SI) ± 1 → (SI); (DI) ± 1 → (DI) [byte] O, S, Z, A, P, C
CMPSW
(SI) ± 2 → (SI); (DI) ± 2 → (DI) [word]
(AL) or (AX) -((ES)*10+ (DI))
SCASB/
SCAS Scan string (DI) ± 1 → (DI) [byte] O, S, Z, A, P, C
SCASW
(DI) ± 2 → (DI) [word]
STRING INSTRUCTIONS
AUTO-INDEXING OF STRING INSTRUCTIONS
The decision to increment or decrement is made based on the status of the direction flag.
The direction Flag: Selects the auto increment (D=0) or the auto decrement (D=1)
operation for the DI and SI registers during string operations.
For example, REP, caused the basic string operation to be repeated until the contents of
register CX become equal to 0.
Each time the instruction is executed, it causes CX to be tested for 0.
If CX is found not to be 0, it is decremented by 1 and the basic string operation is repeated.
If it is 0, the repeat string operation is done and the next instruction in the program is executed.
The repeat count must be loaded into CX prior to executing the repeat string instruction.
STRING INSTRUCTIONS
MODULE-2
CONTROL TRANSFER INSTRUCTIONS
UNCONDITIONAL TRANSFER INSTRUCTIONS
JMP Instruction (Unconditional)
JMP (Jump) unconditionally transfers control from one CS to another or within CS.
These locations can be within the same code segment (near control transfers) or in
different code segments (far control transfers).
There are two basic kinds of unconditional jumps:
• Intrasegment Jump: is limited to addresses within the current code segment. This type of jump is
achieved by just modifying the value in IP.
• Intersegment Jump: permit jumps from one code segment to another. Implementation of this type of
jump requires modification of the contents of both CS and IP.
Mnemonic Meaning Format Operation Flags Effected
Jump to the target address mentioned in the
JMP Unconditional Jump JMP target None
instruction
CONTROL TRANSFER INSTRUCTIONS
UNCONDITIONAL TRANSFER INSTRUCTIONS
Type
Iteration Control
Mnemonics
JMP Instruction Operation Example
Instructions
Short Jump Jump to address location specified as 8-bit constant
JMP 8-bit JMP 45H
[8-bit] value (8-bit) → (IP)
Jump to address location specified as 16-bit constant
JMP 16-bit JMP 1234H
Intersegment value (16-bit) → (IP)
Jump Near Jump Jump to the address location given in the register
JMP Reg. JMP SI
[16-bit] (Reg.) → (IP)
Jump to the address location pointed by the content
JMP [Reg.] JMP [SI]
present in the register [(Reg.)] → (IP)
Jump to address location specified as 16-bit constant JMP
JMP 16-bit:16-bit
value (First 16-bit) → (IP), (Second 16-bit) → (CS) 2000:1234
Intersegment
Far Jump Jump to the address location given in the registers
Jump
[32-bit] [(Reg.1)] → (IP), [(Reg.2)] → (CS)
JMP [Reg.1][Reg.2] JMP [BX][SI]
CONTROL TRANSFER INSTRUCTIONS
UNCONDITIONAL TRANSFER INSTRUCTIONS
JMP Instruction (Unconditional)
Example: Assume the following state of 8086:(CS)=1075H, (IP)=0300H, (SI)=A00H, (BH)=20H,
(BL)=70H, (DS)=400H, (DS:A00)=10H, (DS:A01)=B3H, (DS:A20)=22H, (DS:A70)=1AH. To what
address is program control passed if each of the following JMP instruction is execute?.
Example Logical Address Jump Type
(a) JMP 85 ⇒ 1075:85 ⇒Short jump
(b) JMB 1000H ⇒ 1075:1000 ⇒Near jump
(c) JMP SI ⇒ 1075:0A00 ⇒Near jump
(d) JMP [SI] ⇒ 1075: B310 ⇒Near jump
(e) JMP 3000:1000 ⇒3000:1000 ⇒ Far jump
(f) JMP [BX][SI] ⇒1A22: B310 ⇒ Far jump
CONTROL TRANSFER INSTRUCTIONS
UNCONDITIONAL TRANSFER INSTRUCTIONS
CALLL and RET Instruction
A subroutine is a special segment of program that can be called for execution form any
point in program.
There two basic instructions for subroutine :
• CALL instruction is used to call the subroutine
• RET instruction is used to initiate the return sequence to the main program environment.
Subroutine A
First Instruction
Call subroutine A Example for CALL:
Next instruction CALL 1234h
CALL BX
CALL [BX]
CALL DWORD PTR [DI]
Return
Call subroutine A
Next instruction
CONTROL TRANSFER INSTRUCTIONS
CONDITIONAL TRANSFER INSTRUCTIONS
Jxx Instruction (Conditional)
The conditional jump instructions test the following flag bits: S, Z, C, P, and O.
If the condition under test is true, a branch to the label associated with jump instruction
occurs.
If the condition is false, the next sequential step in the program executes.