I'm trying to get the assembly code version of this action in C++. I made the following code.
#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] << "";
}
}
I have everything practically done for the asm. But, I still cannot get this section to work or translate it.
;How to do the following in asm?
cout << consonant[randCons] << "";
The following is what I have so far: !!WARNING!! code is bad!
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
The characters are one byte each, so just offset the base address of the array with your index.
It seems like you have your index in eax
(the return value from RandomRange
), so you should be able to do e.g.:
mov bl, [cons + eax] ; read the character at index eax in cons, and place it in register bl