2015-04-29 31 views
-2

我正在嘗試將過程encrypt_chars轉換爲彙編程序。我在for循環中已經有一些x86程序集,但是我試圖將剩下的程序轉換爲彙編程序。我會很感激,如果你能幫助我如何將C++中的循環轉換爲彙編器?

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 
    } 
+13

將C++轉換爲彙編程序的最佳方式是使用C++編譯器。 –

+0

好吧,我目前正在使用visual studio。有關如何這樣做的任何建議? – Cube3

+0

@MikeVine我在問另外一個問題。我在問如何將for循環轉換爲彙編程序。 – Cube3

回答

0

接下來是爲「爲」語句轉換成彙編,註冊EDI作爲控制變量「i」,如EDI是被改變「的代碼encrypt21「,只需將其推入並在」call encrypt21「後面彈出即可保留 - 恢復其值。我通過「len」更改了參數「length」,因爲名稱給我帶來了問題:

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

如果我被評爲15,我會投你一票!非常感謝你。我只是在這種情況下徘徊,爲什麼變量我會被EDI控制? – Cube3

+1

可以使用「我」,但EDI更「彙編」。 –

+0

好的,謝謝 – Cube3