Check Array Index Against Bounds

Encoding

EncodingOperand 1Operand 2
rmModRM.reg[r]ModRM.r/m[r]

Description

The BOUND instruction determines if the first operand (an array index) is within the bounds of an array specified in the second operand. The array index is a signed integer located in a register, and the bounds are a memory location containing either two consecutive words (in 16 bit mode) or two consecutive double words (in 32 bit mode). From these two memory values, the first is the lower bound of the array and the second is the upper bound of the array.

If the index is within the bounds specified, this instruction executes with no side effects. If, however, the index is not within the bounds, a #BR exception is raised.

This instruction is not encodable in Long Mode, and, if encountered, will be interpreted as EVEX.

Operation

public void BOUND(U16 index, IntPtr bounds)
{
    if (index < Mem16[bounds] || index > Mem16[bounds + 2])
        #BR;
}

public void BOUND(U32 index, IntPtr bounds)
{
    if (index < Mem32[bounds] || index > Mem32[bounds + 4])
        #BR;
}

Flags Affected

None.

Intrinsics

None. Auto-generated by compiler.

Exceptions

Real-Address Mode

#BR
  • If the bounds test fails.
#UD
  • If the
  • LOCK
  • prefix is used.
#SS(0)
  • If a memory operand using the
  • SS
  • segment has an effective address that is outside the
  • SS
  • segment's limit.
#GP(0)
  • If a memory operand (using a segment other than
  • SS
  • ) has an effective address that is outside the segment's limit.

Virtual-8086 Mode

#BR
  • If the bounds test fails.
#UD
  • If the
  • LOCK
  • prefix is used.
#SS(0)
  • If a memory operand using the
  • SS
  • segment has an effective address that is outside the
  • SS
  • segment's limit.
#GP(0)
  • If a memory operand (using a segment other than
  • SS
  • ) has an effective address that is outside the segment's limit.
#PF(fc)
  • If a page fault occurs.
#AC(0)
  • If alignment checking is enabled while the current privilege level is 3 and an unaligned memory access is made.

Protected Mode

#BR
  • If the bounds test fails.
#UD
  • If the
  • LOCK
  • prefix is used.
#SS(0)
  • If a memory operand using the
  • SS
  • segment has an effective address that is outside the
  • SS
  • segment's limit.
#GP(0)
  • If the destination is located in a non-writable segment.
  • If a memory operand uses a segment containing a NULL selector.
  • If a memory operand (using a segment other than SS) has an effective address that is outside the segment's limit.
#PF(fc)
  • If a page fault occurs.
#AC(0)
  • If alignment checking is enabled while the current privilege level is 3 and an unaligned memory access is made.

Compatibility Mode

#BR
  • If the bounds test fails.
#UD
  • If the
  • LOCK
  • prefix is used.
#SS(0)
  • If a memory operand using the
  • SS
  • segment has an effective address that is outside the
  • SS
  • segment's limit.
#GP(0)
  • If the destination is located in a non-writable segment.
  • If a memory operand uses a segment containing a NULL selector.
  • If a memory operand (using a segment other than SS) has an effective address that is outside the segment's limit.
#PF(fc)
  • If a page fault occurs.
#AC(0)
  • If alignment checking is enabled while the current privilege level is 3 and an unaligned memory access is made.