首页 > 解决方案 > 如何在内联汇编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;
}

但是当我需要处理复杂的声音时,问题就开始了'不明白怎么做,如何加载这样一个字符串的地址并调用系统调用。

标签: c++assemblymasminline-assembly

解决方案


所以,感谢@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;
}

推荐阅读