__segmentlimit

Microsoft 专用

生成 lsl(负载段限制)指令。

语法

unsigned long __segmentlimit(
   unsigned long a
);

参数

a
[in] 一个指定段选择器的常量。

返回值

如果段选择器有效且在当前权限级别可见,则为由 a 指定的段选择器的段限制

要求

Intrinsic 体系结构
__segmentlimit x86、x64

头文件<intrin.h>

备注

如果无法检索到段限制,此指令就会失败。 在失败时,此指令会清除 ZF 标志,并且返回值未定义。

此例程仅可用作内部函数。

示例

#include <stdio.h>

#ifdef _M_IX86
typedef unsigned int READETYPE;
#else
typedef unsigned __int64 READETYPE;
#endif

#define EFLAGS_ZF      0x00000040
#define KGDT_R3_DATA    0x0020
#define RPL_MASK        0x3

extern "C"
{
unsigned long __segmentlimit (unsigned long);
READETYPE __readeflags();
}

#pragma intrinsic(__readeflags)
#pragma intrinsic(__segmentlimit)

int main(void)
{
   const unsigned long initsl = 0xbaadbabe;
   READETYPE eflags = 0;
   unsigned long sl = initsl;

   printf("Before: segment limit =0x%x eflags =0x%x\n", sl, eflags);
   sl = __segmentlimit(KGDT_R3_DATA + RPL_MASK);

   eflags = __readeflags();

   printf("After: segment limit =0x%x eflags =0x%x eflags.zf = %s\n", sl, eflags, (eflags & EFLAGS_ZF) ? "set" : "clear");

   // If ZF is set, the call to lsl succeeded; if ZF is clear, the call failed.
   printf("%s\n", eflags & EFLAGS_ZF ? "Success!": "Fail!");

   // You can verify the value of sl to make sure that the instruction wrote to it
   printf("sl was %s\n", (sl == initsl) ? "unchanged" : "changed");

   return 0;
}
Before: segment limit =0xbaadbabe eflags =0x0
After: segment limit =0xffffffff eflags =0x256 eflags.zf = set
Success!
sl was changed

结束 Microsoft 专用

另请参阅

编译器内部函数