【问题标题】:Assembly: boot loader for custom OS keyboard support组装:自定义操作系统键盘支持的引导加载程序
【发布时间】:2019-07-24 10:24:41
【问题描述】:

我有一个工作简单的自定义操作系统(现在没有做太多:D)。现在我正在使用一个不支持键盘的程序集文件(boot.s)。

汇编文件(boot.s):

# set magic number to 0x1BADB002 to identified by bootloader 
.set MAGIC,    0x1BADB002
# set flags to 0
.set FLAGS,    0
# set the checksum
.set CHECKSUM, -(MAGIC + FLAGS)
# set multiboot enabled
.section .multiboot
# define type to long for each data defined as above
.long MAGIC
.long FLAGS
.long CHECKSUM
# set the stack bottom 
stackBottom:
# define the maximum size of stack to 512 bytes
.skip 512
# set the stack top which grows from higher to lower
stackTop:
.section .text
.global _start
.type _start, @function

_start:
  # assign current stack pointer location to stackTop
    mov $stackTop, %esp
  # call the kernel main source
    call KERNEL_MAIN
    cli
# put system in infinite loop
hltLoop:
    hlt
    jmp hltLoop
.size _start, . - _start

我认为这是缺少的部分,但它是 intel 语法,我不能使用它。

load_idt:
mov edx, [esp + 4]
lidt [edx]
sti
ret

read_port:
mov edx, [esp + 4]
in al, dx   
ret

write_port:
mov edx, [esp + 4]    
mov al, [esp + 4 + 4]  
out dx, al  
ret

keyboard_handler:                 
call keyboard_handler
iretd

我正在使用以下命令编译 boot.s:

as --32 boot.s -o boot.o

谁能帮我把键盘部分(英特尔语法)翻译成 AT&T? :)

【问题讨论】:

    标签: assembly x86 att osdev multiboot


    【解决方案1】:

    有关如何将 NASM 英特尔语法转换为 GAS 的 AT&T 语法的信息可以在此Stackoverflow Answer 中找到,并且在此IBM article 中提供了许多有用的信息。您的代码尤其看起来像:

    load_idt:
        mov 4(%esp), %edx
        lidt (%edx)
        sti
        ret
    
    read_port:
        mov 4(%esp), %edx
        in %dx, %al
        ret
    
    write_port:
        mov 4(%esp), %edx
        mov 8(%esp), %al
        out %al, %dx
        ret
    
    keyboard_handler:                 
        call keyboard_handler
        iret
    

    总的来说,最大的区别是:

    • 对于 AT&T 语法,源位于左侧,目标位于右侧,而英特尔则相反。
    • 在 AT&T 语法中,寄存器名称前面带有 %
    • 在 AT&T 语法中,立即数前面带有 $
    • 内存操作数可能是最大的区别。 NASM 使用 [segment:disp+base+index*scale] 而不是 GAS 的 segment:disp(base, index, scale) 语法。

    其他观察

    我建议将堆栈从多重引导部分移到.bss 部分。 BSS 部分通常不会占用输出可执行文件中的空间(假设使用的是健全的或默认的链接器脚本)。在.text 部分之后可以这样定义堆栈:

    .section .bss
        .lcomm stackBottom 512
    stackTop:
    

    【讨论】:

    • 第一个 write_port 指令缺少 4 字节位移。
    • @SepRoland:谢谢。看来我在翻译过程中不小心拉出了位移。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 2018-03-20
    • 2021-02-06
    相关资源
    最近更新 更多