【发布时间】:2020-08-29 20:02:18
【问题描述】:
我只是想知道如何在汇编程序中生成随机数,我发现来自俄罗斯堆栈溢出的问题,人们问的不是如何在汇编程序中生成随机数,而是如何使用_asm{} 在 c 代码中实现它。
The answer 发布到他的问题让我感到惊讶(翻译成英文):
char r[]="!!!!!!!!!!!№№№№№№№№№№№;;;;;;;;;;;;;;;;;;;;;;;;;55555555555555666666666666666666666666666777777777777777777777777777777777777777777777777777777777777"; // String, which length should be calculated
main()
{
static unsigned long (__cdecl *lenstr)(char*); // Pointer to function declaration. The method for passing parameters must be defined explicitly - it is different in different compilers
static int i=0;
if(!i)
{
static char s[]={
0x5a,
//pop %%edx
0x5f,
//pop %%edi
0xfc,
//cld
0x31,0xc9,
//xor %%ecx,%%ecx
0x31,0xc0,
//xor %%eax,%%eax
0x49,
//dec %%ecx
0xf2,0xae,
//repne scasв
0xf7,0xd1,
//not %%ecx
0x49,
//dec %%ecx
0x91,
//xchg %%eax,%%ecx
0x52,
//push %%edx
0xc3
//ret
}; // Array with assembler code
lenstr=(unsigned long ( __cdecl *)(char*))&s; // Linking function pointer to to that array
i=1;
}
printf("%s%c%d%c%s\n","String length",' ',lenstr(r),' ',"symbols");
}
两个问题:
- 将汇编代码作为强制转换的
char数组放入函数指针的机会存在多长时间?为什么要开发它? - 我不明白:计算字符串长度是一种智能的随机数生成方法,还是只是机器代码转换为指针的示例?
【问题讨论】:
-
这不适用于大多数 x86-64 操作系统;非常量静态数据应该在没有 exec 权限的读写页面中。您需要额外的技巧,例如适用于 Windows 的 VirtualProtect。此外,这看起来像是一个缓慢但很小的 strlen,而不是 PRNG 步骤。