Search code examples
assemblyx86masmreal-modevga

What is the best way to clear the screen in 32-bit x86 assembly language (Video mode 13h)


So at the moment i am copying a screenbuffer (screenbuffer db 64000 DUP(0)) to the video memory (which starts at 0a0000h) to clear the screen. But i was wondering if it is better to just setup the video mode again like this:

mov ax, 13h
int 10h

which seems to clear the screen as well.

Or is there an even better way to clear the screen?


Solution

  • You could use STOSD with a REP prefix to clear video memory for video mode 13 (320x200x256 colors). REP STOSD will repeat STOSD by the count stored in ECX . STOSD will write each DWORD in EAX to ES:[EDI] incrementing EDI by 4 each time.

    REP: Repeats a string instruction the number of times specified in the count register.

    STOSD: stores a doubleword from the EAX register into the destination operand.

    Sample code could look something like:

    cld                    ; Set forward direction for STOSD
    mov ax, 0x0013
    int 0x10               ; Set video mode 0x13 (320x200x256 colors)
    
    push es                ; Save ES if you want to restore it after
    mov ax, 0xa000
    mov es, ax             ; Beginning of VGA memory in segment 0xA000
    mov eax, 0x76767676    ; Set the color to clear with 0x76 (green?) 0x00=black
    xor edi, edi           ; Destination address set to 0
    mov ecx, (320*200)/4   ; We are doing 4 bytes at a time so count = (320*200)/4 DWORDS
    rep stosd              ; Clear video memory
    pop es                 ; Restore ES
    

    This code assumes you are on a 32-bit processor, but doesn't assume you are running in unreal mode.

    If you are using a 16-bit processor (8086/80186/80286) you would have to use the 16-bit registers, and use REP STOSW . CX would be set to (320*200)/2 instead of (320*200)/4. The 16-bit processors don't allow for 32-bit operands so don't support STOSD.

    You could easily convert this code to be an assembly language function.