程序集执行具有不同语法的偏移量的长跳转

6 x86 assembly gnu-assembler osdev att

我正在为内核编写 GDT 并且一切顺利,我正在关注本教程。

http://www.osdever.net/bkerndev/Docs/gdt.htm

当将 C 代码链接到汇编代码时,他使用这段代码。

; This will set up our new segment registers. We need to do
; something special in order to set CS. We do what is called a
; far jump. A jump that includes a segment as well as an offset.
; This is declared in C as 'extern void gdt_flush();'
global _gdt_flush     ; Allows the C code to link to this
extern _gp            ; Says that '_gp' is in another file
_gdt_flush:
lgdt [_gp]        ; Load the GDT with our '_gp' which is a special pointer
    mov ax, 0x10      ; 0x10 is the offset in the GDT to our data segment
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax
    jmp 0x08:flush2   ; 0x08 is the offset to our code segment: Far jump!
flush2:
ret               ; Returns back to the C code!
Run Code Online (Sandbox Code Playgroud)

但是,我的程序集语法在这里有所不同,这是我目前作为boot.s文件的一部分所拥有的。

.global gdt_flush     /*Allows the C code to link to this*/
.extern gp            /*Says that '_gp' is in another file*/
_gdt_flush:
    lgdt gp        /*; Load the GDT with our '_gp' which is a special pointer*/
    mov %ax, 0x10     /* ; 0x10 is the offset in the GDT to our data segment*/
    mov %ds, %ax
    mov %es, %ax
    mov %fs, %ax
    mov %gs, %ax
    mov %ss, %ax
    jmp flush2   /*; 0x08 is the offset to our code segment: Far jump!*/
flush2:
ret               /*; Returns back to the C code!*/
Run Code Online (Sandbox Code Playgroud)

我的问题是如何将此指令的语法翻译成我正在使用的格式?

他的: jmp 0x08:flush2 ; 0x08 is the offset to our code segment: Far jump!

矿: (long l?)jmp ????flush2 /*; 0x08 is the offset to our code segment: Far jump!*/

Mic*_*tch 11

一些东西。远跳转的 AT&T 语法是:

jmp $0x08,$flush2 
Run Code Online (Sandbox Code Playgroud)

在这种情况下,标签需要以$. 直接值之类的0x08也需要一个$. 这一行不会做你认为它做的事情:

mov %ax, 0x10
Run Code Online (Sandbox Code Playgroud)

AT&T 语法的重要一点是,与 Intel 语法不同,操作数是颠倒的。源操作数在前,目的操作在后。其次,x86/x86-64 上 AT&T 语法中的立即数需要$在它们前面加上符号,否则它们实际上被视为内存操作数。您的指令实际上将AX的 16 位内容移动到内存地址 0x00000010 这不是您想要的。你想要的是:

mov $0x10, %ax
Run Code Online (Sandbox Code Playgroud)

这将立即数 0x10 移动到AX。操作数被反转的问题也适用于您的所有行,例如:

mov %ds, %ax
Run Code Online (Sandbox Code Playgroud)

应该:

mov %ax, %ds
Run Code Online (Sandbox Code Playgroud)

我通常更喜欢调用你的函数load_gdt。我通常喜欢使用以下代码传递段值(CS 和 DS)和 GDTR 的地址:

load_gdt:
    mov 4(%esp), %edx    # EDX is 1st argument - GDT record pointer
    mov 8(%esp), %eax    # EAX is 2nd argument - Data Selector
    lgdt (%edx)          # Load GDT with GDT record pointer passed as 1st argument
    mov %eax, %ds        # Reload all the data descriptors with Data selector (2nd arg)
    mov %eax, %es
    mov %eax, %gs
    mov %eax, %fs
    mov %eax, %ss

    pushl 12(%esp)      # Create FAR pointer on stack using Code selector (3rd argument)
    push $.setcs         # Offset of FAR JMP will be setcs label below
    ljmp *(%esp)        # Do the FAR JMP to next instruction to set CS with Code selector,
                        #    and set the EIP (instruction pointer) to offset of setcs
.setcs:
    add $8, %esp        # Restore stack (remove 2 DWORD values we put on stack to
                        #     create FAR Pointer)
    ret
Run Code Online (Sandbox Code Playgroud)

Ç原型会是这样的:

void load_gdt(struct gdt_ptr *gdt_ptr, unsigned int data_sel, unsigned int code_sel);
Run Code Online (Sandbox Code Playgroud)

如果您想将 GNU 汇编器与英特尔语法的变体一起使用,您可以尝试将此指令添加到所有汇编文件的顶部:

.intel_syntax noprefix
Run Code Online (Sandbox Code Playgroud)