Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

assembly - 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
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...