# [asm 80x86] Clear Screen.

This topic is 3762 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi. I tryed find a propper way to clear the screen on the console. But, unfortunately, I havent. I tryed some code but I cant handle it. It is supposed to clear a screen within a c++ console app, so I put the code into the __asm section. And this goes like this. (I writes directly onto the vram.)

void cls( void )
{
__asm
{
push ax
push es
push di

mov ax,0
mov es,ax
mov di,0

mov ax,0B800h 	; segment of video buffer
mov es,ax 	; put this into es
xor di,di 	; clear di, ES:DI points to video memory
mov ah,4 	; attribute - red
mov al,'G' 	; character to put there
mov cx,4000 	; amount of times to put it there
cld 		; direction - forwards
rep stosw 	; output character at ES:[DI]

pop di
pop es
pop ax
}
}


Here the code is from the internet and should draw G's onto the screen! But I get a mem violation at the second mov es, ax. I need some help please. This code should work on the x86 platform. Much thanks Alex

##### Share on other sites
You're OS may not be allowing you to access vram directly. Modern OS's run your programs in protected mode, so you would be accessing virtual memory, not physical memory.

##### Share on other sites
I doubt that code will work. It looks like it assumes you are running in DOS mode (which is bot the same as running a console program). Have you tried something like this. First result from a google of "winapi clear console".

##### Share on other sites
Hi, and thanks.

I dont want to use os specific functions like System( "cls" ).
This wont work under other compilers or OSs like windows.

So you havent any idea how this could work.

I mean, this can be a hard task to achieve, it is simply accessing
the vram and override it completely. But my asm knowledge isnt as strong to
Or is there any chance to do it via a bios interrupt?
No, VRAM sounds better.

Alex

##### Share on other sites
Quote:
 Original post by directNoobHi, and thanks.I dont want to use os specific functions like System( "cls" ).This wont work under other compilers or OSs like windows.So you havent any idea how this could work.I mean, this can be a hard task to achieve, it is simply accessing the vram and override it completely. But my asm knowledge isnt as strong to handle this task. Or is there any chance to do it via a bios interrupt?No, VRAM sounds better.Alex

You are not writing an operating system, so you cannot write directly to video memory. You will have to go through the OS to do this.

##### Share on other sites
modern multitasking operating systems do not give you direct access to hardware resources (not their memory map, not their ports, not their dma channels, not their irq's.. nothing)

this is unconditional in these OS's .. if they let you slam around with the hardware directly, then you can easily destabilize the system

even with directx, which gives "direct" access to video memory, you have to alert the operating system that you are about to do so ("locking" the "surface") and then notify it when you are finished .. while your process is doing that, no other process may access that "surface" .. period .. unconditional

##### Share on other sites
But with an ordinary assembled program, I can access these regions...
You mean, I cant do it with __asm?

But what should I do then?

Alex

##### Share on other sites
@Rockoon1:

P.S.
I dont want to access memory on the graphics card but the vram from the dos console!

##### Share on other sites
Quote:
 Original post by directNoob@Rockoon1:P.S.I dont want to access memory on the graphics card but the vram from the dos console! Because you wrote about dx...

It was a case-in-point.

..are you saying that in C you can access segment B800h?

A) exactly what C compiler is this that lets you use segment:offset pairs (is this a 16-bit compiler??)
B) exactly what OS are you doing this from

##### Share on other sites
I suspect that your "orginary assembled program" is 16-bit, but you are now trying to use a 32-bit C compiler to emit the same assembly statements .. it doesnt work like that at all

a 16-bit program runs under a real mode emulation layer
a 32-bit program runs under protected mode

segment:offset pairs like the ones you are using are for real (16-bit) mode only

##### Share on other sites
Hi Rockoon1.

No, I havent meant a c compiler but a assembler. A normal assembler like
MASM or something like this.

Anyway. I trust you that this doesnt work! As I said, I just have a little experience.

But what can I do due to this problem? How can I clear the screen without
using compiler or OS dependent function? Like System("cls" ), wich works under win, but I think this doesnt work under any other OS, or any other compiler, like the borland compiler?!

Do you have any suggestions?

Thanks
Alex

##### Share on other sites
By using conditional compilation to separate OS specific code. Oh, and don't use system("cls") under windows either. Use the functions here for windows. There's an example here on how to write the program, but it doesn't say how to get the handle to the console windows. You can get it by calling "GetConsoleWindow" Function.

##### Share on other sites
You're really confused. You cannot access physical VRAM or BIOS interrupts on Windows or Linux or any recent OS.

If you want to do that then

1) get a DOS compiler
2) install DOS, or download a DOS emulator, or use the DOS emulator that is included with Windows

The console you see when you create a console application with a Windows compiler is not a "real" console anyway, it's a regular Windows GUI that just looks like a console.

##### Share on other sites
@nobodynews:
Thanks. But System("cls") works perfectly with the win c++ console app.

But the problem is, the work Im doing will not be compiled under win, so it wont be compiled under VC++ compiler. This is the reason I need to do it in c++/c in
ANSI C style. It must work with other compilers. But I am using win and VC++.
But I strongly program ANSI C and avoid windows related functions!
Because without the win .dll s, the code is worthless under the testers environment.

@outRider:
Ah, ok. I thought I can get somthing like a virtual VRAM which is responsible for drawing the dos console.

So you say there is no other way to clear the screen but System(..) or the one along with the borland compiler clrcls() or how it is call ?!

Alex

##### Share on other sites
Quote:
 @nobodynews:Thanks. But System("cls") works perfectly with the win c++ console app.

Of course. However, the command "cls" is Windows specific, even if System() is not. Because you are looking for portability, you should not use this.

Quote:
 But the problem is, the work Im doing will not be compiled under win, so it wont be compiled under VC++ compiler. This is the reason I need to do it in c++/c inANSI C style. It must work with other compilers. But I am using win and VC++.

There is no ANSI compatible method to clear the screen. As others have pointed out, on modern operating systems, you must either directly or indirectly go through the system API.

This can be done by either using your provided System API (For example, the Win32 API), or another library. This is all in combination with conditional compilation (As nobodynews mentioned) to insure different variants for different systems.

Quote:
 @outRider:Ah, ok. I thought I can get somthing like a virtual VRAM which is responsible for drawing the dos console.

Under the DOS NTVDM, it does give you a virtual address space. However, this is only 16 bit DOS applications, which have an emulated real mode envirement.

Hence, if you are developing a 16 bit DOS application, you can directly access video memory by writing to its mapped region (By default text modes is 0xb8000)

Remember that there is a difference between Win32 console applications on 16bit DOS programs. Win32 console programs look like a DOS program, but are 32 bit, NOT 16 bit.

Quote:
 So you say there is no other way to clear the screen but System(..) or the one along with the borland compiler clrcls() or how it is call ?!

Of course there is. You must keep in mind, however, that no matter which option you choose, it is system specific.

If portability is of concern, perhaps you can try using a portable library or API instead?

##### Share on other sites
Hi crypter.

Quote:
 Of course there is. You must keep in mind, however, that no matter which option you choose, it is system specific.

But this means that there is no way.:(
The work Im doing is a implementation for a course at the university and the tester just copies the .h and .cpp files. This also means, I cant include any additional libs or .h or .cpp files.

This is very sad that there is no propper way to clear the screen system indipendent.

However. Then I do it the stupid way and just scroll a lines with cout << endl.

Thanks
Alex

##### Share on other sites
can you use backspace? there is an ascii character for backspace. look it up and see if emitting a bunch of those does the trick.

##### Share on other sites
Quote:
 Original post by directNoobThis is very sad that there is no propper way to clear the screen system indipendent.

When C was created your average state of the art human-accessable output device was a jumped-up typewriter connected to the computer. You can't clear the screen of a typewriter. C++ doesn't improve things in this regard.

It's unfortunate. In general, if it's not an abstraction on top of a file you can't do IO with it using the standard C/C++ library in any sort of portable way. You either need non-portable code or a third-party library that does the non-portable stuff for you.