Part 2
Part 2
2/3/2024 1
Data Transfer Instructions
2/3/2024 2
Data Transfer instructions essentially
copy the data and do not affect any flags
(except of course the POPF instruction
which modifies all the flags as per the
stack top word)
2/3/2024 3
MOV
Stands for move, it is actually copy, that is, when
data is moved from one register source to a
destination register, source is not destroyed,
only, there will be a copy of this data in the
destination register
2/3/2024 4
XCHG
Exchange instruction exchanges data between
registers or between register and memory
2/3/2024 5
PUSH
2/3/2024 6
POP
Pop causes the stack top moved to (that is,
removed from the stack and loaded onto) the
destination register or memory specified by the
instruction
2/3/2024 7
IN: Copy data from a port
IN accumulator, port
The IN instruction reads from an input port into
AL or AX (only these two registers), to be
specified in the instruction
2/3/2024 8
Ex:
IN AL,0C8H;Input a byte from port address 0C8H to AL
IN AX,34H; Input a word from port address 034H to AX
2/3/2024 9
OUT: output a byte or word to a port
OUT port, accumulator
2/3/2024 10
Examples:
out 16h, ax; write to output port at address 16h from reg. ax
out 23h, al; write to output port at address 23h from reg al
2/3/2024 11
CBW (Convert byte to word)
The source register AL and the destination AX
are both implied and not specifically mentioned
in this instruction
2/3/2024 12
If the number in AL is positive, AH will be loaded
with 00 hex, else AH will be loaded with FF hex
Example: cbw
2/3/2024 13
CWD (Convert Word to Double Word)
Example: cwd
2/3/2024 14
Among all the data transfer instructions
popf is the only instruction that affects and
modifies the flags
2/3/2024 15
Binary Arithmetic Instructions
2/3/2024 16
All binary arithmetic instructions update
the flag register based on the result of the
operation performed
2/3/2024 17
ADD
Adds the first operand (destination operand) and
the second operand (source operand) and
stores the result in the destination operand
2/3/2024 18
When an immediate value is used as an operand, it is
sign-extended to the length of the destination operand
format
The OF, SF, ZF, AF, PF, and CF flags are set according
to the result
The OF, SF, ZF, AF, PF, and CF flags are set
according to the result
2/3/2024 20
Example…
data segment
a db 0ABh
b db 0A5h
c dw ?
data ends
code segment
assume cs:code, ds:data
start:
mov ax, data
mov ds, ax
mov al, a
add al, b
mov ah, 00h
adc ah, 00h
mov c, ax
mov ah, 4ch
int 21h
code ends
end start
2/3/2024 21
SUB
Subtraction
The OF, SF, ZF, AF, PF, and CF flags are set
according to the result
2/3/2024 22
SBB
Subtract with borrow
The OF, SF, ZF, AF, PF, and CF flags are set
according to the result
2/3/2024 23
CMP
2/3/2024 24
When an immediate value is used as an operand, it is
sign extended to the length of the first operand
Operation:
The CF, OF, SF, ZF, AF, and PF flags are set
according to the result
2/3/2024 25
Examples:
CMP BX, SI
2/3/2024 26
MUL
2/3/2024 27
The source operand is located in a general-
purpose register or a memory location
Operation:
IF byte operation
THEN
AX ← AL ∗ SRC
ELSE (* word operation *)
DX:AX ← AX ∗ SRC
2/3/2024 28
Flags Affected
Examples:
MUL BX
MUL WORDPTR [BX + DI]48 H
MUL BYTEPTR [SI]
MUL CL
2/3/2024 29
IMUL : multiply signed numbers
IMUL source
Integer (signed) multiply
2/3/2024 30
Examples:
IMUL BH ;Signed byte in AL times signed byte
in BH ; result in AX
;69x14
;AL=01000101=45h=69 decimal
;BH=0000 1110=0Eh=14 decimal
IMUL BH;AX=03C6H=+966 decimal
; MSB=0,positive result magnitude in true form.
;-28x59
;AL=11100100=E4H=-1CH=-28 decimal
;BH=00111011=3BH=+59 decimal
IMUL BH;AX=F98CH=-1652 decimal
;MSB=1,negative result magnitude in 2’s complement form.
2/3/2024 31
DIV:
2/3/2024 32
If the divisor specified in the instruction is a byte
register or byte memory, then the accumulator
will be the word register AX
2/3/2024 33
Execution of DIV instruction in such a case will
cause a division overflow exception to be
generated, and operating system should take
care of this exception
2/3/2024 34
Examples of DIV instruction:
DIV BX;
DIV WORDPTR [DI];
DIV CL;
DIV BYTEPTR [SI];
2/3/2024 35
IDIV : devide by signed byte or word
IDIV source
2/3/2024 36
Examples:
IDIV BH ;Signed word in AX/ signed byte in BH;
2/3/2024 37
INC
Increment register or memory
2/3/2024 38
This instruction allows a loop counter to be updated
without disturbing the CF flag
Operation:
DEST ← DEST + 1;
2/3/2024 39
DEC
Similar to INC, this instruction does the
operation:
DEST ← DEST – 1;
2/3/2024 40
NEG: Form 2’s complement
NEG destination
Replaces the value of operand (the destination
operand) with its two’s complement
2/3/2024 41
Flags Affected
2/3/2024 42
Decimal (BCD, ASCII)
Arithmetic Instructions
2/3/2024 43
DAA: Decimal adjust accumulator (AL) after BCD
addition
Adjusts the sum of two packed BCD values to
create a packed BCD result
2/3/2024 44
The DAA instruction then adjusts the contents of
the AL register to contain the correct 2-digit,
packed BCD result
2/3/2024 45
If the result in the upper nibble of AL is now
greater then 9 or if the carry flag was set by the
addition or correction, then DAA instruction will
add 60H to AL
2/3/2024 46
Example 1:
; AL = 0101 1001 = 59 BCD
; BL = 0011 0101 = 35 BCD
ADD AL, BL ; AL = 1000 1110 = 8EH
DAA ; Add 0110 Because 1110 > 9
; AL = 1001 0100 = 94 BCD
2/3/2024 47
Example 2:
; AL = 1000 1000 = 88 BCD
; BL = 0100 1001 = 49 BCD
ADD AL, BL ; AL = 1101 0001=D1h, AF = 1
DAA ; Add 0110 because AF=1
; AL = 1101 0111 = D7H
; 1101 > 9 so add 0110 0000
; AL = 0011 0111 = 37 BCD, CF = 1
2/3/2024 48
Flags affected
2/3/2024 49
DAS: Decimal adjust after BCD subtraction
Adjusts the result of the subtraction of two packed
BCD values to create a packed BCD result
2/3/2024 50
The DAS instruction then adjusts the contents of the
AL register to contain the correct 2-digit, packed
BCD result
2/3/2024 51
Examples:
;AL=1000 0110=86BCD
; BH=0101 0111=57BCD
SUB AL,BH ;AL=0010 1111=2FH,CF=0
DAS ;Lower nibble of result is 1111,so DAS automatically subtracts
0000 0110 to give AL=0010 1001=29BCD
;AL=0100 1001=49BCD
;BH=0111 0010=72BCD
SUB AL,BH ;AL=1101 0111=D7H,CF=1
DAS ;Subtracts 0110 0000(-60H) bcoz 1101 in upper nibble >9
;AL=01110111=77BCD,CF=1
;CF=1 means borrow was needed
2/3/2024 52
AAA: ASCII adjust AL after addition
Adjusts the sum of two unpacked BCD values to
create an unpacked BCD result
2/3/2024 54
The AAA instruction then adjusts the contents of the
AL register to contain the correct 1-digit unpacked
BCD result
2/3/2024 56
IF ((AL AND 0FH) > 9) OR (AF = 1)
THEN
AL ← AL + 6;
AH ← AH + 1;
AF ← 1;
CF ← 1;
ELSE
AF ← 0;
CF ← 0;
AL ← AL AND 0FH;
2/3/2024 57
AAS: ASCII adjust AL after subtraction
Adjusts the result of the subtraction of two
unpacked BCD values to create a unpacked BCD
result
2/3/2024 59
Example:
;assume BL=0011 1001=39h=ASCII 9
AL=0011 0101=35h=ASCII5
ASCII9-ASCII5
SUB AL,BL ; Result: AL=0000 0100=BCD 04 and CF=0
AAS ; result:AL=0000 0100= BCD 04 and CF=0;no borrow required
2/3/2024 60
IF ((AL AND 0FH) > 9) OR (AF = 1)
THEN
AL ← AL – 6;
AH ← AH – 1;
AF ← 1;
CF ← 1;
ELSE
CF ← 0;
AF ← 0;
AL ← AL AND 0FH;
2/3/2024 61
AAM: ASCII adjust AX after multiply
Adjusts the result of the multiplication of two
unpacked BCD values to create a pair of
unpacked (base 10) BCD values
Example:
;AL=0000 0101=unpacked BCD 5
; BH=0000 1001=unpacked BCD 9
MUL BH ;AL x BH; result in AX
;AX=00000000 00101101=002DH
AAM ;AX=00000100 00000101=0405H
;which is unpacked BCD for 45
2/3/2024 63
AAD: ASCII adjust AX before division
2/3/2024 64
The AAD instruction sets the value in the AL register
to (AL + (10 * AH)), and then clears the AH register
to 00H
2/3/2024 65
Example:
;AX =0607H unpacked BCD for 67 decimal
;CH=09H
AAD; result: AX=0043=43H=67 decimal
DIV CH; devide AX by unpacked BCD in CH
;quotient:AL=07 unpacked BCD
;remainder : AH=04 unpacked BCD
;flags undefined after DIV
2/3/2024 66
Logical Instructions
2/3/2024 67
AND
2/3/2024 68
Each bit of the result is set to 1 if both
corresponding bits of the first and second
operands are 1; otherwise, it is set to 0
Operation:
DEST ← DEST AND SRC;
Flags Affected:
The OF and CF flags are cleared; the SF, ZF,
and PF flags are set according to the result; The
state of the AF flag is undefined
2/3/2024 69
OR
Performs a bitwise inclusive OR operation
between the destination (first) and source
(second) operands and stores the result in the
destination operand location
2/3/2024 70
Each bit of the result of the OR instruction is set
to 0 if both corresponding bits of the first and
second are 0; otherwise it is set to 1
Operation:
DEST ← DEST OR SRC;
Flags Affected:
The OF and CF flags are cleared; the SF, ZF,
and PF flags are set according to the result; The
state of the AF flag is undefined
2/3/2024 71
XOR
2/3/2024 72
Each bit of the result is 1 if the corresponding
bits of the operands are different; each bit is 0 if
the corresponding bits are the same
Operation:
DEST ← DEST XOR SRC;
Flags Affected:
The OF and CF flags are cleared; the SF, ZF,
and PF flags are set according to the result; The
state of the AF flag is undefined
2/3/2024 73
NOT
2/3/2024 74
Operation:
DEST ← NOT DEST;
Flags Affected:
None
2/3/2024 75
TEST
Bitwise AND the two sources operands, ignore the
outcome, but preserve the nature of the result in
the flag register
2/3/2024 76
Operation:
2/3/2024 77
Shift and Rotate
Instructions
2/3/2024 78
Shift and rotate instructions shift the data by one
or more bits towards either left or right, straight
or in a circular fashion
2/3/2024 79
Shift Instructions…
2/3/2024 80
The logical shifts move a 0 into the
rightmost bit position for a logical left shift and
a 0 into the leftmost bit position for a logical
right shift
2/3/2024 81
The arithmetic and logical right shifts are
different because the arithmetic right shift
copies the sign-bit through the number,
while the logical right shift copies a 0
through the number
2/3/2024 82
Logical shifts multiply or divide unsigned
data, and arithmetic shifts multiply or divide
signed data
2/3/2024 83
The logical shift instructions are SHL, SHR
2/3/2024 84
SHL – Shift Operand Bits Left, Put Zero in
LSBs
2/3/2024 85
As a bit is shifted out of the LSB position, a
zero is put in the LSB position
CF MSB LSB 0
2/3/2024 86
If the desired number of shifts is 1, this can be
specified by putting a 1 in the count position of the
instruction
Example:
MOV CL, 05H
SHL AX, CL
2/3/2024 87
SAL – Shift Operand Bits Left, Put Zero in
LSBs
2/3/2024 88
As a bit is shifted out of the LSB position, a
zero is put in the LSB position
CF MSB LSB 0
2/3/2024 89
If the desired number of shifts is 1, this can be
specified by putting a 1 in the count position of the
instruction
Example:
MOV CL, 05H
SAL AX, CL
2/3/2024 90
SHR – Shift Operand Bits Right, New MSB = 0
2/3/2024 91
This instruction shifts each bit in the specified
destination some number of bit positions to
the right
2/3/2024 92
0 MSB LSB CF
2/3/2024 93
If the desired number of shifts is 1, this can be
specified by putting a 1 in the count position of
the instruction
Example:
MOV CL, 03H
SHR AL, CL
2/3/2024 94
SAR
2/3/2024 95
This Arithmetic shift right instruction copies
the sign bit through the number
2/3/2024 96
If the desired number of shifts is 1, this can be
specified by putting a 1 in the count position of the
instruction
Example:
MOV CL, 03H
SAR AL, CL
2/3/2024 97
ROL – Rotate All Bits of Operand Left, MSB
to LSB
ROL Destination, Count
This instruction rotates all the bits in a
specified word or byte to the left some number
of bit positions
2/3/2024 98
If the desired number of shifts is 1, this can
be specified by putting a 1 in the count
position of the instruction
Example:
MOV CL, 03H
ROL AL, CL
2/3/2024 99
ROR – Rotate All Bits of Operand Right,
LSB to MSB
ROR Destination, Count
This instruction rotates all the bits in a
specified word or byte to the right some
number of bit positions
CF MSB LSB
2/3/2024 100
If the desired number of shifts is 1, this can be
specified by putting a 1 in the count position of the
instruction
Example:
MOV CL, 03H
ROR AL, CL
2/3/2024 101
For the ROL and ROR instructions, the original
value of the CF flag is not a part of the result,
but the CF flag receives a copy of the bit that
was shifted from one end to the other
2/3/2024 102
RCL (Rotate Left including carry)
The RCL instruction shifts the CF flag into the
least-significant bit and shifts the most
significant bit into the CF flag
2/3/2024 103
RCR (Rotate Right including carry)
The RCR instruction shifts the CF flag into the
most-significant bit and shifts the least-
significant bit into the CF flag
2/3/2024 104
2/3/2024 105
Control Transfer
Instructions
2/3/2024 106
The intelligence in any program lies in the ability
of the program to follow different courses of
action based on intermediate results produced
during the working of the program; that way, the
program is enabled to perform data sensitive
tasks
2/3/2024 107
JMP: Jump instruction
2/3/2024 108
Near jump—A jump to an instruction within the
current code segment (the segment currently
pointed to by the CS register), sometimes referred to
as an intra segment jump
2/3/2024 110
MNEMONIC CONDITION TESTED
2/3/2024 113
LOOP Instruction
LOOP LABEL
2/3/2024 114
LOOP instruction combines 2 operations in each
instruction
2/3/2024 115
LOOP(jump to specified label if cx≠0 Loop Until CX=0
after auto decrement)
2/3/2024 116
CALL
Call instruction is a returnable jump to the destination or the
target address provided in the instruction
2/3/2024 117
When executing a near call, the processor
pushes the value of the IP register (which
contains the offset of the instruction following the
CALL instruction) onto the stack (for use later as
a return-instruction pointer)
2/3/2024 118
When executing a far call, the processor pushes
the current value of both the CS and IP registers
onto the stack for use as a return-instruction
pointer
2/3/2024 119
RET
The RET (return) instruction returns control back
from the procedure to the program that has
called the procedure
2/3/2024 120
The address is usually placed on the stack by a
CALL instruction, and the return is made to the
instruction that follows the CALL instruction
2/3/2024 121
INT –interrupt program execution
INT type
These instructions are software interrupt procedure
calls
2/3/2024 122
When an 8086 executes an INT instruction it will
2/3/2024 123
Get a new value of IP from an absolute memory address
of 4 times the type specified in the instruction
Ex: for INT 8 instruction, a new IP will be read from
address 00020H.
2/3/2024 124
IRET: Return from interrupt
2/3/2024 125