Inline Assembly Jump Error

后端 未结 3 425
渐次进展
渐次进展 2021-01-17 06:38

Why does this fail, once Masm reaches jmp?

struct gdt_entry
{
    unsigned short limit_low;
    unsigned short base_low;
    unsigned char base_middle;
    u         


        
3条回答
  •  离开以前
    2021-01-17 07:31

    New answer:

    I've already encounter this problem some time ago and the only way I found to update the GDT with MASM inline assembly is to use a far return instruction, instead of the far jump instruction.

    struct gdt_entry gdt[3];
    struct gdt_ptr gp;
    void gdt_flush(){
        __asm{
              lgdt [gp]
    
              mov ax, 0x10
              mov ds, ax
              mov es, ax
              mov fs, ax
              mov gs, ax
              mov ss, ax
    
              ; push the address on the stack
              push 0x08
              mov eax, offset flush2
              push eax
    
              ; ret use the previous pushed address
              _emit 0xCB ; far return
    
          flush2:
              ;ret
       }
    }
    

    As far as I remember, there are two problems:

    • the 32 bits MASM inline assembly cannot compile far instructions, so you have to emit the opcode.
    • the jmp instruction does not do the right stuff and you should use instead the ret instruction to jump to the next line of code.

    Also, don't call the ret instruction from the inline assembly, otherwise you'll skip the epilog code that the compiler puts at the end of the function to clean the stack.


    My first answer below:

    Maybe your GDT descriptor (gp) is badly initialized.

    When you the jump instruction is executed, the processor try to switch to protected mode and the GDT is then required. If the GDT is not set correctly, it crashed.

    The first 16 bits of gp are the size of the gdt (here 3*8 = 24 bytes) and the following 32 bytes are the address of the gdt (here &gdt[0]).

    Also, make sure the ds register is null before calling lgdt: this register is used by the instruction.

提交回复
热议问题