我正在为内核编写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!*/
答案 0 :(得分:3)
一些事情。远程跳转的AT& T语法是:
catch
此案例中的标签需要以false
开头。像jmp $0x08,$flush2
这样的即时值也需要$
。这条线不符合你的想法:
0x08
AT& T语法的重要之处在于,与英特尔语法不同,操作数是相反的。源操作数是第一个,目标操作是在之后。其次,x86 / x86-64上的AT& T语法中的立即值需要在它们之前加上$
符号,否则它们实际上被视为内存操作数。您的指令实际上将 AX 的16位内容移动到了内存地址0x00000010,这不是您想要的。你想要的是:
mov %ax, 0x10
这会将立即值0x10移动到 AX 。操作数被反转的问题也适用于你的所有行:
$
应该是:
mov $0x10, %ax
我通常更喜欢调用你的函数mov %ds, %ax
。我通常喜欢传递段值(CS和DS)以及GDTR的地址,代码如下:
mov %ax, %ds
C 原型将类似于:
load_gdt
如果您想将GNU汇编程序与英特尔语法的变体一起使用,您可以尝试将此指令添加到所有程序集文件的顶部:
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
答案 1 :(得分:0)
除了迈克尔的回答(肯定比我的要好),这是我的翻译:
.global gdt_flush
gdt_flush:
movl 4(%esp),%eax
lgdt (%eax)
movw $0x10, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %fs
movw %ax, %gs
movw %ax, %ss
jmp $0x08,$flush
flush:
ret