我正在尝试解决来自https://challenges.re/2/ 的逆向工程问题——这是挑战 2,目标是尽可能高水平地理解代码的作用。
<f>:
0: mov eax,DWORD PTR [esp+0x4]
4: bswap eax
6: mov edx,eax
8: and eax,0xf0f0f0f
d: and edx,0xf0f0f0f0
13: shr edx,0x4
16: shl eax,0x4
19: or eax,edx
1b: mov edx,eax
1d: and eax,0x33333333
22: and edx,0xcccccccc
28: shr edx,0x2
2b: shl eax,0x2
2e: or eax,edx
30: mov edx,eax
32: and eax,0x55555555
37: and edx,0xaaaaaaaa
3d: add eax,eax
3f: shr edx,1
41: or eax,edx
43: ret
这是我的解决方法,在评论中。因为代码没有给我一个初始起点,我假设初始值分配是12 34 56 78
:
mov eax,DWORD PTR [esp+0x4] ; eax < 12 34 56 78 (305419896d)
bswap eax ; eax < 78 56 34 12
mov edx,eax ; eax = edx = 78 56 34 12
and eax,0xf0f0f0f ; eax = 02 04 06 08
and edx,0xf0f0f0f0 ; edx = 70 50 30 10
shr edx,0x4 ; edx = 07 05 03 01
shl eax,0x4 ; eax = 20 40 60 80
or eax,edx ; eax = 27 45 63 81
mov edx,eax ; edx = eax = 27 45 63 81
and eax,0x33333333 ; eax = 23 01 23 01
and edx,0xcccccccc ; edx = 04 44 40 80
shr edx,0x2 ; edx = 01 11 10 20
shl eax,0x2 ; eax = 8C 04 8C 04
or eax,edx ; eax = 8D 15 9C 24
mov edx,eax ; eax = edx = 8D 15 9C 24
and eax,0x55555555 ; eax = 05 15 14 04
and edx,0xaaaaaaaa ; edx = 88 00 88 20
add eax,eax ; eax = 8D 15 9C 24
shr edx,1 ; edx = 44 00 44 10
or eax,edx ; eax = CD 15 D6 34
ret ; return eax > CD 15 D6 34 (3440760372d)
我仍然不明白的是大局 - 似乎是一些没有目的的随机数学运算,可能我错了。是什么赋予了?