Clear Screen in a Linux Terminal Using Assembly

Clear screen in a Linux terminal using assembly?

In Bash:

echo -ne "\033[H\033[2J"

In C:

printf("\033[H\033[2J");

How do I find the string:

$ strace -e trace=write clear >/dev/null 
write(1, "\33[H\33[2J", 7) = 7
Process 7983 detached

Clean console on assembly

To imitate the terminals clear command have in a .data section:

ClearTerm: db   27,"[H",27,"[2J"    ; <ESC> [H <ESC> [2J
CLEARLEN equ $-ClearTerm ; Length of term clear string

then whenever you want to clear the terminal do:

mov eax, 4                          ; Specify sys_write call
mov ebx, 1 ; Specify File Descriptor 1: Stdout
mov ecx, ClearTerm ; Pass offset of terminal control string
mov edx, CLEARLEN ; Pass the length of terminal control string
int 80h

How can I clear the screen without having to fill it

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.

Clearing the screen by printing a character?

If you want to clear the screen, the "ANSI" sequence in a printf

\033[2J

clears the entire screen, e.g.,

printf '\033[2J'

The command-line clear program uses this, along with moving the cursor to the "home" position, again an "ANSI" sequence:

\033[H

The program gets the information from the terminal database. For example, for TERM=vt100, it might see this (using \E as \033):

clear=\E[H\E[J$<50>

(the $<50> indicates padding needed for real VT100s). You might notice that the 2 is absent from this string. That is because the cursor is first moved to the home (upper left) position, and the 2 (entire screen) is not necessary. Eliminating that from the string made VT100s a little faster.

On the other hand, if you just want to reset the terminal, you can use the VT100-style RIS:

\033c

but that has side-effects, besides not being in ECMA-48. These bug reports were for side-effects of \033c:

  • Debian Bug report logs - #60377
    "reset" broken for dumb terminals
  • Debian Bug report logs - #239205
    "reset changes a unicode console to non-unicode"

Further reading:

  • Why doesn't the screen clear when I type control/L?
  • XTerm Control Sequences

CSI Ps J Erase in Display (ED).
Ps = 0 -> Erase Below (default).
Ps = 1 -> Erase Above.
Ps = 2 -> Erase All.
Ps = 3 -> Erase Saved Lines (xterm).
  • ECMA-48: Control Functions for Coded Character Sets

How to clear screen on protected mode

You can write \x1b[2J to the standard output so the terminal get cleared and fix the cursor position using \x1b[H, for example in nasm:

global  _start

section .data
clr db 0x1b, "[2J", 0x1b, "[H"
clrlen equ $ - clr

section .text
_start:
mov eax, 4
mov ebx, 1
mov ecx, clr
mov edx, clrlen
int 0x80

mov eax, 1
mov ebx, 0
int 0x80

for gnu assembler:

.globl _start

.data
clr : .ascii "\x1b[2J\x1b[H"
clrlen = . - clr

.text
_start:
movl $4, %eax
movl $1, %ebx
movl $clr, %ecx
movl $clrlen, %edx
int $0x80

movl $1, %eax
movl $0, %ebx
int $0x80

How Do You Clear The IRB Console?

On Mac OS X or Linux you can use Ctrl + L to clear the IRB screen.

Clearing the Console in C++

About your error... you have to...

#include <iostream> 
#include <string>

using namespace std;

If you are using just windows use windows console API.
If you are using a linux\unix terminal, use escape codes.
You can do a #if to choose between the two methods.

On linux\unix use the write function defined in in this way:

write(1,"\E[H\E[2J",7); // we use ANSI escape sequences here.

Here is the microsoft page that explain how to do that.

http://support.microsoft.com/kb/99261

The really bad console api microsoft use for the console always makes me angry :) why 100 lines of code to clear a screen? :)

Now the if... you should create a clearscreen.h file and a clearscreen.cpp file.

In clearscreen.h we just put our function.

 void clearconsole();

In clearscreen.cpp we put our code for both operative systems

#ifdef _WIN32 || _WIN64

#include <windows.h>

void clearconsole()
{
...
// 100 lines of codes copied from microsoft article
}

#else

#include <unistd.h>

void clearconsole()
{
write(1,"\E[H\E[2J",7);
}

#endif


Related Topics



Leave a reply



Submit