STI

Set Interrupt Flag

Opcodes

Hex Mnemonic Encoding Long Mode Legacy Mode Description
FB STI A Valid Valid Set interrupt flag; external, maskable interrupts enabled at the end of the next instruction.

Instruction Operand Encoding

Op/En Operand 0 Operand 1 Operand 2 Operand 3
A NA NA NA NA

Description

If protected-mode virtual interrupts are not enabled, STI sets the interrupt flag (IF) in the EFLAGS register. After the IF flag is set, the processor begins responding to external, maskable interrupts after the next instruction is executed. The delayed effect of this instruction is provided to allow interrupts to be enabled just before returning from a procedure (or subroutine). For instance, if an STI instruction is followed by an RET instruction, the RET instruction is allowed to execute before external interrupts are recognized.

The STI instruction delays recognition of interrupts only if it is executed with EFLAGS.IF = 0. In a sequence of STI instructions, only the first instruction in the sequence is guaranteed to delay interrupts. In the following instruction sequence, interrupts may be recognized before RET executes:

If the STI instruction is followed by a CLI instruction (which clears the IF flag), the effect of the STI instruction is negated.

The IF flag and the STI and CLI instructions do not prohibit the generation of exceptions and NMI interrupts. NMI interrupts (and SMIs) may be blocked for one macroinstruction following an STI.

When protected-mode virtual interrupts are enabled, CPL is 3, and IOPL is less than 3; STI sets the VIF flag in the EFLAGS register, leaving IF unaffected.

The following table indicates the action of the STI instruction depending on the processor'smode of operation and the CPL/IOPL settings of the running program or procedure.

This instruction's operation is the same in non-64-bit modes and 64-bit mode.

Decision Table for STI Results
PE VM IOPL CPL PVI VIP VME STI Result
0 X X X X X X IF = 1
1 0 &ge CPL X X X X IF = 1
1 0 < CPL 3 1 0 X VIF = 1
1 0 < CPL < 3 X X X GP Fault
1 0 < CPL X 0 X X GP Fault
1 0 < CPL X X 1 X GP Fault
1 1 3 X X X X IF = 1
1 1 < 3 X X 0 1 VIF = 1
1 1 < 3 X X 1 X GP Fault
1 1 < 3 X X X 0 GP Fault

Pseudo Code

IF PE = 0 (* Executing in real-address mode *)
	IF = 1; (* Set Interrupt Flag *)
ELSE
	(* Executing in protected mode or virtual-8086 mode *)
	IF VM = 0 (* Executing in protected mode *)
		IF IOPL >= CPL
			IF = 1; (* Set Interrupt Flag *)
		ELSE
			IF (IOPL < CPL) and (CPL = 3) and (VIP = 0)
				VIF = 1; (* Set Virtual Interrupt Flag *)
			ELSE
				#GP(0);
			FI;
		FI;
	ELSE
		(* Executing in Virtual-8086 mode *)
		IF IOPL = 3
			IF = 1; (* Set Interrupt Flag *)
		ELSE
			IF ((IOPL < 3) and (VIP = 0) and (VME = 1))
				VIF = 1; (* Set Virtual Interrupt Flag *)
			ELSE
				#GP(0); (* Trap to virtual-8086 monitor *)
			FI;
		FI;
	FI;
FI;

Flags Affected

The IF flag is set to 1; or the VIF flag is set to 1.

Exceptions

64-Bit Mode Exceptions

Same exceptions as in protected mode.

Compatibility Mode Exceptions

Same exceptions as in protected mode.

Virtual-8086 Mode Exceptions

Same exceptions as in protected mode.

Real-Address Mode Exceptions

Exception Description
#UD If the LOCK prefix is used.

Protected Mode Exceptions

Exception Description
#UD If the LOCK prefix is used.
#GP(0) If the CPL is greater (has less privilege) than the IOPL of the current program or procedure.