![](/img/trans.png)
[英]Passing array as a parameter to an external ASM (Assembly) x86 and accessing it
[英]Assembly asm x86 Encryption/Decryption Program
我已经创建了这个程序已有一段时间了,我不知道如何解决解密程序,对您的帮助将不胜感激。
到目前为止,代码的加密部分可以正常工作。
#include <conio.h> // for kbhit
#include <iostream> // for cin >> and cout <<
#include <iomanip> // for fancy output
using namespace std;
#define MAXCHARS 6 // feel free to alter this, but 6 is the minimum
#define dollarchar '$' // string terminator
char OChars[MAXCHARS],
EChars[MAXCHARS],
DChars[MAXCHARS] = "Soon!"; // Global Original, Encrypted, Decrypted character strings
//----------------------------- C++ Functions ----------------------------------------------------------
void get_char (char& a_character)
{
cin >> a_character;
while (((a_character < '0') | (a_character > 'z')) && (a_character != dollarchar))
{ cout << "Alphanumeric characters only, please try again > ";
cin >> a_character;
}
}
//-------------------------------------------------------------------------------------------------------------
void get_original_chars (int& length)
{ char next_char;
length = 0;
get_char (next_char);
while ((length < MAXCHARS) && (next_char != dollarchar))
{
OChars [length++] = next_char;
get_char (next_char);
}
}
//---------------------------------------------------------------------------------------------------------------
//----------------- ENCRYPTION ROUTINES -------------------------------------------------------------------------
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 // Push last parameter first
lea eax,EKey
push temp_char
push eax
call encrypt3 // encrypt the character
mov temp_char, al
add esp, 8 // Clean parameters from stack
pop ecx // restore original register values from stack
pop eax //
}
EChars [i] = temp_char; // Store encrypted char in the encrypted chars array
}
return;
__asm {
encrypt3:
push ebp // Save the old base pointer value
mov ebp, esp // Set the new base pointer value
push edx // Save EDX to the first unused empty stack
push ecx //ecx register containing the temp_char is pushed to the stack
push eax // Save EAX to the first unused empty stack
mov edx, [ebp + 8] // Accessing the last value of ebp
movzx eax, byte ptr[eax] // Move 4 bytes to the EAX register
rol al, 1 // Rotate AL register one position to the left
rol al, 1 // Rotate AL register one position to the left
rol al, 1 // Rotate AL register one position to the left
mov edx, eax // Move 4 bytes from EAX into edx
pop eax // Restore original EAX
mov byte ptr[eax], dl //moves the Ekey value into the EAX register as an 8-bit value
pop ecx //stores the current letter being encrypted within the ECX register (it was pushed to the stack earlier in the assembly code).
xor ecx, edx //clears the EDX register of all values
mov eax, ecx // Move 4 bytes from ECX into EAX
ror al, 1 // Rotate AL register one position to the left
ror al, 1 // Rotate AL register one position to the left
ror al, 1 // Rotate AL register one position to the left
pop edx // Restore the value of EDX
pop ebx // Restore original EBX
mov esp, ebp // Dellocate local variables
pop ebp // Restore the original value of EBP
ret // Return EAX value
}
//--- End of Assembly code
}
// end of encrypt_chars function
//---------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------
//----------------- DECRYPTION ROUTINES -------------------------------------------------------------------------
//
void decrypt_chars(int length, char EKey)
{
return;
}
// end of decrypt_chars function
//---------------------------------------------------------------------------------------------------------------
当然,在放入汇编语言之前,请先用高级语言编写代码。
原因如下:
edx
寄存器 mov edx, [ebp + 8] // Accessing the last value of ebp
movzx eax, byte ptr[eax] // Move 4 bytes to the EAX register
rol al, 1 // Rotate AL register one position to the left
rol al, 1 // Rotate AL register one position to the left
rol al, 1 // Rotate AL register one position to the left
mov edx, eax // Move 4 bytes from EAX into edx
在上面的代码中,将[ebp + 8]
移到edx
。 然后,您稍后将eax
复制到edx
四个指令。 为什么要打扰这里的第一条指令?
使用汇编语言进行编码的常见原因之一是效率。 据说您可以比编译器更好地编码,或者可以使用比编译器更好的特殊指令。 如以下示例所示,您不需要:
rol al, 1 // Rotate AL register one position to the left
rol al, 1 // Rotate AL register one position to the left
rol al, 1 // Rotate AL register one position to the left
上面的代码应编码为rol al, 3
。
另外,您是否有理由使用al
register代替eax
?
清除edx
寄存器错误操作与注释不匹配。
xor ecx, edx //clears the EDX register of all values
edx
寄存器位于错误的一侧。
语句xor edx, edx
实际上清除edx
寄存器。
我建议您破坏汇编语言,并以高级语言重写功能。 首先使它工作。 检查编译器的汇编语言。 如果您可以比编译器更有效地编码算法,则可以这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.