CLOSE

In our last chapter we drawn the pixel, character and string on the screen. Sometimes we need to clear the screen with a particular with color (mostly black traditional standard). Later on we will do a interesting project to drawn random colorful pixel in the entire screen.

1 clear_screen:

All we need is to fill the framebuffer with dedicated color in order to clear the complete screen. This is achieved by traversing through the framebuffer using a loop of count of size of screen (width * height). For every pixel position set it with the color with which we need to clear the screen with.

Below is the code to clear the screen:

FRAMEBUFFER_ADDRESS_MODE_13 equ 0xA0000
MODE_13_SCREEN_WIDTH equ 320
MODE_13_SCREEN_HEIGHT  equ 200


; Function to clear the screen with a specific color
; Inputs:
; al = color to fill
clear_screen:
    pusha               ; Save all general-purpose registers

    ; Point EDI to the start of the framebuffer
    mov edi, FRAMEBUFFER_ADDRESS_MODE_13

    ; Set ECX to the total number of pixels
    mov ecx, MODE_13_SCREEN_WIDTH * MODE_13_SCREEN_HEIGHT

    ; Clear the screen
    rep stosb           ; Fill CX bytes at ES:DI with AL

    popa                ; Restore all general-purpose registers
 ret

Explanation:

  • pusha saves all general-purpose registers.
  • mov edi, FRAMEBUFFER_ADDRESS_MODE_13: Set edi to point to the start of the framebuffer.
  • mov ecx, MODE_13_SCREEN_WIDTH * MODE_13_SCREEN_HEIGHT: sets ecx to the total number of pixels (320 * 200).
  • rep stosb fills the framebuffer with the color value in al.
  • popa restores all general-purpose registers.
  • ret returns from the function.

Calling the Function:

mov al, 13	; magenta color
call clear_screen

Output:

image-142.png

2 Draw Random Pixels

Now we will cover our entire screen with random colors of pixels. For that we would need a random number generator. Since mode 13h has 2^8 (256) allowed colors. we would generate random number in between them. For that we will use the rdtsc instruction.

The rdtsc instruction reads the time-stamp counter, which is a 64-bit register that increments every CPU clock cycle. You can use rdtsc to generate a random value by taking the lower 8 bits of the counter.

Here is random color generator function:

; Function to generate a random number
; Returns the random number in AL
generate_random:
    rdtsc               ; Read the Time Stamp Counter
    xor eax, edx        ; XOR the high and low parts for more randomness
    and al, 0xFF        ; Use only the lower 8 bits for the color (0-255)
    ret
  • Uses rdtsc to read the time-stamp counter. It reads the time-stamp counter, which increments every CPU cycle, into eax and edx.
  • xor eax, edx XORs the high and low parts for added randomness.
  • and al, 0xFF keeps only the lower 8 bits of the result, making it a value between 0 and 255.
FRAMEBUFFER_ADDRESS_MODE_13 equ 0xA0000
MODE_13_SCREEN_WIDTH equ 320
MODE_13_SCREEN_HEIGHT  equ 200

; Function to draw random colorful pixels
draw_random_pixels:
    pusha               ; Save all general-purpose registers

    mov edi, FRAMEBUFFER_ADDRESS_MODE_13; DI points to the start of the framebuffer
    mov ecx, MODE_13_SCREEN_WIDTH * MODE_13_SCREEN_HEIGHT ; CX = total number of pixels

.draw_loop:
    call generate_random         ; Get a random color in AL
    stosb                        ; Store the random color in the framebuffer
    loop .draw_loop               ; Repeat for all pixels

    popa                ; Restore all general-purpose registers
    ret

Explanation:

  • The draw_random_pixels function fills the screen with random colors.
  • pusha saves all general-purpose registers.
  • mov edi, FRAMEBUFFER_ADDRESS_MODE_13 sets edi to the start of the framebuffer.
  • mov ecx, MODE_13_SCREEN_WIDTH * MODE_13_SCREEN_HEIGHT sets ecx to the total number of pixels.
  • In the loop, it calls generate_random to get a random color in al and stores it in the framebuffer using stosb.
  • loop .draw_loop repeats the process for all pixels.
    • The loop instruction decrements the ecx register and repeats the loop until ecx becomes zero. This means the loop will run exactly ecx times, which in this case is the total number of pixels on the screen.
  • popa restores all general-purpose registers.
image-143.png