Search code examples
assemblyx86nasmdosbios

x86 Video mode Clear Screen


Hello I'm trying to clear the screen in DOS video mode with this code.

But when I run it, the dot is still there!

    org 100h

    mov     ah, 0       ; set display mode function.
    mov     al, 13h     ; mode 13h = 640x480 pixels, 256 colors.
    int     10h         ; set it!

    mov     cx, 10      ; column
    mov     dx, 10      ; row
    mov     al, 15      ; white
    mov     ah, 0ch     ; put pixel
    int     10h         ; draw pixel

    ; -------  clear the screen ----------     
    ; -------  doesn't work! dot is still there

    mov ax,0B800h
    mov es,ax
    xor di,di
    xor ax,ax
    mov cx,2000d
    cld
    rep stosw

    ; -------------------------------------

    ;wait for keypress
    mov ah,00
    int 16h

    mov ax, 4c00h ; exit to operating system.
    int 21h

    ;======================================================

I tried resetting the video mode with INT 10 but this gives me blinks which is unwanted inside my loop


Solution

  • There are some issues with your code.

    First, BIOS mode 13h is not 640x480 at 8 bits/pixel, it's 320x200 at 8 bits/pixel.

    B800h is the address of BIOS text modes. BIOS graphics modes use A000h.

    So it should be:

    mov ax,0A000h
    mov es,ax
    xor di, di     ; ES:0 is the start of the framebuffer
    

    320x200 at 8 bits consumes 320*200 = 64 000 bytes of video memory. So the value of cx is incorrect. It should be:

    mov cx,32000d    ; you can write 320 * 200/2 in your source if you want
    cld
    xor ax,ax
    rep stosw        ; zero 2*CX bytes at ES:DI