Professional Documents
Culture Documents
Chapter 4. Instruction Sets
Chapter 4. Instruction Sets
DIV Example:
CMP ADC AL, 74H
ADC DX, AX
2. Arithmetic Instructions
ADD
ADC Increment
DEC
SBB reg, data (reg) (reg) – data - CF
NEG SBB mem, data (mem) (mem) - data - CF
DIV reg
ADD For 16-bit ÷ 8-bit :
(AL) (AX) ÷ (reg8) Quotient
ADC (AH) (AX) MOD(reg8) Remainder
INC For 32-bit ÷ 16-bit :
SUB (AX) (DX)(AX) ÷ (reg16) Quotient
(DX) (DX)(AX) MOD(reg16) Remainder
SBB
DEC DIV mem
For 16-bit ÷ 8-bit :
(AL) (AX) ÷ (mem8) Quotient
NEG (AH) (AX) MOD(mem8) Remainder
OR
NOT reg8 (reg8) (reg8)’
XOR
NOT reg16 (reg16) (reg16)’
TEST
NOT mem (mem) (mem)’
No flags are affected by the NOT
Instruction.
Example:
NOT BX ;Complement contents of BX register
3.1. Logical instructions
AND
NOT
AND reg2/mem, reg1/mem
AND
AND reg2, reg1 (reg2) (reg1) & (reg2)
OR AND reg2, mem (reg2) (reg2) & (mem)
AND mem, reg1 (mem) (mem) & (reg1)
XOR
AND reg/mem, data
TEST
AND reg, data (reg) (reg) & data
AND mem, data (mem) (mem) & data
CF and OF are both 0 after AND. PF, SF, and
ZF are updated by AND. AF is undefined.
Example:
AND BH, CL ;AND byte in CL with byte in BH. Result in BH
AND BX, 00FFH ; AND word in BX with immediate value 00FFH.
3.1. Logical instructions
OR
NOT
OR reg2/mem, reg1/mem
AND
OR reg2, reg1 (reg2) (reg1) | (reg2)
OR OR reg2, mem (reg2) (reg2) | (mem)
OR mem, reg1 (mem) (mem) | (reg1)
XOR
OR reg/mem, data
TEST
OR reg, data (reg) (reg) | data
OR mem, data (mem) (mem) | data
CF and OF are both 0 after OR. PF, SF, and ZF
are updated by OR. AF is undefined.
Example:
OR BH, CL ;CL ORed with BH. Result in BH
OR BX, 00FFH ; BX ORed with immediate value 00FFH.
3.1. Logical instructions
Exclusive OR
NOT
XOR reg2/mem, reg1/mem
AND
XOR reg2, reg1 (reg2) (reg1) ^ (reg2)
OR XOR reg2, mem (reg2) (reg2) ^ (mem)
XOR mem, reg1 (mem) (mem) ^ (reg1)
XOR XOR reg/mem, data
TEST
XOR reg, data (reg) (reg) ^ data
XOR mem, data (mem) (mem) ^ data
CF and OF are both 0 after XOR. PF, SF, and
ZF are updated by XOR. AF is undefined.
Example:
XOR BH, CL ;CL XORed with BH. Result in BH
XOR BX, 00FFH ; BX XORed with immediate 00FFH.
3.1. Logical instructions
ANDs the contents of a source byte or word
NOT with the contents of the specified destination
word.
AND Flags are updated, but neither operand is
OR changed.
XOR The TEST instruction is often used to set flags
before a Conditional Jump instruction.
TEST TEST reg2/mem, reg1/ mem TEST Operand2, Operand1
For CMPSB:
If DF = 0, then (DI) (DI) + 1; (SI) (SI) + 1
REP If DF = 1, then (DI) (DI) - 1; (SI) (SI) – 1
For CMPSW:
If DF = 0, then (DI) (DI) + 2; (SI) (SI) + 2
If DF = 1, then (DI) (DI) - 2; (SI) (SI) - 2
4. String Manipulation Instructions
Scan (compare) a string byte or word with
MOVS accumulator
SCAS compares a byte in AL or a word in AX
CMPS with a byte or word pointed by DI in ES.
The string to be scanned must be in the
SCAS extra segment.
Both operands are not affected.
LODS Format: SCASB, SCASW
For SCASB:
STOS If Y=(ES) x 10h + (DI)
Modify flags (AL)-(Y)
If (AL) > (Y), then CF = 0; ZF = 0; SF = 0
REP If (AL)< (Y) , then CF = 1; ZF = 0; SF = 1
If (AL) = (Y) , then CF = 0; ZF = 1; SF = 0
STOS
REP
4. String Manipulation Instructions
Load string byte in to AL or string word in to AX.
MOVS Copies a byte from a string location pointed by SI
to AL, or a word from a string location pointed by
CMPS SI to AX.
Doesn’t affect any flag.
SCAS Format: LODSB, LODSW
If X=(DS) x 10h + (SI)
LODS LODSB ; (AL)(X)
; If DF = 0, then (SI) (SI) + 1
: If DF = 1, then (SI) (SI) – 1
STOS
LODSW ; (AX)(X)
REP ; If DF = 0, then (SI) (SI) +2
; If DF = 1, then (SI) (SI) – 2
4. String Manipulation Instructions
MOVS Store byte from AL or word from AX in to string.
Copies a byte from AL or a word from AX to a
memory location in the extra segment pointed to
CMPS
by DI.
Doesn’t affect any flag.
SCAS Format: STOSB, STOSW
If Y=(ES) x 10h + (DI)
LODS
STOSB ; (Y)(AL)
; If DF = 0, then (DI) (DI) + 1
STOS ; If DF = 1, then (DI) (DI) - 1
STOSW ; (Y)(AX)
DF = 0, then (DI) (DI) + 2
REP ; If
; If DF = 1, then (DI) (DI) - 2
4. String Manipulation Instructions
MOVS It is a Prefix that repeats String Instruction
until specified conditions exist.
REP is a prefix, which is written before one of
CMPS
the string instructions. It will cause the CX
register to be decremented and the string
SCAS instruction to be repeated until CX = 0.
REP
55
4. String Manipulation Instructions
MOVS REPZ / REPE: While CX 0 and ZF = 1, repeat
execution of string instruction and (CX) (CX) – 1
CMPS Example:
REPZ CMPSB ; Compare string bytes until end of string or
SCAS until string bytes not equal.
Mnemonics Explanation
HLT Halt the process.
WAIT Wait for TEST pin active
ESC opcode mem/ reg Used to pass instruction to a
coprocessor which shares the
address and data bus with the 8086
LOCK Lock bus during next instruction
NOP No operation
6) Program Execution Transfer Instructions
The program control/ execution transfer/
instructions transfer the flow of execution of
the program to a new address specified in the
instructions directly or indirectly.
When this type of instruction is executed,
the CS and IP registers get loaded with new
values of CS and IP corresponding to the
location to be transferred.
This type of instructions is classified in to
two types:
Unconditional program control instructions
Conditional program control instructions
6.1 Unconditional program control instructions
In the case of unconditional program control
instructions, the execution control is transferred to
the specified location independent of any status or
condition.
Instructions in this category include: CALL,
RET, JMP.
CALL: Unconditional Call
This instruction is used to call a Subroutine
(Procedure) from a main program. Address of
procedure may be specified directly or
indirectly.
There are two types of procedure depending
upon whether it is available in the same
segment or in another segment.
Near procedure: Procedure is stored in the same
code segment with the main program.
Far procedure: Procedure is stored outside of the
main program’s code segment.
CALL: Unconditional Call
For Near procedures:
CALL instruction pushes content of IP register in
to the Stack and loads IP register with offset
addresses of the procedure to be called.
For Far procedures:
CALL instruction pushes content of both IP and CS
registers in to the Stack and loads CS & IP
registers with segment and offset addresses of
the procedure to be called.
RET: Return from the Procedure
At the end of the procedure, the RET
instruction must be executed.
The RET instruction will return execution
from a procedure to the next instruction
after the CALL instruction which was used to
CALL the procedure.
If the procedure is a near procedure:
then the return will be done by replacing the
instruction pointer with a word from the top of
the stack.
RET: Return from the Procedure
If the procedure is a far procedure:
then the instruction pointer will be replaced by the
word at the top of the stack.
The stack pointer will then be incremented by 2.
The code segment register is then replaced with a
word from the new top of the stack.
JMP: - Unconditional Jump
This instruction will always cause the 8086 to
fetch its next Instruction from the location
specified in the instruction rather than from
the next location after the JMP instruction.
Example:
JMP NEXT ; Fetch next instruction from address at label NEXT. This is
direct jump
JMP BX ; Replace the contents of IP with the contents of BX. This is
indirect near Jump
JMP [BX] ; Replace IP with a word from a memory location pointed to
by BX in DS. This is an indirect near jump.
JMP DWORD PTR [ SI] ; Replace IP with a word pointed to by SI in DS.
Replace CS with a word pointed to by SI + 2 in
DS. This is an indirect far jump.
6.1) Conditional program control instructions
Instruction Description
LOOP jump to Specified Label if CX≠0 after Auto
decrement
LOOPE/LOOPZ Loop While CX ≠ 0 and ZF = 1
LOOPNE/LOOPNZ Loop While CX ≠ 0 and ZF = 0
Example
MOV CX, 0005
MOV BX, 0FF7H
Label: MOV AX, 1234h
OR BX, AX
AND DX, AX
Loop Label
End of slide
Example of unconditional jump
ORG 100h
MOV AX, 5 ; set AX to 5.
MOV BX, 2 ; set BX to 2.
JMP calc ; go to 'calc'.
back: JMP stop ; go to 'stop'.
calc:
ADD AX, BX ; add BX to AX.
JMP back ; go 'back'.
stop:
RET ; return to operating system.
END ; directive to stop the compiler.
Example of conditional jump
include emu8086.inc
ORG 100h
MOV AL, 25 ; set AL to 25.
MOV BL, 10 ; set BL to 10. later change to 25 and try
CMP AL, BL ; compare AL - BL.
JE equal ; jump if AL = BL (ZF = 1).
PUTC 'N' ; if it gets here, then AL <> BL,
JMP stop ; so print 'N', and jump to stop.
equal: ; if gets here,
PUTC 'Y' ; then AL = BL, so print 'Y'.
stop:
RET ; gets here no matter what.
END