Assembly:用于自定义OS键盘支持的引导加载程序

问题描述 投票:2回答:1

我有一个简单的自定义操作系统(现在做的不多: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

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

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 intel-syntax multiboot
1个回答
4
投票

有关如何将NASM Intel语法转换为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:
© www.soinside.com 2019 - 2024. All rights reserved.