Unit I
Unit I
Unit I
Instruction SET
• MOV[1000H],[3230H] XXXXXXX
• XCHG (Exchange)
• swaps the contents of two operands.
• This instruction takes the place of three MOV instructions.
• It does not require a temporary location to save the contents of one operand while load
the other is being loaded.
• XCHG is especially useful for implementing semaphores or similar data structures for
process synchronization.
• The XCHG instruction can swap two byte operands,
two word operands, or two doubleword operands.
•
• Stack Manipulation Instructions
• PUSH (Push)
• decrements the stack pointer (ESP), then transfers the source
operand to the top of stack indicated by ESP.
• PUSH is often used to place parameters on the stack before
calling a procedure;
• It is also the basic means of storing temporary variables on the
stack.
• The PUSH instruction operates on memory operands,
immediate operands, and register operands (including segment
registers).
• PUSHA (Push All Registers) saves the contents of the eight
general registers on the stack.
•
• This instruction simplifies procedure calls by reducing the
number of instructions required to retain the contents of the
general registers for use in a procedure
• The syntax is the same as for the movsx instructions except, of course, you use the
movzx mnemonic rather than movsx.
• MOVZX EAX,BL
• This copies the contents of BL register to EAX register. Zero is copied to the MSB of
the EAX register as well as the AH register.
Binary Arithmetic Instructions
• Addition
• Subtration
• Multiplication
• Division
• Comparision
• Change sign
• ADD (Add Integers) replaces the destination operand with the sum of the
source and destination operands. Sets CF if overflow.
• ADC (Add Integers with Carry) sums the operands, adds one if CF is set,
and replaces the destination operand with the result. If CF is cleared, ADC
performs the same operation as the ADD instruction. An ADD followed by
multiple ADC instructions can be used to add numbers longer than 32 bits.
• INC (Increment) adds one to the destination operand. INC does not affect
CF.
• ADD(Add byte or word or double word
• ADD distination,source
• Destination= destination+ source
• add reg, reg
• add reg, mem
• add mem, reg
• add reg, immediate data
• add mem, immediate data
• add eax/ax/al, immediate data
• Both operands i.e. source and destination cannot be memory locations.
• Flags: AF,CF,OF,PF,SF,ZF affected.
• ADC- Add with carry
ADC destination, source
Destination= destination+source+CF
INC(Increment destination by 1)
INC Destination
Destination=destination+1
e.g. INC CX
INC AL
INC EBX
Subtraction Instructions
SUB, SBB, DEC,
• They set the zero flag if the result is zero
• These instructions set the sign flag if the result is negative.
• These instructions set the overflow flag if signed
overflow/underflow occurs.
• They set the auxiliary carry flag as necessary for BCD/ASCII
arithmetic.
• They set the parity flag according to the number of one bits
appearing in the result value.
• The sub and sbb instructions set the carry flag if an unsigned
overflow occurs.
Note that the dec instruction does not affect the carry flag.
• SUB (Subtract Byte or word)
SUB destination, source
Destination=destination-source
DEC destination
destination= destination -1
• Multiplication Instructions
• dx:ax := ax * operand16
3. A three-operand form; two are source and one is the destination operand.
• If magnitude of the product does not require all bits of
the destination the unused bits are filled with copies of
the sign bit.
• Division Instructions
• DIV, IDIV
• NEG (Negate)
Perform 2’s complement negation of the destination
• CMP( Compare Two Operands)
CMP Destination, source
Destination- Source
Result is not stored in either of the dest or source only
flags are updated.
Both source and dest operands must be of same type
cmp ax, bx
The flags are set as follows:
Z: The zero flag is set if and only if ax = bx. This is the only time ax-bx
produces a zero result.
S: The sign flag is set to one if the result is negative. i.e. source is greater than
destination
O: The overflow flag is set after a cmp operation if the difference of ax and bx
produced an overflow or underflow. As mentioned above, the sign flag and the
over- flow flag are both used when performing signed comparisons.
AL= 86
BH=57
SUB AL,BH
DAS
2. Unpacket BCD adjustment Instructions
AAA (ASCII adjust after addition)
AAS (ASCII adjust after subtraction)
AAM (ASCII adjust for Multiply)
AAD (ASCII adjust after Divide)
• AAA (ASCII adjust after addition)
If lower nibble of AL is greater than 9 then
AL=AL+6 and AH=AH+1
AF=1 , CF=1
Numerical data coming into a computer from a terminal through
keyboard is usually in ASCII code.
The numbers 0 to 9 are represented by ASCII codes 30H to 39H.
After addition AAA instruction is used to make sure that the
result is the correct unpacked BCD.
AAA works only on AL register and after an ADD instruction
• AAS: (ASCII adjust after subtraction)
If lower nibble of AL is greater than 9 then
AL=AL - 6
AH=AH-1
AF=1
CF=1
• AAM: This instruction divides ax by 10 and leaves the quotient in ah and
the remainder in al
This adjustment must be done after multiplication
BT (Bit Test)
- If these instructions locate a one, they clear the zero flag and
store the bit index (0..31) into the destination operand.
• Difference between Test and AND is that TEST does not alter the
destination operand
• Difference between Test an Bit Test is
• BT tests a single bit and
• TEST tests multiple bits in one operation.
Control Transfer Instructions
• The 80386 provides both conditional and unconditional control
transfer instructions to direct the flow of execution.
1. The program can JMP to a location specified by a general register (any of EAX,
EDX, ECX, EBX, EBP, ESI, or EDI). The processor moves this 32-bit value into
EIP and resumes execution.
2. The processor can obtain the destination address from a memory operand
specified in the instruction.
Call Instruction
• CALL (Call Procedure) activates an out-of-line procedure, saving on
the stack the address of the instruction following the CALL for later
use by a RET (Return) instruction.
• RET restores the value of EIP that was saved on the stack by the
previous CALL instruction.
• If the value of ECX is initially zero, then the LOOP executes 232
times.
• LOOPE (Loop While Equal) and LOOPZ (Loop While
Zero) are synonyms for the same instruction.
• If ECX is non-zero and ZF=1, the program branches to the target label
specified in the instruction.
• If ECX is non-zero and ZF=0, the program branches to the target label
specified in the instruction.
• JCXZ- (Jump if ECX Zero) JCXZ is useful in combination with the LOOP
instruction and with the string scan and compare instructions, all of which
decrement ECX.
• Sometimes, it is desirable to design a loop that executes zero times if the count
variable in ECX is initialized to zero.
• Because the LOOP instructions (and repeat prefixes) decrement ECX before they
test it, a loop will execute 232 times if the program enters the loop with a zero
value in ECX.
• A programmer may conveniently overcome this problem with JCXZ, which
enables the program to branch around the code within the loop if ECX is zero
when JCXZ executes.
Software-Generated Interrupts
• The INT n, INTO, and BOUND instructions allow the programmer to
specify a transfer to an interrupt service routine from within a
program.
• INT n (Software Interrupt 0-255) activates the interrupt service
routine that corresponds to the number coded within the instruction.
• The INT instruction may specify any interrupt type .
• Programmers may use this flexibility to implement multiple types of
internal interrupts or to test the operation of interrupt service routines.
(Interrupts 0-31 are reserved by Intel.)
• The interrupt service routine terminates with an IRET instruction that
returns control to the instruction that follows INT.
INTO (Interrupt on Overflow) invokes interrupt 4 if OF is set.
• Interrupt 4 is reserved for this purpose.
• OF is set by several arithmetic, logical, and string instructions.
BOUND (Detect Value Out of Range) verifies that the signed value
contained in the specified register lies within specified limits.
An interrupt (INT 5) occurs if the value contained in the register is less
than the lower bound or greater than the upper bound.
BOUND index,Range
Test the value of index register against predefined range of index if it is
out of range then INT 5 is generated.
String and Character Translation Instructions
• The instructions in this category operate on strings rather than on
logical or numeric values.
• The power of 80386 string operations derives from the following
features of the architecture:
1. A set of primitive string operations
MOVS ── Move String
CMPS ── Compare string
SCAS ── Scan string
LODS ── Load string
STOS ── Store string
2. Indirect, indexed addressing, with automatic incrementing or
decrementing of the indexes.
• Indexes:
ESI ── Source index register
EDI ── Destination index register
• Control flag:
DF ── Direction flag
• Control flag instructions:
CLD ── Clear direction flag instruction
STD ── Set direction flag instruction
3. Repeat prefixes
• REP ── Repeat while ECX not zero
• REPE/REPZ ── Repeat while equal or zero
• REPNE/REPNZ ── Repeat while not equal or not zero
MOVS (Move String) moves the string element pointed to by ESI to
the location pointed to by EDI.
• MOVSB operates on byte elements, MOVSW operates on word
elements, and MOVSD operates on double words.
• The destination segment register cannot be overridden by a segment
override prefix, but the source segment register can be overridden.
MOVS dest string, Source string.
It is assumed that source string resides in the current data segment (DS)
( one can change by seg override prefx)
Destination string must be in current Extra segment(ES).
ESI= index for source string
EDI= index for destination string.
• In case of byte
ES : [DI] = DS : [SI]
If DF=0 then SI=SI+1, DI=DI+1
If DF=1 then SI=SI-1 , DI=DI-1
• In case of Word
ES : [DI] = DS : [SI]
If DF=0 then SI=SI+2, DI=DI+2
If DF=1 then SI=SI-2 , DI=DI-2
• In Case of Double Word
• In case of byte
ES : [EDI] = DS : [ESI]
If DF=0 then ESI=ESI+4, EDI=EDI+4
If DF=1 then ESI=ESI-4 , EDI=EDI-4
CMPS: CMPS (Compare Strings) subtracts the destination string
element (at ES:EDI) from the source string element (at DS:ESI) and
updates the flags AF, SF, PF, CF and OF.
CMPS dest+string, source_string
If the string elements are equal, ZF=1; otherwise, ZF=0. If DF=0,the
processor increments the memory pointers (ESI and EDI) for the
twostrings.
CMPSB compares bytes, CMPSW compares words, and CMPSD
compares doublewords.
The segment register used for the source address can be changed with a
segment override prefix while the destination segment register cannot be
overridden.
• In case of byte
DS : [SI] - ES : [DI]
If DF=0 then SI=SI+1, DI=DI+1
If DF=1 then SI=SI-1 , DI=DI-1
• In case of Word
DS : [SI] - ES : [DI]
If DF=0 then SI=SI+2, DI=DI+2
If DF=1 then SI=SI-2 , DI=DI-2
• In Case of Double Word
• In case of byte
DS : [ESI] - ES : [EDI]
If DF=0 then ESI=ESI+4, EDI=EDI+4
If DF=1 then ESI=ESI-4 , EDI=EDI-4
SCAS (Scan String) subtracts the destination string element at ES:EDI
from EAX, AX, or AL and updates the flags AF, SF, ZF, PF, CF and OF.
If the values are equal, ZF=1; otherwise, ZF=0. If DF=0, the processor
increments the memory pointer (EDI) for the string.
• STOS destination_string
• In case of byte
DS : [SI] =AL
If DF=0 then SI=SI+1, DI=DI+1
If DF=1 then SI=SI-1 , DI=DI-1
• In case of Word
DS : [SI] =AX
If DF=0 then SI=SI+2, DI=DI+2
If DF=1 then SI=SI-2 , DI=DI-2
• In Case of Double Word
DS : [ESI]=EAX
If DF=0 then ESI=ESI+4, EDI=EDI+4
If DF=1 then ESI=ESI-4 , EDI=EDI-4
Repeat (Prefix)
• REP/REPE/REPNE/REPNZ
• The ENTER instruction can be used in two ways: nested and non-nested.
• LEAVE copies EBP to ESP to release all stack space allocated to the
procedure by the most recent ENTER instruction.
• Then LEAVE pops the old value of EBP from the stack.
• The flag transfer instructions allow a program to alter the other flag
bits with the bit manipulation instructions after transferring these flags
to the stack or the AH register.
• The instructions LAHF and SAHF deal with five of the status flags,
which are used primarily by the arithmetic and logical instructions.
• LAHF LOADS FIVE FLAGS FROM THE FLAG REGISTER INTO
REGISTER AH.
• SAHF STORES THESE SAME FIVE FLAGS FROM AH INTO
THE FLAG REGISTER.
• THE BIT POSITION OF EACH FLAG IS THE SAME IN AH AS IT
IS IN THE FLAG REGISTER.
• THE REMAINING BITS (MARKED UU) ARE RESERVED; DO
NOT DEFINE.
• The PUSHF and POPF instructions are not only useful for storing the flags
in memory where they can be examined and modified but are also useful for
preserving the state of the flags register while executing a procedure.
• PUSHF (Push Flags) decrements ESP by two and then transfers the low-
order word of the flags register to the word at the top of stack pointed to by
ESP . The variant PUSHFD decrements ESP by four, then transfers both
words of the extended flags register to the top of the stack pointed to by
ESP (the VM and RF flags are not moved, however).
• POPF (Pop Flags) transfers specific bits from the word at the top of stack
into the low-order byte of the flag register, then increments ESP by two. The
variant POPFD transfers specific bits from thedoubleword at the top of the
stack into the extended flags register (the RF and VM flags are not changed,
however), then increments ESP by four.
Coprocessor Interface Instructions
• A numerics coprocessor (e.g., the 80387 or 80287) provides an extension to
the instruction set of the base architecture. The coprocessor extends the
instruction set of the base architecture to support high-precision integer and
floating-point calculations. This extended instruction set includes
arithmetic, comparison, transcendental, and data transfer instructions.
• The coprocessor also contains a set of useful constants to enhance the speed
of numeric calculations.
• A program contains instructions for the coprocessor in line with the
instructions for the CPU.
• The system executes these instructions in the same order as they appear in
the instruction stream.
• The coprocessor operates concurrently with the CPU to provide maximum
throughput for numeric calculations.
• ESC (Escape) is a 5-bit sequence that begins the opcodes that
identify floating point numeric instructions.
• The ESC pattern tells the 80386 to send the opcode and addresses of
operands to the numerics coprocessor.
• The numerics coprocessor uses the escape instructions to perform
high-performance, high-precision floating point arithmetic that
conforms to the IEEE floating point standard 754.
• WAIT (Wait) is an 80386 instruction that suspends program execution
until the 80386 CPU detects that the BUSY pin is inactive.
• This instruction causes the processor to wait till an interrupt is
recognized.
• This condition indicates that the coprocessor has completed its
processing task and that the CPU may obtain the results.
Segment Register Instructions
1. Segment-register transfer instructions.
• MOV SegReg, ... MOV ..., SegReg
• PUSH SegReg POP SegReg
• LGS (Load Pointer Using GS) operates identically to LDS except that
GS receives the segment selector rather than DS.
• LSS (Load Pointer Using SS) operates identically to LDS except that SS
receives the segment selector rather than DS.
Miscellaneous Instructions
• Address Calculation Instruction
LEA (Load Effective Address) LEA register,source
transfers the offset of the source operand (rather than its value) to the destination
operand.
• The source operand must be a memory operand, and the destination operand must be
a general register.
• This instruction is especially useful for initializing registers before the execution of
the string primitives (ESI, EDI) or the XLAT instruction (EBX).
• The LEA can perform any indexing or scaling that may be needed.
• Example: LEA EBX, EBCDIC_TABLE
Causes the processor to place the address of the starting location of the table labeled
EBCDIC_TABLE into EBX.
• No-Operation Instruction
• NOP (No Operation) occupies a byte of storage but affects nothing but
the instruction pointer, EIP.
• This instruction uses three clock cycles and increments the instruction
pointer (EIP) to point to the next instruction.
• It can be used to increase the delay of a delay loop.
• Translate Instruction
• XLAT (Translate) replaced a byte in the AL register with a byte from a
user-coded translation table.
• When XLAT is executed, AL should have the unsigned index to the table
addressed by EBX.
• XLAT changes the contents of AL from table index to table entry. EBX is
unchanged.
• The XLAT instruction is useful for translating from one coding system to
another such as from ASCII to EBCDIC.
• The translate table may be up to 256 bytes long.
• The value placed in the AL register serves as an index to the location of
the corresponding translation value.
XLAT m8 Set AL to memory byte DS:[(E)BX +
unsigned AL]
• XLAT changes the AL register from the table index to the table entry.
• AL should be the unsigned index into a table addressed by DS:BX (for
an address-size attribute of 16 bits) or DS:EBX (for an address-size
attribute of 32 bits).
23 March 2023 Indira College of Engineering Management, Pune 131