CLOSE

In x86 assembly language, clearing the screen during the boot process or in real mode involves utilizing BIOS interrupts. It can be achieved using various ways, we will explore each of them in detail.

Different ways to achieve clear screen:

  1. Using BIOS Functions:
    1. Using Scroll Up Function
    2. Using Scroll Down Function
    3. Changing Video Mode
  2. Without Using BIOS Functions:
    1. Writing Directly to Video Memory

For the Scroll up, down and writing directly to video memory options, we have to explicitly reset the cursor position, which also we have covered in this chapter.

[1] Clearing the Screen Using BIOS Functions:

To clear the screen in real mode, we'll use the BIOS interrupt int 0x10. This interrupt allows us to call various video services provided by the BIOS, including screen clearing.

1.1 Using Scroll Up Function

int 0x10 Interrupt Function AH=06H (0x06):

  • AH = 06: Scroll up function
  • AL: Number of lines to scroll; previous lines are blanked. If AL is 0 or AL is greater than the screen size, the entire window is blanked.
  • BH: Attribute to be used on the blank line
  • CH, CL: Row and column of the upper left corner of the scroll window
  • DH, DL: Row and column of the lower right corner of the scroll window

Code:

Here's an example of x86 assembly code to clear the screen:

[ORG 0x7C00]   ; Set the origin to the BIOS boot sector address

    ; Clear the screen
    mov ah, 0x06    ; AH = 06h (Scroll up function)
    mov al, 0       ; AL = 0 lines to scroll (clear the entire screen)
    mov bh, 0x07    ; BH = 07h (attribute byte, white on black)
    mov cx, 0x0000  ; CH = 0, CL = 0 (upper left corner)
    mov dx, 0x184F  ; DH = 24, DL = 79 (lower right corner)
    int 0x10        ; Call BIOS video interrupt

    ; Infinite loop
    cli             ; Clear interrupts
.endloop:
    hlt             ; Halt processor
    jmp .endloop    ; Infinite loop

times 510 - ($-$$) db 0   ; Fill the rest of the sector with zeros
dw 0xAA55                 ; Boot signature

Explanation:

  • mov ah, 0x06 sets up the video scrolling function.
  • mov al, 0 specifies that we want to scroll the entire screen.
  • mov bh, 0x07 sets the attribute byte to white on black.
  • mov cx, 0x0000 sets the position to the upper left corner.
  • mov dx, 0x184F sets the position to the lower right corner.
  • int 0x10 calls the BIOS video interrupt to clear the screen.

After clearing the screen, the code enters an infinite loop to halt the processor.

1.2 Using Scroll Down Function:

int 0x10Interrupt Function AH = 07h (0x7)

  • AH = 07: Scroll down function
  • AL: Number of lines to scroll; previous lines are blanked. If AL is 0 or AL is greater than the screen size, the entire window is blanked.
  • BH: Attribute to be used on the blank line
  • CH, CL: Row and column of the upper left corner of the scroll window
  • DH, DL: Row and column of the lower right corner of the scroll window

Code:

[ORG 0x7C00]   ; Set the origin to the BIOS boot sector address

    ; Clear the screen
    mov ah, 0x07    ; AH = 07h (Scroll down function)
    mov al, 0       ; AL = 0 lines to scroll (clear the entire screen)
    mov bh, 0x07    ; BH = 07h (attribute byte, white on black)
    mov cx, 0x0000  ; CH = 0, CL = 0 (upper left corner)
    mov dx, 0x184F  ; DH = 24, DL = 79 (lower right corner)
    int 0x10        ; Call BIOS video interrupt

    ; Infinite loop
    cli             ; Clear interrupts
.endloop:
    hlt             ; Halt processor
    jmp .endloop    ; Infinite loop

times 510 - ($-$$) db 0   ; Fill the rest of the sector with zeros
dw 0xAA55                 ; Boot signature

Explanation:

  • mov ah, 0x07 sets up the video scrolling function for scrolling down.
  • mov al, 0 specifies that we want to scroll the entire screen.
  • mov bh, 0x07 sets the attribute byte to white on black.
  • mov cx, 0x0000 sets the position to the upper left corner.
  • mov dx, 0x184F sets the position to the lower right corner.
  • int 0x10 calls the BIOS video interrupt to clear the screen by scrolling down.
image-123.png

From the Screenshot above you will noticed that all those BIOS text is erased but the cursor is still at its previous position hasn't erased to (0,0)

  • Actually cursor is blinking at this position, Can't capture blinking in the screenshot.

Now, let's set the position of cursor as well;

1.3 Set Cursor Position (Explicitly for Scroll Up and Down Functions)

int 0x10Interrupt Function AH = 02H (0x02):

  • AH = 02: Set cursor position
  • BH: Page number (0 for graphics modes)
  • DH: Row
  • DL: Column

Code Snippet:

[ORG 0x7C00]   ; Set the origin to the BIOS boot sector address

    ; Clear the screen
    mov ah, 0x07    ; AH = 07h (Scroll down function)
    mov al, 0       ; AL = 0 lines to scroll (clear the entire screen)
    mov bh, 0x07    ; BH = 07h (attribute byte, white on black)
    mov cx, 0x0000  ; CH = 0, CL = 0 (upper left corner)
    mov dx, 0x184F  ; DH = 24, DL = 79 (lower right corner)
    int 0x10        ; Call BIOS video interrupt

    ; Move cursor to top-left corner
    mov ah, 0x02    ; AH = 02h (Set cursor position)
    mov bh, 0x00    ; BH = 0 (Page number)
    mov dh, 0x00    ; DH = 0 (Row)
    mov dl, 0x00    ; DL = 0 (Column)
    int 0x10        ; Call BIOS video interrupt

    ; Infinite loop
    cli             ; Clear interrupts
.endloop:
    hlt             ; Halt processor
    jmp .endloop    ; Infinite loop

times 510 - ($-$$) db 0   ; Fill the rest of the sector with zeros
dw 0xAA55                 ; Boot signature

Explanation:

  • mov ah, 0x02 sets up the video function to set the cursor position.
  • mov bh, 0x00, mov dh, 0x00, and mov dl, 0x00 set the cursor position to the top-left corner.
  • int 0x10 calls the BIOS video interrupt to move the cursor to the top-left corner.
image-124.png

1.4 Using BIOS Interrupt 0x10, Function 0x00 (Set Video Mode)

Setting the video mode can also be used to clear the screen as it reinitializes the video display.

BITS 16


    org 0x7C00

start:
    ; Set video mode to 80x25 text mode (mode 3), which clears the screen
    mov ah, 0x00
    mov al, 0x03
    int 0x10

    ; No need to reset the cursor as it's already at the top-left corner

    hlt                 ; Halt the CPU

times 510-($-$$) db 0   ; Pad the rest of the boot sector with zeroes
dw 0xAA55               ; Boot sector signature

This code above sets the default video mode (mode 3) again by using BIOS interrupt function. Resetting the video mode is a simple way to clear the screen since it reinitializes the display. However, this method also resets other display settings.

[2] Without Using BIOS Functions

2.1 Writing Directly to Video Memory

Another method is to write directly to the video memory located at 0xB8000 for text mode. This method involves directly manipulating the video memory to set all characters to spaces with a given attribute.

; clear_screen_direct_memory.asm
BITS 16




org 0x7C00

start:

    ; Clear the screen by writing spaces to video memory
    mov ax, 0xB800      ; Segment address of video memory
    mov es, ax          ; Set ES register to video memory segment
    xor di, di          ; Set DI to 0 (start of video memory)
    mov cx, 2000        ; 80 * 25 = 2000 character cells

clear_loop:
    mov ax, 0x0720      ; 0x07 is the attribute (light gray on black), 0x20 is the space character
    stosw               ; Store word at ES:DI from AX and increment DI by 2
    loop clear_loop     ; Repeat for all character cells

    ; Reset cursor position to the top-left corner
    mov ah, 0x02        ; Set cursor position function
    mov bh, 0x00        ; Page number (0)
    mov dh, 0x00        ; Row (0)
    mov dl, 0x00        ; Column (0)
    int 0x10            ; Call BIOS interrupt

    hlt                 ; Halt the CPU

times 510-($-$$) db 0   ; Pad the rest of the boot sector with zeroes
dw 0xAA55               ; Boot sector signature

References:

https://stanislavs.org/helppc/int_10.html