汇编中除法和摸的问题!

来源:百度知道 编辑:UC知道 时间:2024/05/26 15:15:10
VS2005取模,反汇编后出来的算法 不是很了解?! 哪位大大能给解释???
=============================
#include <stdio.h>
#define COUNT 60
int main(void)
{
int val;
int miao;
int shi;
scanf("%d",&val);
miao=val%COUNT;
//shi=val/COUNT;
printf("result is %d",miao);
return 0;
}
=============================================
00401000 push ecx
00401001 lea eax,[esp]
00401004 push eax
00401005 push offset string "%d" (4020E4h)
0040100A call dword ptr [__imp__scanf (4020A0h)]
00401010 mov ecx,dword ptr [esp+8]
00401014 mov eax,88888889h
00401019 imul ecx
0040101B add edx,ecx
0040101D sar edx,5
00401020 mov eax,edx
00401022 shr eax,1Fh
00401025 add eax,edx
00401027 mov edx,eax
00401029 shl edx

=============================
#include <stdio.h>
#define COUNT 60
int main(void)
{
int val;
int miao;
int shi;
scanf("%d",&val);
miao=val%COUNT;
//shi=val/COUNT;
printf("result is %d",miao);
return 0;
}
=============================================
00401000 push ecx ;保存ecx
00401001 lea eax,[esp] ;取val变量的地址到eax中
00401004 push eax ;给scanf传val变量地址参数
00401005 push offset string "%d" (4020E4h);给scanf传字符串参数
0040100A call dword ptr [__imp__scanf (4020A0h)];调用scanf
00401010 mov ecx,dword ptr [esp+8] ;返回值放入ecx中,
;即把输入的值赋给ecx

00401014 mov eax,88888889h ;被乘数,注意这是一个负数!
00401019 imul ecx ;注意整数乘法!为方便理解算法,假设输入的是60,则:
0040101B add edx,ecx ;结果高位,即-28加上乘数,edx=32
0040101D sar edx,5 ;右移5位,edx=1.注意sar这个指令!
00401020 mov eax,edx
00401022 shr eax,1Fh ;右移31位,eax=0,判断最高位
00401025 add eax,edx ;eax=1
0040102