Search code examples
assemblyx86-16bios

Text Mode Cursor Disappearing after Setting Video Mode or Clearing Screen


I am writing a kernel in x86 assembly, and it is supposed to show a welcome message, wait for a key press, clear the screen, and print input from the keyboard. So far, this is all working, until the screen is cleared. Specifically, after the screen is cleared, the cursor is removed.

First, I tried using this code to clear the screen:

mov ah, 0x06
mov al, 0x00
mov cx, 0x00
mov dh, 0x19
mov dl, 0x50
int 0x10

This does clear the screen, however, the cursor does not show anymore, and keyboard input is not displayed. I tried re-setting the video mode instead:

mov ah, 0x00
mov al, 0x03
int 0x10

This also clears the screen, and keyboard input is displayed, but the cursor is still hidden. I tried using this code to display it:

mov ah, 0x01
mov ch, 0x06
int 0x10

It still would not show the cursor. How would I display the cursor, or clear the screen such that it still is being displayed?

UPDATE: My code:

kernel_main:
    mov ax, 0x07C0
    add ax, 288
    mov ss, ax
    mov sp, 4096

    mov ax, 0x07C0
    mov ds, ax

    mov si, kern_inst
    call print_str
    call pause_key
    call clear_screen

    call function

    mov si, kern_fin
    call print_str

.data:
    kern_inst db "Tap any key to begin the program.", 0
    kern_fin db "The program successfully finished.", 0

function:
    call solid_cursor
    call print_input
    call hide_cursor
    call print_input

    ret

print_str:
    pusha
    mov ah, 0x0E

.repeat:
    lodsb
    cmp al, 0
    je .ret
    int 0x10
    jmp .repeat

.ret:
    popa
    ret

print_input:
    pusha

.repeat:
    mov ah, 0x00
    int 0x16
    cmp al, 0x0D
    je .ret
    mov ah, 0x0E
    int 0x10
    jmp .repeat

.ret:
    popa
    ret

clear_screen:
    pusha
    mov ah, 0x06
    mov al, 0x00
    mov bh, 0x07
    mov cx, 0x00
    mov dh, 0x19
    mov dl, 0x50
    int 0x10
    mov ah, 0x02
    mov bh, 0x00
    mov dx, 0x00
    int 0x10
    popa
    ret

solid_cursor:
    pusha
    mov ah, 0x01
    mov ch, 0x06
    int 0x10
    popa
    ret

hide_cursor:
    pusha
    mov ah, 0x01
    mov ch, 0x26
    int 0x10
    popa
    ret

pause_key:
    push ax
    mov ah, 0x00
    int 0x16
    pop ax
    ret

I discovered that removing the solid_cursor procedure would make the cursor visible; however, when I intentionally make the cursor invisible using hide_cursor, I cannot then revert back to a visible cursor.


Solution

  • You don't need BIOS to clear the screen. Just write zeros on the video buffer at B800:0. It's much faster:

    xor di, di
    mov bx, 0b800h
    mov es, bx
    mov cx, 2000
    mov ax, 0720h ; black background 0 / grey foreground 7 / space character (0x20)
    rep stosw