- 论坛徽章:
- 0
|
这段c代码
- int test_int_mod()
- {
- unsigned int mode_u;
- int mode_i;
-
- int x;
- unsigned int y;
- x=99;
- y=77;
- x=(x+1)%64;
- y=(y+1)%64;
-
- x=(x+1)%32;
- y=(y+1)%32;
- mode_u=32;
- mode_i=32;
- x=(x+1)%mode_i;
- y=(y+1)%mode_u;
- mode_u=11;
- mode_i=12;
- x=(x+1)%mode_i;
- y=(y+1)%mode_u;
-
- return(x);
- }
复制代码
在VC6.0下被编译成了如下:
- ?test_int_mod@@YAHXZ PROC NEAR ; test_int_mod
- push ebp
- mov ebp, esp
- sub esp, 16 ; 00000010H
- mov DWORD PTR _x$[ebp], 99 ; 00000063H
- mov DWORD PTR _y$[ebp], 77 ; 0000004dH
- mov eax, DWORD PTR _x$[ebp]
- add eax, 1
- and eax, -2147483585 ; 8000003fH
- jns SHORT $L69607
- dec eax
- or eax, -64 ; ffffffc0H
- inc eax
- mov DWORD PTR _x$[ebp], eax
- mov eax, DWORD PTR _y$[ebp]
- add eax, 1
- xor edx, edx
- mov ecx, 64 ; 00000040H
- div ecx
- mov DWORD PTR _y$[ebp], edx
- mov edx, DWORD PTR _x$[ebp]
- add edx, 1
- and edx, -2147483617 ; 8000001fH
- jns SHORT $L69608
- dec edx
- or edx, -32 ; ffffffe0H
- inc edx
- mov DWORD PTR _x$[ebp], edx
- mov eax, DWORD PTR _y$[ebp]
- add eax, 1
- xor edx, edx
- mov ecx, 32 ; 00000020H
- div ecx
- mov DWORD PTR _y$[ebp], edx
- mov DWORD PTR _mode_u$[ebp], 32 ; 00000020H
- mov DWORD PTR _mode_i$[ebp], 32 ; 00000020H
- mov eax, DWORD PTR _x$[ebp]
- add eax, 1
- cdq
- idiv DWORD PTR _mode_i$[ebp]
- mov DWORD PTR _x$[ebp], edx
- mov eax, DWORD PTR _y$[ebp]
- add eax, 1
- xor edx, edx
- div DWORD PTR _mode_u$[ebp]
- mov DWORD PTR _y$[ebp], edx
- mov DWORD PTR _mode_u$[ebp], 11 ; 0000000bH
- mov DWORD PTR _mode_i$[ebp], 12 ; 0000000cH
- mov eax, DWORD PTR _x$[ebp]
- add eax, 1
- cdq
- idiv DWORD PTR _mode_i$[ebp]
- mov DWORD PTR _x$[ebp], edx
- mov eax, DWORD PTR _y$[ebp]
- add eax, 1
- xor edx, edx
- div DWORD PTR _mode_u$[ebp]
- mov DWORD PTR _y$[ebp], edx
- mov eax, DWORD PTR _x$[ebp]
- mov esp, ebp
- pop ebp
- ret 0
- ?test_int_mod@@YAHXZ ENDP ; test_int_mod
复制代码
其中不能针对mode_u变量的值做优化(gcc也类似问题),但对大家自己的特定编译器或虚拟机解释器在编译时则可考虑加入判断mode_u是不是2的次幂来做不同的处理的代码.
但对于边解释边运行的机制来说:这样一来判断能力mode_u是不是2的次幂 又要多出代码耗时了.
因此只能考虑编译型或伪编译性执行采用这样的建议. |
|