我正在尝试实现从程序的c ++部分读取字符串,用内联汇编对其进行处理(将所有拉丁符号通过ASCII表中的数字转换为它们的HEX表示形式)并使用c ++代码打印的c ++程序。我在编译它时遇到问题,我找到了可以正确执行的方法,但我无法实现它。
我尝试使用以下方法进行编译:
gcc -S -masm=intel code.cpp -o code
但是当我尝试实现它时,会有很多这样的错误:
./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
这是我程序的代码:
#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;
}
-S
会生成一个asm源文件,而不是可执行文件。看起来您好像然后尝试将其作为bash
脚本运行,将每一行都视为shell命令。不要那样做。