Assembly: boot loader for custom OS keyboard support

大憨熊 提交于 2019-12-11 03:22:43

问题


I have a working simple custom OS (doesn't do much for now :D). Right now i'm using an assembly file (boot.s) that has no keyboard support.

The assembly file (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

I think this is the missing part but it is in intel syntax and i cannot use it.

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

I'm compiling the boot.s with the following command:

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

Can anyone help me translating the keyboard part (Intel syntax) to AT&T? :)


回答1:


Information on how you can go about translating NASM Intel syntax to GAS's AT&T syntax can be found in this Stackoverflow Answer, and a lot of useful information is provided in this IBM article. Your code in particular would look like:

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

In general the biggest differences are:

  • With AT&T syntax the source is on the left and destination is on the right and Intel is the reverse.
  • With AT&T syntax register names are prepended with a %
  • With AT&T syntax immediate values are prepended with a $
  • Memory operands are probably the biggest difference. NASM uses [segment:disp+base+index*scale] instead of GAS's syntax of segment:disp(base, index, scale).

Other Observations

I recommend moving the stack out of the multiboot section into the .bss section. The BSS section doesn't generally take up space in the output executable (assuming one is using a sane or default linker script). The stack could be defined this way after the .text section:

.section .bss
    .lcomm stackBottom 512
stackTop:


来源:https://stackoverflow.com/questions/54960681/assembly-boot-loader-for-custom-os-keyboard-support

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!