assemblygccx86inline-assemblygdt

Read value of GDTR


I find out that it's possibly to read GDTR by SGDT assembly command. Inserting this piece of assembly in my C code I get Error: operand type mismatch for 'sgdt'

unsigned long j;
asm("sgdt %0" : "=r"(j));

Solution

  • sgdt can only take a memory operand, not a register, so it has to be "=m". The operand-size is 2+8 bytes (for ; limit then address in that order) so you need a struct; using a long will result in storing outside the object.

    Read the manual! https://www.felixcloutier.com/x86/sgdt


    Other caveats:


    On my Linux 5.18 system with a Skylake CPU (no UMIP support), I put sgdt [rsp] (NASM syntax) into a static executable so I could single-step it with GDB (starti / stepi). After that instruction: