您如何在nasm中启用a20?

时间:2019-01-30 18:17:36

标签: x86 nasm bootloader

我正在尝试在Bootloader中进入长模式,现在我在第一部分启用了a20行,但遇到了一个问题,那就是我编写的代码完全使屏幕变了黑色,甚至不能显示应该说的数字

我已经在互联网上尝试了许多不同的a20功能,但是对我来说还没有任何作用。

这些功能的代码为:

check_a20:
    pushf
    push ds
    push es
    push di
    push si
    cli
    xor ax, ax ; ax = 0
    mov es, ax
    not ax ; ax = 0xFFFF
    mov ds, ax
    mov di, 0x0500
    mov si, 0x0510
    mov al, byte [es:di]
    push ax
    mov al, byte [ds:si]
    push ax
    mov byte [es:di], 0x00
    mov byte [ds:si], 0xFF
    cmp byte [es:di], 0xFF
    pop ax
    mov byte [ds:si], al
    pop ax
    mov byte [es:di], al
    mov ax, 0
    je check_a20_exit
    mov ax, 1
    check_a20_exit:
    pop si
    pop di
    pop es
    pop ds
    popf
ret

seta20: ;Enable the a20 line if it worked then ax = 1 else 0
    pusha
    call check_a20 ;Check a20
    cmp ax, 1
    je .end ;If it worked then end function else:
    .keyboard: ;Test the 8042 keyboard controller
        call .empty_8042
        mov al, 0xd1 ;command write
        out 0x64, al
        call .empty_8042
        mov al, 0xdf ; A20 on
        out 0x60, al
        call .empty_8042 ;wait
    .empty_8042: ;For the 8042 function over this
        in al, 0x64
        test al, 2
        jnz .empty_8042
    ret
    call check_a20  ;Check a20
    cmp ax, 1
    je .end ;If it worked then end function else:
    .fasta20:
    in al, 0x92
    or al, 2
    out 0x92, al
    .end:
    popa
    call check_a20
ret

在完成这些功能之后,我有了一个将ax打印为十六进制的功能:

main:

    ;Stack, video and other setups(not important)

    call seta20 ;Set a20

    mov dl, 00h ;Set cursor for a print a20 check
    mov dh, 01h 
    mov bh, 00h
    mov ah, 02h
    int 10h

    call check_a20 ;Check a20
    mov dl, al
    mov bl, 02h
    call printhex ;Print dl

    jmp $   ;Infinite loop

printhex: ;print hex input(dl=value, bl=color) 8 bit
    pusha
    mov dh, 0x00
    mov ch, dl ;unshifted (for next hex)
    shr dl, 4 ; get high 4 bits(HEX)
    cmp dl, 9
    jna .1to9 
    .atof: ;if the number is a to f
        add dl, 55
        jmp .next
    .1to9:
        add dl, 48 ;add 48 to make it into a number
    .next:
        mov ah, 0Eh ;print char mode
        mov bh, 0
        mov al, dl
        int 10h ;Print 1st number of the two
    shl ch, 4
    mov dl, ch
    shr dl, 4 ; get high 4 bits(HEX)
    cmp dl, 9
    jna .1to92 
    .atof2: ;if the number is a to f
        add dl, 55
        jmp .print2
    .1to92:
        add dl, 48 ;add 48 to make it into a number
    .print2:
        mov ah, 0Eh ;print char mode
        mov bh, 0
        mov al, dl
        int 10h ;Print 1st number of the two
     popa
ret

我已经知道我打印结果的功能可以正常工作了,因为我已经测试了很多次,但是应该发生的是它应该用我的printhex16打印一个十六进制数字 我拥有的功能

1 个答案:

答案 0 :(得分:3)

您的A20代码背后的方法看起来不错,但是看来您在实现它的方式上存在错误。您有seta20的代码:

seta20: ;Enable the a20 line if it worked then ax = 1 else 0
    pusha
    call check_a20 ;Check a20
    cmp ax, 1
    je .end ;If it worked then end function else:
    .keyboard: ;Test the 8042 keyboard controller
        call .empty_8042
        mov al, 0xd1 ;command write
        out 0x64, al
        call .empty_8042
        mov al, 0xdf ; A20 on
        out 0x60, al
        call .empty_8042 ;wait
    .empty_8042: ;For the 8042 function over this
        in al, 0x64
        test al, 2
        jnz .empty_8042
    ret
    call check_a20  ;Check a20
    cmp ax, 1
    je .end ;If it worked then end function else:
    .fasta20:
    in al, 0x92
    or al, 2
    out 0x92, al
    .end:
    popa
    call check_a20
ret

问题是您已将一个函数放置在另一个函数中,并且无意间使您的代码陷入了该函数中。尤其是这些代码行是问题所在:

        out 0x60, al
        call .empty_8042 ;wait
    .empty_8042: ;For the 8042 function over this
        in al, 0x64
        test al, 2
        jnz .empty_8042
        ret
    call check_a20  ;Check a20

call .empty_8042将调用函数.empty_8042,将刷新8042; ret将返回到call .empty_8042之后的指令,然后将开始执行.empty_8042中的代码。问题是第二次未将其作为函数调用,因此没有正确的返回地址。当它到达ret时,它将尝试返回到栈顶的任何值。这可能会导致您的代码挂起,重新启动系统或执行其他意外的事情。

一个快速解决方案是放置一条 JMP 指令以跳过.empty_8042中的代码。这样的事情会做:

        out 0x60, al
        call .empty_8042 ;wait
        jmp .skip_function
    .empty_8042: ;For the 8042 function over this
        in al, 0x64
        test al, 2
        jnz .empty_8042
        ret
.skip_function:
    call check_a20  ;Check a20

最好将.empty_8042函数与seta20函数分开,这样就不必不必要地跳过.empty_8042。您的代码可能类似于:

empty_8042:
    in al, 0x64
    test al, 2
    jnz empty_8042
    ret

seta20: ;Enable the a20 line if it worked then ax = 1 else 0
    pusha
    call check_a20 ;Check a20
    cmp ax, 1
    je .end ;If it worked then end function else:
    .keyboard: ;Test the 8042 keyboard controller
        call empty_8042
        mov al, 0xd1 ;command write
        out 0x64, al
        call empty_8042
        mov al, 0xdf ; A20 on
        out 0x60, al
        call empty_8042 ;wait

    call check_a20  ;Check a20
    cmp ax, 1
    je .end ;If it worked then end function else:
    .fasta20:
    in al, 0x92
    or al, 2
    out 0x92, al
    .end:
    popa
    call check_a20
    ret