在ASM x86中实现C ++调用约定

时间:2015-04-19 11:24:47

标签: assembly x86 cdecl

我编写了一个简单的程序,使用ASM x86对字符串进行模糊处理。用户必须输入所需的字符串,然后为要进行模糊处理的字符串选择一个键(Ekey)。 (我知道您可以使用移位运算符或表格查找来模拟左右位旋转,我只是想让自己熟悉ASM)

我试图改变程序,以便它采用一个公认的C ++标准调用程序,例如Cdecl,用于将参数(EKey& tempChar)传递到子程序中-routine obfuscate

尽管进行了数小时的研究,但我还是没有成功,所以我来到这里是为了希望有一点经验的人能够提供一些指导。

以下是相关功能:

void obfusc_chars (int length, char EKey)
{   char tempChar;                      // char temporary store

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

obfuscate子程序:

// Inputs: register EAX = 32-bit address of Ekey,
//                  ECX = the character to be encrypted (in the low 8-bit field, CL).

// Output: register EAX = the encrypted value of the source character (in the low 8-bit field, AL).

       __asm {

     obfuscate: push    esi
                push    ecx
                mov     esi, eax
      and dword ptr     [esi], 0xFF
      ror byte ptr      [esi], 1
      ror byte ptr      [esi], 1
      add byte ptr      [esi], 0x01
                mov     ecx, [esi]
                pop     edx
            x17:ror     dl, 1
                dec     ecx
                jnz     x17
                mov     eax, edx
                add eax, 0x20
                xor eax, 0xAA
                pop esi
                ret
        }

提前感谢您花时间阅读。

0 个答案:

没有答案