首页 > 解决方案 > 将 CDECL 调用约定采用到仅复制/粘贴当前参数的函数中

问题描述

好的,我的讲师编写了一个加密程序,他希望我实现一个 cdecl 调用约定。我了解堆栈、推送和弹出的基本概念,但发现在这里很难实现。

问题: 我如何在这个 CALLER/CALEE 函数中实现 CDECL?

下面来电

__asm
{                                   //
  push   eax                        // Push the eax register onto the call stack, it will preserve value when we pop the register
  push   ecx                        // Pushes the ecx value on stack, it currently contains the value of first character entered by the user
  push   edx                        // push the edx register onto the call stack, preserve value of edx register when we pop it.

  movzx  ecx, temp_char             // copy the value of variable temp_char(ascii of first character of user input) in ECX register, movzx is used to
                                    // specify that higher-order "unused" bits in the int value are zeroes.
  lea    eax, EKey                  // copy the value of variable EKey(which is the ascii of encryption key 'S') into the EAX register
  call   encrypt_18                 // run the encryption routine 18 which will encrypt the characters, one by one
  mov    temp_char, dl              // copy the contents of dl register which contains the encryped value of char into the temp_char variable

  pop    edx                        // return the value in edx  register
  pop    ecx                        // return the value in ecx  register
  pop    eax                        // return the value in eax  register
}
EChars[i] = temp_char;              // Store encrypted char in the Encrypted }

被叫

 __asm
  {
  encrypt_18:
          push  ecx          // pushes the ecx register on stack with the ascii value to be encrypted
          mov   esi, eax     // copies the value of EAX which contains the Encryption Key ascii value of 'S' character into ESI register
          xchg  eax, [eax]   // load char Ekey into the AL(8 bit register), replace old key with pointer to the new key
          and eax, 0x79      // 
          ror   al, 1        //  
          ror   al, 1       //  
          ror   al, 1       //  
          add   eax, 0x03   // These 5 instructions do the encryption, it adds rol(key & 0x79, 3) + 5 to the char,
                            // it also rotates it left by 1
          mov   edx, eax   // copies the value of EAX which is the new key into the EDX register
          pop   ecx        //  return the value of ECX 
          add   ecx, 0x02  // add 2 to the value in ECX which is currently the first char entered by user, it becomes 
                          //equal to a char that is value of original char + 2.
          add   ecx, edx  // adds the value of edx with the value of ecx
          ror   cl, 1     // shift the right hand side bit to the left side in the ECX register
          mov[esi], eax   // copy the content of EAX register into the ESI register address pointer
          mov   edx, ecx  // copy the value of ECX register into the EDX register 
          ret             // return back to the function

  }

标签: assemblyx86

解决方案


推荐阅读