简体   繁体   English

内联汇编编译为标准C ++代码

[英]Inline assembly compiled into standard C++ code

I need some help with some inline assembly using the GNU GCC compiler in Code::Blocks. 我需要一些使用Code :: Blocks中的GNU GCC编译器进行内联汇编的帮助。 There is currently no method in standard C++ available which equates to the old 'CLS' function of DOS. 目前在标准C ++中没有可用的方法,这相当于DOS的旧“CLS”功能。 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. 我拒绝偏离标准/ ANSI C ++并通过Windows API或使用图形库是不可能的,因为我需要一些跨平台的东西。 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. 正在向这些年幼的孩子讲授的'系统(“CLS”)例程是不可能的,因为它为安全问题开辟了一个巨大的后门,微软希望最终用户能够绑定并捆绑到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. 我已经用C ++完成了无数次,但我也想尝试一下,看看我们是否可以在内联汇编中获得一些东西,只是为了看看它是否更快更顺畅。 The second snippet does in fact perform a true clear screen just like the DOS 'CLS' function. 实际上,第二个片段确实执行了一个真正清晰的屏幕,就像DOS'CLS'功能一样。 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. 所以,我也想尝试一下,看看我们是否最终能够为C ++程序中的跨平台CLS提供至少一个由陪审团操纵的固定程序。 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. 我的朋友认为这个汇编代码是16位的,但是自从2002年退休以来他就不记得了,而且他对C ++一无所知,所以我们对彼此没有帮助。 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. 如果比我更聪明的人可以将汇编代码转换为内联汇编,那么我会非常感谢你,并且你会为很多C ++编码人员做出巨大帮助。 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. 我需要将此代码转换为可在英特尔x86 \\ 64架构上运行的代码,并且可以成功编译(使用相应的编译器)以在Windows 32 \\ 64,Linux,UNIX,Ubuntu或OS X上运行。请尽可能彻底你的翻译可能,因为我不理解大会的舔,我无法理解我试图阅读的2或3本书,以便学习它。 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. IT \\网络人员要求控制台应用程序配置他们的小玩具以减少开销,但他们需要能够读取提示和输入,而不是只是看着一堆文字滚动。 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 最好的问候,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;
}

That 16-bit assembly code (or anything like it) has unfortunately zero chance of working on a modern operating system. 这16位汇编代码(或类似的东西),有不幸的几率为零现代操作系统上工作的。 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: 清除屏幕最便携的方法可能是使用ANSI转义码:

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

This is likely to work on most of the platforms you listed, but you'll have to test it. 这很可能适用于您列出的大多数平台,但您必须对其进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM