gpt4 book ai didi

c++ - 运行时检查失败 #0。不知道如何使用 CDECL

转载 作者:行者123 更新时间:2023-11-28 04:23:22 25 4
gpt4 key购买 nike

有人要求我更改一些 __asm 代码,以便它实现 C++ 调用约定。我试过使用 cdecl 但我一直收到此错误

Run-Time Check Failure #0: The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

该代码用于使用 for 循环一次一个字符地加密字符串。函数 encrypt_1 对当时正在通过 for 循环的字符进行编码。

我尝试通过为 for 循环和函数的基指针 (ebp) 和堆栈指针 (esp) 分配不同的值来使用 cdecl但我不断收到运行时检查失败#0,我所做的每一次调整都会导致程序中断。这几天我一直在强调这个问题,任何人都可以给我提示我可能哪里出错了吗?

正文:

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

for (int i = 0; i < length; i++) // Encrypt characters one at a time
{
temp_char = OChars[i]; // Get the next char from Original Chars array
// Each character in the string will be encrypted individually
__asm
{ //
push eax // Stores a backup of the location of eax to be used later
push ecx // Stores a backup of the charcter to be encrypted in the stack for later use
push edx // Stores a backup of the location for the encrypted character in the stack for later use

//
movzx ecx, temp_char // Places the character (8 bits) to be encrypted in the ecx register (32 bits) and replaces any extraneous bits with 0 as they are not being used.
lea eax, EKey // places the Ekey in the eax register for later use. Registers are fast than calling variables?

push ebp
mov ebp, esp
// sub esp, 4

push ecx // Parameter for encrypt1. - Temporary Character
push eax // Parameter for encrypt1. - Address for the key.

call encrypt_1 // Begins the Encryption Function
mov temp_char, dl // Stores the encrypted character in the temp_char variable to be reconstruced later

add esp, 8

pop eax // Restoring eax to previous location before parameter call
pop ecx // Restores Temporary Character location before parameter call


pop edx // Restores the edx register to its original value, ready for the next character
pop ecx // Restores the ecx register to its original value, ready for the next character
pop eax // Restores the eax register to its original value, ready for the next character
mov esp, ebp
pop ebp
// ret
}
EChars[i] = temp_char; // Store encrypted char in the Encrypted Chars array
}
return;

函数:

  __asm
{
encrypt_1:
push ebp //Saves the present value of ebp on the stack for later use
mov ebp, esp //Places the stack pointer where the base pointer is so variables can be stored
mov ecx, [ebp +08h]
mov edx, [ebp +0Ch]
push ecx // Stores the original character on the top of the stack
movzx ecx, byte ptr[eax] // Moves the Ekey (8-bit) into the ecx register (32-bit), changing all extraneous bits to 0 so it can be rotated
add cl, 0x01 // Adds 1 to Ekey
rol cl, 1 // Rotates the binary value of Ekey to the left 6 times for further security
rol cl, 1 // Could have just used "rol cl, 6"
rol cl, 1
rol cl, 1
rol cl, 1
rol cl, 1
mov edx, ecx // Move the encrypted Ekey to edx, freeing the ecx register for the original character to be used later.
mov byte ptr[eax], dl // Move byte pointer to the left side of eax
pop ecx // Retrieves the original character to be encrypted
x1 : rol cl, 1 // Rotate the original character one place to the left , encrypting it
dec edx // Decrease the rotated Ekey by one, acting as a counter to continue decrypting the original character
jnz x1 // Jump back to x1 unless the Ekey = 0, which dictates that the encryption is complete
add ecx, 0x20 // Add 32 bits to the encrypted character, filling the register
mov edx, ecx // Place the encrypted character in the edx register, ready to be returned
mov esp, ebp
pop ebp
//add esp, 8
ret // Return the encrypted character
}

//--- End of Assembly code
}

附言。抱歉,有些乱七八糟的注释行,我一直在尝试各种方法来让它工作。

最佳答案

没有理由用汇编编写调用。您应该将调用代码更改为:

    EChars[i] = encrypt_1(&EKey, OChars[i]);

如果出于某种原因你觉得你必须用汇编写它,那么你可以写:

    temp_char = OChars[i];
__asm
{
movzx ecx, temp_char
lea eax, EKey
push ecx // Second param: character to encrypt
push eax // First param: address of EKey.
call encrypt_1
mov temp_char, al
}
EChars[i] = temp_char;

函数:

  __asm
{
encrypt_1:
push ebp
mov ebp, esp
mov eax, [ebp+08h] // first param: address of EKey
mov dl, [ebp+0Ch] // second param: character to encrypt
mov cl, byte ptr [eax] // get value of EKey
add cl, 0x01
rol cl, 6
mov byte ptr [eax], cl // store updated value of EKey
rol dl, cl
add dl, 0x20
movzx eax, dl // Return encrypted character in eax
mov esp, ebp
pop ebp
ret
}

除了简化之外,我唯一修复的是:
1. 使用eax而不在函数内加载它。
2.函数参数顺序颠倒
3. 根本没有使用函数的 EKey 参数。

关于c++ - 运行时检查失败 #0。不知道如何使用 CDECL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54961926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com