编写一个简单的引导程序,读取用户名

Jyt*_*tug 2 x86 assembly osdev minix bootloader

我正在尝试学习操作系统的工作方式。这是我要解决的一个容易的任务:编写一个简单的引导程序,提示用户输入他的名字并打印欢迎消息,例如“ hello,>> name <<”-之后,它什么也不做。

如果minix 3qemu此相关,我正在与一起运行。我只是将asm文件dd及其前512个字节编译为/dev/c0d0(的虚拟硬盘minix)。

我可以打印消息并打印用户输入的内容。但是,此后我没有设法打印用户名。

这是我的汇编代码:

[bits 16]
[org 0x7c00]

mov si, HelloString
call print_string
mov di, name
call read_name
mov si, name
call print_string

read_name:
    read_char:
        mov ah, 0h  ; read character from keyboard
        mov [di], ah    ; save it in the buffer
        inc di      ; next char
        int 0x16    ; store it in AL
        cmp ah, 0x0d    ; check for enter
        je stop_reading 
        mov ah, 0eh     ; display character in AL
        int 0x10    ; echo it
        jmp read_char   ; an so on
    stop_reading:
        mov si, EoL
        call print_string
        ret

print_char:
    mov ah, 0x0e    ; one char
    mov bh, 0x00    ; page number
    mov bl, 0x07    ; font color
    int 0x10
    ret

print_string:
    next_char:
        mov al, [si]
        inc si
        or al, al
        jz exit_function
        call print_char
        jmp next_char
    exit_function:
        ret

;data
HelloString db 'Enter your name', 0xd, 0xa, 0
name times 20 db 0
EoL db 0xd, 0xa, 0

times 510 - ($ - $$) db 0;
dw 0xaa55
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Mic*_*tch 5

您的代码有很多问题。罗斯和我在评论中指出了一些。您应该阅读我的通用Bootloader技巧。尽管与您的实际问题无关,但应将DS(如果最终需要,则将ES)设置为0,因为您使用的起点是0x7c00(org 0x7c00)。您还应该在您知道代码不会破坏的地方设置堆栈。我将以下代码添加到顶部:

    mov si, HelloString
    call print_string
Run Code Online (Sandbox Code Playgroud)

更改为:

    xor ax, ax         ; AX=0
    mov ds, ax
    mov es, ax
    mov ss, ax         ; SS=ES=DS=0
    mov sp, 0x7c00     ; Place stack before the bootloader. Grows down from 0x0000:0x7c00

    mov si, HelloString
    call print_string
Run Code Online (Sandbox Code Playgroud)

代码运行完毕后,应将CPU置于无限循环中,以免因在主代码下执行功能而导致CPU无法继续运行。因此,在标签之前read_name:放置无限循环。这样的事情很典型:

    cli                ; Turn off interrupts        
endloop:
    hlt                ; Halt processor until next interrupt encountered
    jmp endloop        ; Jump back just in case we get an MNI (non-maskable interrupt)
Run Code Online (Sandbox Code Playgroud)

您的read_char函数中存在一些错误。BIOS中断信息的最佳位置之一是Ralph Brown的中断列表Int 0x16 / AH = 0记录为:

AH = 00h

Return:
AH = BIOS scan code
AL = ASCII character
Run Code Online (Sandbox Code Playgroud)

您应该在AL中使用ASCII字符存储到字符串缓冲区中。您还应该比较AL与0x0d,而不是AH(这是键盘扫描代码,而不是ASCII字符)。在使用读取字符之前,还将数据存储到字符串缓冲区中int 0x16。之后,您需要将它们放入缓冲区。到达后,stop_reading:您将需要在缓冲区的末尾放置一个NUL(0x00)字符。

您的代码read_name如下所示:

read_name:
read_char:
    mov ah, 0h  ; read character from keyboard
    int 0x16    ; store it in AL
    cmp al, 0x0d    ; check for enter
    je stop_reading
    mov [di], al    ; save it in the buffer
    inc di      ; next char
    mov ah, 0eh     ; display character in AL
    int 0x10    ; echo it
    jmp read_char   ; an so on
stop_reading:
    mov byte [di], 0x00    ; NUL terminate buffer
    mov si, EoL
    call print_string
    ret
Run Code Online (Sandbox Code Playgroud)

修改后的引导程序看起来像:

[bits 16]
[org 0x7c00]

    xor ax, ax         ; AX=0
    mov ds, ax
    mov es, ax
    mov ss, ax
    mov sp, 0x7c00     ; Place stack before the bootloader. Grows down from 0x0000:0x7c00

    mov si, HelloString
    call print_string
    mov di, name
    call read_name
    mov si, name
    call print_string

    cli                ; Turn off interrupts        
endloop:
    hlt                ; Halt processor until next interrupt encountered
    jmp endloop        ; Jump back just in case we get an MNI (non-maskable interrupt)

read_name:
read_char:
    mov ah, 0h         ; read character from keyboard
    int 0x16           ; store it in AL
    cmp al, 0x0d       ; check for enter
    je stop_reading
    mov [di], al       ; save it in the buffer
    inc di             ; next char
    mov ah, 0eh        ; display character in AL
    int 0x10           ; echo it
    jmp read_char      ; an so on
stop_reading:
    mov byte [di], 0   ; NUL terminate buffer
    mov si, EoL
    call print_string
    ret

print_char:
    mov ah, 0x0e       ; one char
    mov bh, 0x00       ; page number
    mov bl, 0x07       ; font color
    int 0x10
    ret

print_string:
next_char:
    mov al, [si]
    inc si
    or al, al
    jz exit_function
    call print_char
    jmp next_char
exit_function:
    ret

;data
HelloString db 'Enter your name', 0xd, 0xa, 0
name times 20 db 0
EoL db 0xd, 0xa, 0

times 510 - ($ - $$) db 0;
dw 0xaa55
Run Code Online (Sandbox Code Playgroud)

我强烈建议使用BOCHS调试引导加载程序。它具有一个内置的调试器,该调试器可以了解实模式和实模式寻址,并且比QEMU更适合调试引导加载程序。

  • 从汇编语言的角度来看,进行OS开发的最佳位置是[OS Dev Wiki](http://wiki.osdev.org/Main_Page)。它不是汇编参考,但教程几乎完全是汇编语言。 (2认同)