分类目录

展开|收起

看你喜欢

(1) (1) (42) (1) (1) (1) (16) (2) (1) (1) (4) (1) (2) (7) (4) (1) (1) (1) (1) (3) (1) (5) (1) (1) (1) (1) (1) (2) (1) (4) (4) (3) (1) (1) (2) (1) (37) (2) (1) (5) (3) (1) (4) (1) (1) (11) (3) (1) (9) (3) (1) (23) (2) (1) (2) (1) (1) (1) (1)

最新精华

函数调用汇编分析【3】- 实例分析6(返回浮点值)

3.6 返回浮点值

(1)源程序

#include <stdio.h>
float add(int c , int d)
{
    float e;
    e=c+d;  
    return e;
}

void main()
{
    int a = 1;
    int b = 2;
    float c;

    c=add(a,b);

    while (1)
    {
    }
}

(2)汇编代码

1:    #include <stdio.h>
2:
3:    float add(int c , int d)
4:    {
00401020   push        ebp
00401021   mov         ebp,esp
00401023   sub         esp,48h
00401026   push        ebx
00401027   push        esi
00401028   push        edi
00401029   lea         edi,[ebp-48h]
0040102C   mov         ecx,12h
00401031   mov         eax,0CCCCCCCCh
00401036   rep stos    dword ptr [edi]
5:        float e;
6:        e=c+d;
00401038   mov         eax,dword ptr [ebp+8]    // 取c
0040103B   add         eax,dword ptr [ebp+0Ch]  // c+d
0040103E   mov         dword ptr [ebp-8],eax    // [ebp-8]=c+d
00401041   fild        dword ptr [ebp-8]    // [ebp-0Ch] -> st(0)
                                        fild指令用于装入整数到浮点寄存器st(0) 
00401044   fst         dword ptr [ebp-4]   // 保存浮点数st(0)到e
                                         注意:add返回后,main是用fstp指令
                                         从浮点寄存器st(0)获取返回值
7:        return e;
8:    }
00401047   pop         edi
00401048   pop         esi
00401049   pop         ebx
0040104A   mov         esp,ebp
0040104C   pop         ebp
0040104D   ret

9:
10:
11:   void main()
12:   {
00401060   push        ebp
00401061   mov         ebp,esp
00401063   sub         esp,4Ch
00401066   push        ebx
00401067   push        esi
00401068   push        edi
00401069   lea         edi,[ebp-4Ch]
0040106C   mov         ecx,13h
00401071   mov         eax,0CCCCCCCCh
00401076   rep stos    dword ptr [edi]
13:       int a = 1;
00401078   mov         dword ptr [ebp-4],1   // 栈中保存a
14:       int b = 2;
0040107F   mov         dword ptr [ebp-8],2   // 栈中保存b
15:       float c;                           // 单精度浮点数,占用32位
16:
17:       c=add(a,b);
00401086   mov         eax,dword ptr [ebp-8]
00401089   push        eax                 // b的值入栈作为第二个参数
0040108A   mov         ecx,dword ptr [ebp-4]
0040108D   push        ecx                // a的值入栈作为第一个参数
0040108E   call        @ILT+0(add) (00401005) // 调用add
00401093   add         esp,8               // 调整堆栈指针
00401096   fstp        dword ptr [ebp-0Ch]  // [ebp-0Ch] <- st(0);
                                         然后再执行一次浮点寄存器的出栈操作

18:
19:       while (1)
00401099   mov         edx,1
0040109E   test        edx,edx
004010A0   je          main+44h (004010a4)
20:       {
21:       }
004010A2   jmp         main+39h (00401099)
22:   }
004010A4   pop         edi
004010A5   pop         esi
004010A6   pop         ebx
004010A7   add         esp,4Ch
004010AA   cmp         ebp,esp
004010AC   call        __chkesp (00401180)
004010B1   mov         esp,ebp
004010B3   pop         ebp
004010B4   ret

(3)小结

浮点数类型的返回值放在ST(n)中,这里举的是最简单的单精度浮点类型的例子,更多信息参见x86汇编相关文档。

  打分:5.0/5 (共3人投票)
(浏览总计: 39 次)
Add Comment Register



发表回复

  

  

  

您可以使用这些HTML标签

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>