Documente Academic
Documente Profesional
Documente Cultură
4
Example:
NEG AL
NEG BX
o CMP-Compare Byte or Word
The comparison is done by subtracting the source byte or word
from the destination byte or word.
o Format: operand1 - operand2
Example:
MOV AL, 5
MOV BL, 5
CMP AL, BL ; AL = 5, ZF = 1
5
CMP CX, BX CF, ZF, and SF will be left as follows:
CF ZF SF
CX = BX 0 1 0 Result of subtraction is 0
CX > BX 0 0 0 No borrow required, so CF = 0
CX < BX 1 0 1 Subtraction required borrow, so CF = 1
• AAS--ASCII Adjust After Subtraction
The 8086 allows us to subtract the ASCII codes for two
decimal digits.
The AAS instruction is then used to make sure the result is
the correct BCD number.
Example: ASCII 9-ASCII 5 (9-5)
AL = 00111001 = 39H = ASCII 9
BL = 001 10101 = 35H = ASCII 5
SUB AL, BL Result: AL = 00000100 = BCD 04
AAS Result: AL = 00000100 = BCD 04.
6
DAS-Decimal Adjust after BCD Subtraction
Example: AL 1000 0110 86 BCD
BH 0101 0111 57 BCD
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 = 00101001 29 BCD
Multiplication Instruction
o MUL---Multiply Unsigned Bytes or Words
8
oAAM-ASCII Adjust after Multiplication
• Before we can multiply two ASCII digits, we must first mask the
upper 4 bits of each. Corrects the result of multiplication of two
BCD values.
After the two unpacked BCD digits are multiplied, the AAM
instruction is used to adjust the product to two unpacked BCD
digits In AX.
EXAMPLE:
AL 00000101 unpacked BCD 5
BH 00001001 unpacked BCD 9
MUL BH AL x BH; result in AX
AX = 00000000 00101101 =
002DH
AAM AX = 00000100 00000101 = 045H,
which is unpacked BCD for 45.
9
Division Instruction
• DIV-Unsigned Divide
• This instruction is used to divide an unsigned word by a byte or
to divide an unsigned double word (32 bits) by a word.
• Example:
MOV AX, 203 ; AX = 00CBh
MOV BL, 4
DIV BL ; AL = 50 (32h), AH = 3
RET
IDIV-Divide by Signed Byte or Word
• This instruction is used to divide a signed word by a signed byte,
or to divide a signed double word (32 bits) by a signed word.
• Example: MOV AX, -405
MOV BL, 4
IDIV BL ; AL = -101 , AH = -1
RET 10
AAD- BCD Adjust before Division.
• Prepares two BCD values for division.
• Example:
MOV AX, 0309 ;dividend in AX in BCD.
AAD ;BCD in AX converted to hex
and placed in AL
MOV BL, 04 ;divisor in BL in binary.
DIV BL ; divide AX by BL.
11
3. Bit Manipulation Instructions:
Logical Instructions:
• NOT- Invert each bit of the operand.
• Format:
• If bit is 1 turn it to 0.
• If bit is 0 turn it to 1.
• Example:
MOV AL, 00011011b
NOT AL ; AL = 11100100b
RET
• AND-Logical AND between all bits of two operands.
• These rules apply:
• 1 AND 1 = 1
• 1 AND 0 = 0
• 0 AND 1 = 0
• 0 AND 0 = 0
12
• Example:
MOV AL, 'a' ; AL = 01100001b
AND AL, 11011111b ; AL = 01000001b ('A')
RET
OR- OR Logical between all bits of two operands.
• These rules apply:
• 1 OR 1 = 1
• 1 OR 0 = 1
• 0 OR 1 = 1
• 0 OR 0 = 0
• Example:
MOV AL, 'A' ; AL = 01000001b
OR AL, 00100000b ; AL = 01100001b ('a')
RET
13
o XOR -Logical XOR (Exclusive OR) between all bits
of two operands.
• These rules apply:
• 1 XOR 1 = 0
• 1 XOR 0 = 1
• 0 XOR 1 = 1
• 0 XOR 0 = 0
• Example:
MOV AL, 00000111b
XOR AL, 00000010b ; AL = 00000101b
RET
14
o Shift Instructions:
o SHL-Shift Operand Bits Left, Put Zero in LSB(s)
o CF ← MSB ← LSB ← 0
Example: MOV AL, 11100000b
SHL AL, 1 ; AL = 11000000b, CF=1.
RET
15
o ROL-Rotate Bits of Operand Left, MSB to LSB
Example1: ROL AX, 1 Word in AX 1bit position left, MSB to
LSB and CF
• Example2 :
MOV AL, 1Ch ; AL = 00011100b
ROL AL, 1 ; AL = 00111000b, CF=0.
RET
16
oROR-Rotate Bits of Operand Right, LSB to MSB
• Example:
MOV AL, 1Ch ; AL = 00011100b
ROR AL, 1 ; AL = 00001110b, CF=0.
RET
17
oRCR-Rotate Operand to the Right through CF
• Shift all bits right, the bit that goes off is set to CF and previous
value of CF is inserted to the leftmost position.
• Example:
STC ; set carry (CF=1).
MOV AL, 1Ch ; AL = 00011100b
RCR AL, 1 ; AL = 10001110b, CF=0.
RET
o MOVS/MOVSB/MOVSW -Move String Byte or String Word
This instruction copies a byte or a word from a location in the data
segment to a location in the extra segment.
The offset of the source byte or word in the data segment must be in
the SI register.
The offset of the destination in the extra segment must be contained
in the DI register.
18
Example:
MOV SI, OFFSET SOURCE_STRING Load offset of start of source string in
DS into SI
MOV DI, OFFSET DESTINATION-STRING Load offset of start of destination
string in ES into DI
21
oREP/REPE/REPZ/REPNE/REPNZ- (Prefix) Repeat
String Instruction until Specified Conditions Exist
REP is a prefix, which is written before one of the string instructions. It will
cause the CX register to be decremented and the string instruction to be repeated
until CX = 0.
REPE and REPZ are two mnemonics for the same prefix.
They stand for Repeat if Equal and Repeat if Zero, respectively.
REPE or REPZ is often used with the Compare String instruction or with the
Scan String instruction.
REPE or REPZ will cause the string instruction to be repeated as long as the
compared bytes or words are equal (ZF = 1) and CX is not yet counted down to
zero.
In other words, there are two conditions that will stop the repetition: CX = 0 or
string bytes or words not equal.
22
Conditional Transfer Instructions:
JA/JNBE Jump if above/jump if not below or equal.
JAE/JNB Jump if above or equal/jump if not below.
JB/JNAE Jump if below/jump if not above or equal.
JBE/JNA Jump if below or equal/jump if not above.
JC Jump if carry flag CF = 1.
JE/JZ Jump if equal/jump if zero flag ZF = 1.
JG/JNLE Jump if greater/jump if not less than or equal.
JGE/JNL Jump if greater than or equal/ Jump if not less than.
JL/JNGE Jump if less than/jump if not greater than or-equal.
JLE/JNG Jump if less than or equal/jump if not greater than.
JNC Jump if no carry (CF = 0).
JNE/JNZ Jump if not equal/jump if not zero (ZF = 0).
JNO Jump if no overflow (overflow flag OF = 0).
JNP/JPO Jump if not parity/jump if parity odd (PF = 0).
JNS Jump if not sign (sign flag SF= 0).
JO Jump if overflow flag OF = 1.
JP/JPE Jump if parity/jump if parity even (PF = 1).
JS Jump if sign (SF=1) 23
• Iteration Control Instructions:
LOOP Loop through a sequence of instructions until CX =
0.
LOOPE/LOOPZ Loop through a sequence of instructions while ZF
=1 and CX ≠ 0.
LOOPNE/LOOPNZ Loop through a sequence of instructions while ZF = 0 and
CX ≠ 0.
JCXZ Jump to specified address if CX = 0.
• Interrupt Instructions:
24
Flag Set/Clear Instructions:
STC Set carry flag CF to 1.
CLC Clear carry flag CF to 0.
CMC Complement the state of the carry flag CF.
STD Set direction flag DF to 1.
CLD Clear direction flag DF to 0.
STI Set interrupt enable flag to 1 (enable INTR input).
CLI Clear interrupt enable flag to 0 (disable INTR input).
25
o External Hardware Synchronization Instructions:
HLT Halt (do nothing) until interrupt or reset.
WAIT Wait (do nothing) until signal on the TEST pin is low.
ESC Escape to external coprocessor such as 8087 or 8089
LOCK An instruction prefix. Prevents another processor from
taking the bus (in MAX mode)
No Operation Instruction:
26
Examples
1. To Copy a String from one set of locations to another
data segment
testmsg db 'Mekelle Institute of Technology'
len dw 30
data ends
dst segment
dest db 30 dup(0)
dst ends
code segment
assume cs:code,ds:data,es:dst
start:mov ax,data
mov ds,ax
mov ax,dst
mov es,ax
lea si,testmsg
lea di,dest
mov cx,len
cld
rep movsb
mov ah,4ch
int 21h
code ends
end start
27
2. Program to find the largest of 5 bytes
data segment
arr db 09,02,99,02,05
count dw 05h
lar db ?
data ends
code segment
assume cs:code,ds:data
start: mov ax,data
mov ds,ax
mov si,offset[arr]
mov cx,count
dec cx
mov al,[si]
up: inc si
cmp al,[si]
jnc down1
mov al,[si]
down1: loop up
mov lar,al
int 3
code ends
end start
28
3. To Sort an array in ascending order (Using Bubble
Sorting Method)
data segment
arr db 5, 4, 3, 1, 2
count dw 05
data ends
code segment
assume cs:code, ds:data
start:mov ax,data
mov ds,ax
mov cx,count
dec cx
up2: mov dx,cx
mov bx,0000h
up1: mov al,arr[bx]
cmp al,arr[bx+1]
jbe down
xchg al,arr[bx+1]
mov arr[bx],al
down: inc bx
loop up1
mov cx,dx
loop up2
int 3h
code ends
end start
29
END OF CHAPTER 2
30