How can I clear the screen without having to fill it

38,353

Solution 1

Setting a graphics mode through BIOS (int 10h with AH=0) will clear the screen.

Scrolling the screen up or down through BIOS (int 10h with AH=6 or 7) can clear the screen as well.

This will only work where you can invoke BIOS service functions.

MSDOS is where this will always work.

In Windows this will work only in DOS applications and if Windows can actually run them. 64-bit editions of Windows don't support DOS applications at all and starting with Windows Vista even in 32-bit editions of Windows many DOS apps don't work fully.

Remember also that if a DOS application runs in a window in Windows, only that window will get cleared, not the entire screen.

Solution 2

I got this to work (used qemu, NASM)

(http://www.gabrielececchetti.it/Teaching/CalcolatoriElettronici/Docs/i8086_and_DOS_interrupts.pdf)

call cls
jmp $

cls:
  pusha
  mov ah, 0x00
  mov al, 0x03  ; text mode 80x25 16 colours
  int 0x10
  popa
  ret

Solution 3

In assembly, try this:

mov ah, 0x06
mov al, 0
int 10h

And no, you cannot do this on windows. This code can only be used for bootloaders, and assembly kernels (16 bit only, WARNING: DO NOT TRY ON 32 BIT!!!)

If you'd like to do in Windows (Console Applications), then try this:

C++

//YOU SHOULD INCLUDE STDIO.H and CONIO.H. You should also type:
//using namespace std

system("cls");

VB.NET

//You should imports System and other Default namespaces
shell("cls")

C#

System.Diagnostics.Proccess.Start("CMD.exe /c cls");

NOTE: I don't think we can make Console apps using C# or VB. Of course, i never tried. Just saying. But these codes only work for windows.

Solution 4

For Windows console applications, in plain C:

#include <tchar.h>
#include <wincon.h>

VOID
ClearScreen(HANDLE hConsoleOutput)
{
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    COORD coPos;
    DWORD dwWritten;

    GetConsoleScreenBufferInfo(hConsoleOutput, &csbi);

    coPos.X = 0;
    coPos.Y = 0;
    FillConsoleOutputAttribute(hConsoleOutput, csbi.wAttributes,
                               csbi.dwSize.X * csbi.dwSize.Y,
                               coPos, &dwWritten);
    FillConsoleOutputCharacter(hConsoleOutput, TEXT(' '),
                               csbi.dwSize.X * csbi.dwSize.Y,
                               coPos, &dwWritten);
    SetConsoleCursorPosition(hConsoleOutput, coPos);
}

...

// In your main code:
/* Clear the full console screen */
ClearScreen(hOutput);

where the hConsoleOutput is a HANDLE to a console screen-buffer (obtained either via GetStdHandle(STD_OUTPUT_HANDLE), or CreateConsoleScreenBuffer(...), or other means. What this function does is to, first, retrieve the current console screen-buffer information (that contains its current size), then fill the complete screen-buffer with the default text attribute and with spaces, then finally place the cursor at (0,0).

Share:
38,353
Gabriele Cirulli
Author by

Gabriele Cirulli

Updated on July 09, 2022

Comments

  • Gabriele Cirulli
    Gabriele Cirulli almost 2 years

    Does an interrupt service routine exist to help me clear the screen of the terminal? Will it work on windows?

  • Gabriele Cirulli
    Gabriele Cirulli over 12 years
    Thanks, int 10,0 worked perfectly. Any idea on how you'd do it for 32/64bit console applications? Just printing a few spaces?
  • Alexey Frunze
    Alexey Frunze over 12 years
    If it's a 32-bit DPMI DOS application, you can still use the BIOS functions. Other than that, you're out of luck. BIOS doesn't work in 64-bit mode. Neither does DOS.
  • Michael Petch
    Michael Petch over 8 years
    Technically if you are running this on a 32-bit Windows, you can run the compiled assembler code (assuming it is compiled and linked for 16-bit). It will run via NTVDM which does allow you to run 16-bit Windows and DOS applications as well as some 32-Bit DOS apps. Many of the BIOS and DOS Interrupts are supported and there are some limits on the video modes. The general exception would be anything that would compromise security, On 64-bit Windows you'll need an emulator or a virtual machine.
  • adrian
    adrian over 6 years
    Funny, using the assembly method on QEMU only clears the first character.
  • Jony
    Jony almost 5 years
    mov ax, 0x0003 then int 0x10 works on MacOS, bochs with nasm.