我正在为内核编写 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!
但是,我的汇编语法有所不同,这是我的
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!*/
我的问题是如何将这条指令的语法翻译成我正在使用的格式?
他的:
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!*/
最佳答案
一些东西。远跳的 AT&T 语法是:
jmp $0x08,$flush2
在这种情况下,标签需要以
$
开头。 .立即数,如 0x08
还需要一个$
.这条线没有做你认为它做的事情:mov %ax, 0x10
关于 AT&T 语法的重要一点是,与 Intel 语法不同,操作数是相反的。源操作数在前,目标操作在后。其次,x86/x86-64 上 AT&T 语法中的立即数需要有
$
附加在它们前面的符号,或者它们实际上被视为内存操作数。您的指令实际上将 AX 的 16 位内容移动到了内存地址 0x00000010,这不是您想要的。你想要的是:mov $0x10, %ax
这会将立即值 0x10 移动到 AX。操作数被反转的问题也适用于您的所有行,例如:
mov %ds, %ax
应该:
mov %ax, %ds
我通常更喜欢调用你的函数
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
C 原型(prototype)将类似于:
void load_gdt(struct gdt_ptr *gdt_ptr, unsigned int data_sel, unsigned int code_sel);
如果您想将 GNU 汇编器与英特尔语法的变体一起使用,您可以尝试将此指令添加到所有汇编文件的顶部:
.intel_syntax noprefix
关于程序集执行具有不同语法的偏移量的长跳转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49438550/