c++ - 如何在内联汇编c ++中使用字符串矩阵
问题描述
我的任务是使用内联 MASM 从西里尔字母音译为拉丁语。我编写了以下代码,现在它只能处理简单的声音,如“a”、“b”等。
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <clocale>
#include <Windows.h>
using namespace std;
int main()
{
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
char map[33][4] = { "a",
"b"
, "v"
, "g"
, "d"
, "e"
, "z"
, "z"
, "i"
, "i"
, "k"
, "l"
, "m"
, "n"
, "o"
, "p"
, "r"
, "s"
, "t"
, "u"
, "f"
, "h"
, "c"
, "tch"
, "sh"
, "sch"
, ""
, "yy"
, ""
, "ae"
, "yu"
, "ya"
};
char str_out[160];
char str_in[80];
cin.getline(str_in, 80);
_asm
{
lea esi, str_in;
lea edi, str_out;
Process:
lods
cmp al, '\0';
je End_String;
cmp al, 223;
jb Write_Symbol;
// russian а stands for == 224
translit:
mov ebx, esi;
mov ecx, edi;
xor edi, edi;
lea esi, map;
movzx edi, al;
sub edi, 224
mov al, [esi + edi*4]; // **loads only first character**
mov esi, ebx;
mov edi, ecx;
jmp write_symbol;
Write_Symbol:
stos;
jmp Process;
End_String:
stos;
}
cout << str_out << endl;
system("pause");
return 0;
}
但是当我需要处理复杂的声音时,问题就开始了'不明白怎么做,如何加载这样一个字符串的地址并调用系统调用。
解决方案
所以,感谢@Peter Cordes,我解决了我的任务:
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <clocale>
#include <Windows.h>
using namespace std;
char map[33][8] = {
"a...",
"b..."
, "v..."
, "g..."
, "d..."
, "e..."
, "z..."
, "z..."
, "i..."
, "i..."
, "k..."
, "l..."
, "m..."
, "n..."
, "o..."
, "p..."
, "r..."
, "s..."
, "t..."
, "u..."
, "f..."
, "h..."
, "c..."
, "tch."
, "sh.."
, "sch."
, "...."
, "yy.."
, "...."
, "ae.."
, "yu.."
, "ya.."
};
int main()
{
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
cout << &map << "\n";
char str_out[160];
char str_in[80];
cin.getline(str_in, 80);
int len = 0;
_asm
{
lea esi, str_in;
lea edi, str_out;
Process:
xor eax, eax;
lodsb
cmp al, '\0';
je End_String;
cmp al, 223;
jb Write_Symbol;
// я == 255
translit:
mov ebx, esi;
mov ecx, edi;
xor edi, edi;
lea esi, map;
movzx edi, al;
sub edi, 224;
mov eax, dword ptr [esi + edi*8];
mov esi, ebx;
mov edi, ecx;
add len, 4;
stosd;
jmp Process;
Write_Symbol:
add len, 1;
stosb;
jmp Process;
End_String:
stosb;
}
str_out[len] = '\0';
for (int i = 0; i < len; i++) {
if (str_out[i] != '.') {
cout << str_out[i];
}
}
cout << "\n";
system("pause");
return 0;
}
推荐阅读
- c - 尝试将数组从主内存复制到共享内存的段错误
- arrays - 用 { } 初始化数组
- python - 如何使用“重复”索引旋转 pd 数据框?
- c# - 2 个具有相同名称和随机 PID 的进程 - 我如何知道我需要哪一个?
- python - 我不知道如何从输出随机 numpy 中删除 ['']
- python - 如何检测嵌入 QWidget.createWindowContainer 的外部窗口何时自行关闭?
- php - 使用 Sanctum 和 Vue SPA 的 Laravel 7 策略不起作用(或未注册)
- java - 无法将 int 字段 ch.bbw.apimashup.entity.Request.id 设置为 ch.bbw.apimashup.entity.Request
- c++ - 文件修改时间被后台缓存刷新覆盖
- python - 无法从“sklearn.utils.fixes”导入名称“延迟”