Question

I need some help with some inline assembly using the GNU GCC compiler in Code::Blocks. There is currently no method in standard C++ available which equates to the old 'CLS' function of DOS. I refuse to stray from standard/ANSI C++ and going through the Windows API or using graphics libraries is out of the question because I need something cross-platform. The 'system("CLS") routine that's being taught to these young kids is out of the question because is opens up a huge backdoor to security issues, and Microsoft wants the end-user bound and gagged to Windows. I'm also getting on in years and can't afford to contribute the time necessary to learn Assembly language to acheive this, so I am hoping one of you ladies or gentlemen well-versed in inline assembly can do a bit of translation for me. An old friend of mine who used to do assembly programming but retired a while back, dug up two snippets of his old assembly code, one of which pauses the screen waiting for the user to press any key. I've done this countless times in C++ but I also want to experiment and see if we can get something going in inline assembly, just to see if it's any faster and smoother. The second snippet does in fact perform a true clear screen just like the DOS 'CLS' function. So, I wanted to experiment with that as well, to see if we could finally impart at least a jury-rigged fixed for a cross platform CLS in C++ programs. My friend thinks this assembly code is 16-bit, but he can't remember for dure since he retired back in 2002, and he has no knowledge of C++ so we're no help to each other. I want to creat to separate functions which I can call from anywhere in my code.

If someone much brighter than myself could translate the assembly code to inline assembly, I would be so very grateful to you, and you would be doing a LOT of C++ coders a huge favor. I need this code translated to where it will work on Intel x86\64 architecture, and can be successfully compiled (using a respective compiler) to work on Windows 32\64, Linux, UNIX, Ubuntu, or OS X. Please be as thorough as possible with your translation because I don't understand a lick of assembly and I can't understand the 2 or 3 books I tried to read in order to learn it. I basically need to be able to just copy and paste it (only the inline assembly code and any requirements to compile\use it). I need these two functions because I author a tremendous amount of console apps for the company I work with, my part mostly dealing with networking and intranet. The IT\network guys demand console apps to configure their little toys to cut down on overhead, but they need to be able to read the prompts and input as is comes to them rather than just watching a bunch of text roll by. I've already achieved this with system dependent hacks and they think it's currently perfect the way they can interact with the programs. But, I want to move them away from system dependence and make them cross-platform before I retire next year and can leave my employers in fairly decent shape.

I'm sorry this was so long a post but I need the help. Thank you very much in advance and anything someone might be able to work out will be greatly appreciated.

Best regards, GypsyPrince

Example:

#include <someheader>

void clrscrn()    // Clear user's screen like 'CLS'...
{
        // Execute assembly code to clear screen...
    mov ax,0A000h    // Yes, this is the actual assembly code...
    mov es,ax
    xor di,di
    xor ax,ax
    mov cx,32000d      
    cld
    rep stosw
}

void pauseapp(bool ShowPrompt = false)    // Halt execution of program til user presses key...
{
    if(ShowPrompt)    // If the prompt is displayed...
    {
        std::cout << "\nPress any key to continue...";   // Prompt user to press a key...
     }

     /* Execute assembly code to halt program and wait for keypress - show flashing caret at end of prompt if prompt is displayed (just like in an IDE console)... */         
    mov ah,00    // Yes, this is the actual assembly code too...    
    int 16h
}


int main()
{
    std::cout << "First block of text paused to give user time to read." << endl;
    pauseapp(true)

    clrscrn();
    std::cout << "Second block of text paused to give user time to read." << endl;
    pauseapp(true)

    clrscrn();
    std::cout << "Third block of text paused to give user time to read." << endl;
    pauseapp(true)

    clrscrn();

    return 0;
}
Was it helpful?

Solution

That 16-bit assembly code (or anything like it) has unfortunately zero chance of working on a modern operating system. It depends on a memory-mapped text display area, which simply doesn't exist in today's systems.

The most portable way to clear the screen might be to use ANSI escape codes:

fputs(stdout, "\x1b[2J");

This is likely to work on most of the platforms you listed, but you'll have to test it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top