首页
社区
课程
招聘
[求助]一个函数的汇编代码
发表于: 2010-6-6 13:39 4497

[求助]一个函数的汇编代码

2010-6-6 13:39
4497
枫痕(593637020)  13:19:03
__inline int calc(char str[],int n)
{
        int i,result=0;

        for(i=0;i<n;i++)
                result+=(str[i]|0x20);
        return result-n*('a'-1)-100;
}

函数是内联的
vs2008的Ox编译选项编译的:
.text:004010FD                 sub     esi, ebx        ; 减后为参数n
.text:004010FF                 xor     ecx, ecx
.text:00401101                 xor     edi, edi
.text:00401103                 xor     ebp, ebp
.text:00401105                 xor     eax, eax
.text:00401107                 cmp     esi, 2
.text:0040110A                 jl      short loc_401135
.text:0040110C                 mov     edx, [esp+44h+pBuf]
.text:00401110                 lea     edx, [ebx+edx+1] ;这里后,edx为参数 str
.text:00401114
.text:00401114 loc_401114:                             ; CODE XREF: sub_401060+CFj
.text:00401114                 movsx   ebx, byte ptr [edx+eax-1]
.text:00401119                 or      ebx, 20h
.text:0040111C                 add     ecx, ebx
.text:0040111E                 movsx   ebx, byte ptr [edx+eax]
.text:00401122                 or      ebx, 20h
.text:00401125                 add     edi, ebx
.text:00401127                 add     eax, 2
.text:0040112A                 lea     ebx, [esi-1]
.text:0040112D                 cmp     eax, ebx
.text:0040112F                 jl      short loc_401114
.text:00401131                 mov     ebx, [esp+44h+var_28]
.text:00401135
.text:00401135 loc_401135:                             ; CODE XREF: sub_401060+AAj
.text:00401135                 cmp     eax, esi
.text:00401137                 jge     short loc_401146
.text:00401139                 mov     edx, [esp+44h+pBuf]
.text:0040113D                 add     eax, ebx
.text:0040113F                 movsx   ebp, byte ptr [eax+edx]
.text:00401143                 or      ebp, 20h
.text:00401146
.text:00401146 loc_401146:                             ; CODE XREF: sub_401060+D7j
.text:00401146                 lea     eax, [esi+esi*2]
.text:00401149                 shl     eax, 5
.text:0040114C                 sub     ecx, eax
.text:0040114E                 add     ecx, edi
.text:00401150                 lea     ebp, [ecx+ebp-64h]

代码给全在最后吧~~
先说问题。

不知道为什么,编译器要把那个递增值改成2,然后,增加了好多额外的代码~
这样会执行更快点么?

void start(FILE *fp)
{
        char *pBuf=(char *)malloc(MAX_BUF);
        char szWord[MAX_WORD_LEN];
        int nBytesRead;
        int nStart;
        int nCurrent;
        //int bResetFlag;//重置nStart的标志 为1重置
        int bStartFlag;//nStart是否开始计数标志为1开始计数

        int nRet;
       
        while(nBytesRead=fread(pBuf,1,MAX_BUF,fp))
        {       
                bStartFlag=0;
                //bResetFlag=1
                nCurrent=0;
                for(;nCurrent<nBytesRead;nCurrent++)
                {
                        if((pBuf[nCurrent]|0x20)>='a'&&(pBuf[nCurrent]|0x20)<='z')
                        {
                                if(!bStartFlag)
                                {
                                        bStartFlag=1;
                                        nStart=nCurrent;
                                }
                                continue;
                        }
                        else if(pBuf[nCurrent]>127) //判断是否为扩展的多字节字符
                        {
                                nCurrent++;//配置循环的结构的++ 加2
                        }                               
                       
                        if(bStartFlag)
                        {
                                nRet=calc((char *)((unsigned int)pBuf+nStart),nCurrent-nStart);
                               
                                memcpy(szWord,(void *)((unsigned int)pBuf+nStart),nCurrent-nStart);
                                szWord[nCurrent-nStart]='\0';

                                if(nRet<0)
                                {       
                                        printf("%s less than 100\n",szWord);
                                }
                                else if(nRet==0)
                                        printf("%s equal 100\n",szWord);
                                else
                                        printf("%s big than 100\n",szWord);

                                bStartFlag=0;
                        }
                }

                if(bStartFlag)
                {
                        fseek(fp,nStart-MAX_BUF,SEEK_CUR); //单词截断

                }

        }

        free(pBuf);
}

__inline int calc(char str[],int n)
{
        int i,result=0;

        for(i=0;i<n;i++)
                result+=(str[i]|0x20);
        return result-n*('a'-1)-100;
}

.text:00401060 ; int __fastcall sub_401060(FILE *File)
.text:00401060 sub_401060      proc near               ; CODE XREF: _main+4Cp
.text:00401060
.text:00401060 pBuf            = dword ptr -34h
.text:00401060 nBytesRead      = dword ptr -30h
.text:00401060 var_2C          = dword ptr -2Ch
.text:00401060 var_28          = dword ptr -28h
.text:00401060 File            = dword ptr -24h
.text:00401060 Dst             = byte ptr -20h
.text:00401060 var_4           = dword ptr -4
.text:00401060
.text:00401060                 sub     esp, 34h
.text:00401063                 mov     eax, stack_seek
.text:00401068                 xor     eax, esp
.text:0040106A                 mov     [esp+34h+var_4], eax
.text:0040106E                 push    ebp
.text:0040106F                 push    esi
.text:00401070                 push    edi
.text:00401071                 mov     esi, ecx        ; use with out init
.text:00401073                 push    6400000h        ; Size
.text:00401078                 mov     [esp+44h+File], esi
.text:0040107C                 call    ds:malloc
.text:00401082                 push    esi             ; File
.text:00401083                 push    6400000h        ; Count
.text:00401088                 mov     edi, eax
.text:0040108A                 push    1               ; ElementSize
.text:0040108C                 push    edi             ; DstBuf
.text:0040108D                 mov     [esp+54h+pBuf], edi
.text:00401091                 call    ds:fread
.text:00401097                 mov     ebp, eax
.text:00401099                 add     esp, 14h
.text:0040109C                 mov     [esp+40h+nBytesRead], ebp
.text:004010A0                 test    ebp, ebp
.text:004010A2                 jz      loc_4011ED
.text:004010A8                 push    ebx
.text:004010A9                 mov     ebx, [esp+44h+var_28]
.text:004010AD                 jmp     short loc_4010B4
.text:004010AD ; ---------------------------------------------------------------------------
.text:004010AF                 align 10h
.text:004010B0
.text:004010B0 loc_4010B0:                             ; CODE XREF: sub_401060+186j
.text:004010B0                 mov     ebp, [esp+44h+nBytesRead]
.text:004010B4
.text:004010B4 loc_4010B4:                             ; CODE XREF: sub_401060+4Dj
.text:004010B4                 xor     esi, esi
.text:004010B6                 xor     edx, edx
.text:004010B8                 mov     [esp+44h+var_2C], esi
.text:004010BC                 test    ebp, ebp
.text:004010BE                 jle     loc_4011CA
.text:004010C4
.text:004010C4 loc_4010C4:                             ; CODE XREF: sub_401060+149j
.text:004010C4                 mov     cl, [esi+edi]
.text:004010C7                 mov     al, cl
.text:004010C9                 or      al, 20h
.text:004010CB                 cmp     al, 61h
.text:004010CD                 jl      short loc_4010EB
.text:004010CF                 cmp     al, 7Ah
.text:004010D1                 jg      short loc_4010EB
.text:004010D3                 test    edx, edx
.text:004010D5                 jnz     loc_4011A2
.text:004010DB                 mov     ebx, esi
.text:004010DD                 mov     edx, 1
.text:004010E2                 mov     [esp+44h+var_28], ebx
.text:004010E6                 jmp     loc_4011A2
.text:004010EB ; ---------------------------------------------------------------------------
.text:004010EB
.text:004010EB loc_4010EB:                             ; CODE XREF: sub_401060+6Dj
.text:004010EB                                         ; sub_401060+71j
.text:004010EB                 cmp     cl, 7Fh
.text:004010EE                 jle     short loc_4010F5
.text:004010F0                 inc     esi
.text:004010F1                 mov     [esp+44h+var_2C], esi
.text:004010F5
.text:004010F5 loc_4010F5:                             ; CODE XREF: sub_401060+8Ej
.text:004010F5                 test    edx, edx
.text:004010F7                 jz      loc_4011A2
.text:004010FD                 sub     esi, ebx        ; lenght
.text:004010FF                 xor     ecx, ecx
.text:00401101                 xor     edi, edi
.text:00401103                 xor     ebp, ebp
.text:00401105                 xor     eax, eax
.text:00401107                 cmp     esi, 2
.text:0040110A                 jl      short loc_401135
.text:0040110C                 mov     edx, [esp+44h+pBuf]
.text:00401110                 lea     edx, [ebx+edx+1]
.text:00401114
.text:00401114 loc_401114:                             ; CODE XREF: sub_401060+CFj
.text:00401114                 movsx   ebx, byte ptr [edx+eax-1] ; 这个-1和上面那个加1消掉了
.text:00401119                 or      ebx, 20h
.text:0040111C                 add     ecx, ebx
.text:0040111E                 movsx   ebx, byte ptr [edx+eax]
.text:00401122                 or      ebx, 20h
.text:00401125                 add     edi, ebx
.text:00401127                 add     eax, 2
.text:0040112A                 lea     ebx, [esi-1]
.text:0040112D                 cmp     eax, ebx
.text:0040112F                 jl      short loc_401114 ; 这个-1和上面那个加1消掉了
.text:00401131                 mov     ebx, [esp+44h+var_28]
.text:00401135
.text:00401135 loc_401135:                             ; CODE XREF: sub_401060+AAj
.text:00401135                 cmp     eax, esi
.text:00401137                 jge     short loc_401146
.text:00401139                 mov     edx, [esp+44h+pBuf]
.text:0040113D                 add     eax, ebx
.text:0040113F                 movsx   ebp, byte ptr [eax+edx]
.text:00401143                 or      ebp, 20h
.text:00401146
.text:00401146 loc_401146:                             ; CODE XREF: sub_401060+D7j
.text:00401146                 lea     eax, [esi+esi*2]
.text:00401149                 shl     eax, 5
.text:0040114C                 sub     ecx, eax
.text:0040114E                 add     ecx, edi
.text:00401150                 lea     ebp, [ecx+ebp-64h]
.text:00401154                 mov     ecx, [esp+44h+pBuf]
.text:00401158                 push    esi             ; Size
.text:00401159                 lea     edx, [ebx+ecx]
.text:0040115C                 push    edx             ; Src
.text:0040115D                 lea     eax, [esp+4Ch+Dst]
.text:00401161                 push    eax             ; Dst
.text:00401162                 call    memcpy
.text:00401167                 add     esp, 0Ch
.text:0040116A                 mov     [esp+esi+44h+Dst], 0
.text:0040116F                 test    ebp, ebp
.text:00401171                 jl      short loc_401194
.text:00401173                 jnz     short loc_401181
.text:00401175                 lea     ecx, [esp+44h+Dst]
.text:00401179                 push    ecx
.text:0040117A                 push    offset aSEqual100 ; "%s equal 100\n"
.text:0040117F                 jmp     short loc_40118B
.text:00401181 ; ---------------------------------------------------------------------------
.text:00401181
.text:00401181 loc_401181:                             ; CODE XREF: sub_401060+113j
.text:00401181                 lea     edx, [esp+44h+Dst]
.text:00401185                 push    edx
.text:00401186                 push    offset aSBigThan100 ; "%s big than 100\n"
.text:0040118B
.text:0040118B loc_40118B:                             ; CODE XREF: sub_401060+11Fj
.text:0040118B                 call    ds:printf
.text:00401191                 add     esp, 8
.text:00401194
.text:00401194 loc_401194:                             ; CODE XREF: sub_401060+111j
.text:00401194                 mov     esi, [esp+44h+var_2C]
.text:00401198                 mov     ebp, [esp+44h+nBytesRead]
.text:0040119C                 mov     edi, [esp+44h+pBuf]
.text:004011A0                 xor     edx, edx
.text:004011A2
.text:004011A2 loc_4011A2:                             ; CODE XREF: sub_401060+75j
.text:004011A2                                         ; sub_401060+86j ...
.text:004011A2                 inc     esi
.text:004011A3                 cmp     esi, ebp
.text:004011A5                 mov     [esp+44h+var_2C], esi
.text:004011A9                 jl      loc_4010C4
.text:004011AF                 test    edx, edx
.text:004011B1                 jz      short loc_4011CA
.text:004011B3                 mov     ecx, [esp+44h+File]
.text:004011B7                 push    1               ; Origin
.text:004011B9                 lea     eax, [ebx-6400000h]
.text:004011BF                 push    eax             ; Offset
.text:004011C0                 push    ecx             ; File
.text:004011C1                 call    ds:fseek
.text:004011C7                 add     esp, 0Ch
.text:004011CA
.text:004011CA loc_4011CA:                             ; CODE XREF: sub_401060+5Ej
.text:004011CA                                         ; sub_401060+151j
.text:004011CA                 mov     edx, [esp+44h+File]
.text:004011CE                 push    edx             ; File
.text:004011CF                 push    6400000h        ; Count
.text:004011D4                 push    1               ; ElementSize
.text:004011D6                 push    edi             ; DstBuf
.text:004011D7                 call    ds:fread
.text:004011DD                 add     esp, 10h
.text:004011E0                 mov     [esp+44h+nBytesRead], eax
.text:004011E4                 test    eax, eax
.text:004011E6                 jnz     loc_4010B0
.text:004011EC                 pop     ebx
.text:004011ED
.text:004011ED loc_4011ED:                             ; CODE XREF: sub_401060+42j
.text:004011ED                 push    edi             ; Memory
.text:004011EE                 call    ds:free
.text:004011F4                 mov     ecx, [esp+44h+var_4]
.text:004011F8                 add     esp, 4
.text:004011FB                 pop     edi
.text:004011FC                 pop     esi
.text:004011FD                 pop     ebp
.text:004011FE                 xor     ecx, esp
.text:00401200                 call    sub_401209
.text:00401205                 add     esp, 34h
.text:00401208                 retn
.text:00401208 sub_401060      endp

[课程]Linux pwn 探索篇!

收藏
免费 0
支持
分享
最新回复 (5)
雪    币: 55
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
函数是内联的, 是实现代码混淆的办法。你看去掉__inLine 这个关键子,就不会出现这个问题了吧。试一试·················
2010-6-6 13:54
0
雪    币: 23
活跃值: (20)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
帮你顶帮你顶
2010-6-6 18:52
0
雪    币: 724
活跃值: (81)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
4

...
.text:00401114 movsx ebx, byte ptr [edx+eax-1] ; 这个-1和上面那个加1消掉了
.text:00401119 or ebx, 20h
.text:0040111C add ecx, ebx
.text:0040111E movsx ebx, byte ptr [edx+eax]
.text:00401122 or ebx, 20h
.text:00401125 add edi, ebx
.text:00401127 add eax, 2
.text:0040112A lea ebx, [esi-1]
.text:0040112D cmp eax, ebx
.text:0040112F jl short loc_401114 ; 这个-1和上面那个加1消掉了
...


这应是一种优化,在指令执行过程中,内存访问占用的时间比较长,而对于现代CPU来说,内存访问和计算可以同时执行,也就是说0040111E处的内存访问和前面两个计算指令可以同时执行,这样可以节省时间,但这种优化不能跨分支。

另外,楼主的程序好象有错,

...
else if(pBuf[nCurrent]>127) //判断是否为扩展的多字节字符
{
nCurrent++;//配置循环的结构的++ 加2
}
...

pBuf[nCurrent]>127这个条件永不成立吧,从汇编代码中也可以证实这一点。
2010-6-6 20:11
0
雪    币: 4
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
[QUOTE=半道出家;819924]这应是一种优化,在指令执行过程中,内存访问占用的时间比较长,而对于现代CPU来说,内存访问和计算可以同时执行,也就是说0040111E处的内存访问和前面两个计算指令可以同时执行,这样可以节省时间,但这种优化不能跨分支。

另外,楼主的程序好象有错,

pBuf[nCurrent]>12...[/QUOTE]

是汉字就成立了
2010-6-7 11:29
0
雪    币: 724
活跃值: (81)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
6
是汉字也不成立
2010-6-7 14:46
0
游客
登录 | 注册 方可回帖
返回
//