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
retExplanation:
pushasaves all general-purpose registers.mov edi, FRAMEBUFFER_ADDRESS_MODE_13: Setedito point to the start of the framebuffer.mov ecx, MODE_13_SCREEN_WIDTH * MODE_13_SCREEN_HEIGHT: setsecxto the total number of pixels (320 * 200).rep stosbfills the framebuffer with the color value inal.poparestores all general-purpose registers.retreturns from the function.
Calling the Function:
mov al, 13 ; magenta color
call clear_screenOutput:

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
rdtscto read the time-stamp counter. It reads the time-stamp counter, which increments every CPU cycle, intoeaxandedx. xor eax, edxXORs the high and low parts for added randomness.and al, 0xFFkeeps 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
retExplanation:
- The
draw_random_pixelsfunction fills the screen with random colors. pushasaves all general-purpose registers.mov edi, FRAMEBUFFER_ADDRESS_MODE_13setsedito the start of the framebuffer.mov ecx, MODE_13_SCREEN_WIDTH * MODE_13_SCREEN_HEIGHTsetsecxto the total number of pixels.- In the loop, it calls
generate_randomto get a random color inaland stores it in the framebuffer usingstosb. loop .draw_looprepeats the process for all pixels.- The
loopinstruction decrements theecxregister and repeats the loop untilecxbecomes zero. This means the loop will run exactlyecxtimes, which in this case is the total number of pixels on the screen.
- The
poparestores all general-purpose registers.

Leave a comment
Your email address will not be published. Required fields are marked *
