c++ - 使用索引从字符串中选择单个字符?

标签 c++ assembly translate irvine32

我试图在 C++ 中获取此操作的汇编代码版本。我做了以下代码。

#include <iostream>
#include <string>
#include <cstdlib>
#include <random>
using namespace std;

void RandomizeData();

string vowel = "AEIOU";
string consonant = "BCDFGHJKLMNPQRSTVWXYZ";
int Matrixes = 0;
int Rows = 0;
int Characters = 0;
int test;

int main()
{
    // declare variables

    while (Matrixes < 4)
    {
        while (Rows < 4)
        {
            while (Characters < 4)
            {
                RandomizeData();
                ++Characters;
            }
            Characters = 0;
            Rows++;
            cout << "\n";
        }
        Rows = 0;
        Characters = 0;
        cout << "\n\n";
        ++Matrixes;
    }

    cin >> test;

    return 0;
}

void RandomizeData()
{
    int randVowel = (rand() % 5);
    int randCons = (rand() % 21);

    test = (rand() % 2);

    if (test == 1)
    {
        cout << consonant[randCons] << "";
    }
    else
    {
        cout << vowel[randVowel] << "";
    }   
}

我已经为 asm 完成了所有实际工作。但是,我仍然无法使这部分工作或翻译它。

        ;How to do the following in asm?
        cout << consonant[randCons] << "";

以下是我目前所拥有的: !!警告!!代码有问题!

INCLUDE Irvine32.inc

.386
.stack 4096
ExitProcess proto,dwExitCode:dword


    .data

        vowels  DB  "AEIOU"
        cons    DB  "BCDFGHJKLMNPQRSTVWXYZ", 0
        path    DWORD   0
        cool    BYTE    ?                                   ;determines vowel or cons

        ;Loop counters
        rows    DWORD   0
        matrixes    DWORD   0
        characters  DWORD   0

        ;Random variables
        rndVowel        DWORD   ?
        rndCons     DWORD   ?



    .code
        main PROC 
            STEP1:  cmp     matrixes, 4
                    jge     STEP4

            STEP2:  cmp     rows, 4
                    jge     STEP1
                    mov     characters, 0

            STEP3:  cmp     characters, 4
                    jge     STEP2
                    call    CharSelector                ;get & display character
                    inc     characters              
                    jmp     STEP3                       ;repeat STEP 3

            STEP4:  invoke  ExitProcess,0
        main ENDP




        CharSelector PROC
            call    Randomize                           ;seed
            mov     eax, 2
            call    RandomRange
            mov     path, eax                           ;mov result to path

            STEP1:  cmp     path, 1
                    mov     ecx, 0
                    jne     STEP2   

            STEP2:                                      ;block chooses vowel index                                  
                    mov     eax, 5
                    call    RandomRange
                    mov     rndVowel, eax


                    ;How to do the following in asm


                    call    WriteString


                    exit

            STEP3:                                      ;block chooses cons index
                    mov     eax, 21
                    call    RandomRange
                    mov     rndCons, eax
                    exit
        CharSelector ENDP


    end main

最佳答案

每个字符都是一个字节,所以只需用您的索引偏移数组的基址即可。

您的索引似乎在 eax 中(RandomRange 的返回值),因此您应该能够执行以下操作:

mov bl, [cons + eax]  ; read the character at index eax in cons, and place it in register bl

关于c++ - 使用索引从字符串中选择单个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33910648/

相关文章:

c++ - 函数参数名称的范围

assembly - 在内联汇编中更快实现的简单 C 函数的示例是什么?

c - 如何在程序集循环后转移控制权?

c - 如何将 C 语言转换为汇编语言,以便在 ATmega32U4(连接到 Teensy 2.0)上运行的程序

c++ - 调试版本找不到调试运行时 DLL

c++ - 是否可以在 OS X 上重新设置 QProgressBar 的样式?

assembly - 实模式汇编获取键盘输入

python - 简单语言翻译器

d3.js - 反转 d3.zoom 比例和翻译的顺序

c++ - 使用邻接矩阵计算概率