简体   繁体   English

使用 gcc 编译 c++ 代码并实现它

[英]Compile c++ code with inline-assembly & Intel syntax with gcc and implement it

I'm trying to implement c++ program which read string from c++ part of program, process it with inline assembly (convert all latin symbols to their HEX representation by number in ASCII table) and print it with c++ code.我正在尝试实现 c++ 程序,该程序从程序的 c++ 部分读取字符串,使用内联汇编处理它(通过 ASCII 表中的数字将所有拉丁符号转换为其十六进制表示)并使用 Z6CE809EACF90BA125B40FA4BD903962E 打印它I have a problem with compiling it, I've found the way to do it without mistakes, but I can't implement it.我在编译它时遇到了问题,我找到了没有错误的方法,但我无法实现它。

I've tried to compile that with:我试图用以下方法编译它:

gcc -S -masm=intel code.cpp -o code

But when I try to implement it, there are a lot of errors like that:但是当我尝试实现它时,会出现很多这样的错误:

./main: line 1: .file: command not found
./main: line 2: .intel_syntax: command not found
./main: line 3: .text: command not found
./main: line 4: .section: command not found
./main: line 5: .type: command not found
./main: line 6: .size: command not found
./main: line 7: _ZStL19piecewise_construct:: command not found
./main: line 8: .zero: command not found
./main: line 9: .local: command not found
./main: line 10: .comm: command not found
./main: line 11: .LC0:: command not found

Here is a code of my program:这是我的程序的代码:

#include <iostream>
#include <fstream>
#include <clocale>
#include <curses.h>

#define MAX_LENGTH 80

using namespace std;

void printWelcomeStr();

int main()
{
    char input_string[MAX_LENGTH];
    char output_string[MAX_LENGTH];
    cin.getline(input_string, MAX_LENGTH, '\n');
    asm
    (
        ".intel_syntax;"
        "lea eax, input_string\n"
        "mov esi, %eax\n"
        "lea eax, output_string\n"
        "mov %edi, %eax\n"

        "BEGINCYCLE:\n"
        "lodsb\n"
            "test al, al\n"
            "je EXIT\n"

            "cmp al, 41h\n" // >= 'A'
            "jnbe LatinLetterGeneral\n"

            "jmp NEXT\n"

            "LatinLetterGeneral:\n"
                "cmp %al, 5Ah\n" // <= 'Z'
                "jnae LatinLetterFinal\n"
                "cmp al, 61h\n" // >= 'a'
                "jnbe LatinLetterFinal\n"
                "jmp NEXT\n"

            "LatinLetterFinal:\n"
                "cmp al, 7Ah\n" // > 'z'
                "ja NEXT\n"

                "cmp ax, al\n"
                "sub ax, 30h\n"
                "push cx\n"
                "mov cx, 2\n"
                "rol dl, 4\n"
                "mov ax, 300fh\n"
                "and al,dl\n"
                "aaa\n"
                "aad 11h\n"
                "pop cx\n"
                "mov al, di\n"

                "jmp NEXT\n"

            "NEXT:\n"
                "stosb\n"
                "jmp BEGINCYCLE\n"

            "EXIT:\n"
                "stosb\n"
    );

    cout << "\n\nResult:" << endl;
    ofstream file_output("output.txt");
    int i = 0;
    while (output_string[i] != '\0')
    {
        cout << output_string[i];
        file_output << output_string[i];
        i++;
    }
    cout << endl;
    file_output << endl;
    file_output.close();

    getch();
    system("pause");

    return 0;
}

Using -S makes an asm source file instead of an executable.使用-S生成 asm 源文件而不是可执行文件。 It looks like you then tried to run that as a bash script, treating each line as a shell command.看起来您随后尝试将其作为bash脚本运行,将每一行视为 shell 命令。 Don't do that.不要那样做。

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

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