Jordan Barber Jordan Barber - 22 days ago 5
C++ Question

How to convert for loop in c++ to assembler?

I am trying to convert the procedure encrypt_chars to assembler. I already have some x86 in assembly in the for loop, however I am trying to convert the remainder of the procedure into assembler. I'd be appreciative if you could help me

void encrypt_chars (int length, char EKey)
{ char temp_char; // char temporary store

for (int i = 0; i < length; i++) // encrypt characters one at a time
{
temp_char = OChars [i]; //
__asm { //
push eax // save register values on stack to be safe
push ecx //
//
movzx ecx,temp_char // set up registers (Nb this isn't StdCall or Cdecl)
lea eax,EKey //
call encrypt12 // encrypt the character
mov temp_char,al //
//
pop ecx // restore original register values from stack
pop eax //
}
EChars [i] = temp_char; // Store encrypted char in the encrypted chars array
}

Answer

Next is the code for the "for" statement converted into assembler, register EDI is used as the control variable "i", if EDI is been changed by "encrypt21", just push it before and pop it after "call encrypt21" to preserve-restore its value. I changed the parameter "length" by "len" because the name gave me problems :

void encrypt_chars(int len, char EKey)
{   char temp_char;

__asm {   mov  edi, 0           ;FOR ( EDI = 0;

        fori:

        ;GET CURRENT CHAR.

          mov  al, OChars[edi]
          mov  temp_char, al

        ;ENCRYPT CURRENT CHAR.

          push   eax              // save register values on stack to be safe
          push   ecx
          movzx  ecx,temp_char  // set up registers (Nb this isn't StdCall or Cdecl)
          lea    eax,EKey
          call   encrypt12              // encrypt the character
          mov    temp_char,al
          pop    ecx        // restore original register values from stack
          pop    eax 

       ;STORE ENCRYPTED CHAR.

          mov  al, temp_char
          mov  EChars[ edi ], al

       ;FOR STATEMENT : FOR ( EDI = 0; EDI < LEN, EDI++ )

          inc  edi              ;EDI++.
          cmp  edi, len
          jb   fori             ;IF ( EDI < LEN ) JUMP.
      }
return;
}