回 帖 发 新 帖 刷新版面

主题:[原创]【C++提高】理解pascal参数传递顺序

理解pascal参数传递顺序
   

   用pascal声明函数后,编译时使用pascal参数传递顺序。该关键字对编译代码产生如下影响:
    1.参数按出现顺序,从左至右地进栈。
    2.被调用函数在返回调用前负责栈的平衡。
    例如下例:

    int pascal some_function(int first,int second,int third)
    {
       int i;
       i = first + second + third;
       return i;
    }

    void main()
    {
       int i = some_function(1,2,3);
    }

    main()中的函数调用产生下列代码:

    mov   ax,0001       ;push the first argument
    push  ax
    mov   ax,0002       ;push the second argument
    push  ax
    mov   ax,0003       ;push the last argument
    push  ax
    push  cs                 ;push cs for a FAR call
    call  some_function
    add   sp,006          ;rebalance the stack
    ...
                                  ;stack is already balanced
    ...

    在some_function()结束时,以简单的汇编指令

     retf      0006

     来清除栈的内容。该指令返回调用者,并调整了六个栈字节。


回复列表 (共5个回复)

沙发

call  some_function
add   sp,006          ;rebalance the stack
有问题!
怎么能这么胡写呢?
看这里:
[url=http://dozb.blogchina.com/1758683.html]http://dozb.blogchina.com/1758683.html[/url]

[url=http://auntyellow.yculblog.com/post.203401.html]http://auntyellow.yculblog.com/post.203401.html[/url]

板凳

[quote]怎么能这么胡写呢?[/quote]
To zhjh008

Did you notice one of what you provided is LZ's blog article?

[em1][em1][em1]

3 楼


^_^还真没发现,不过他这里的确是不对的,估计那汇编代码是他直接给copy过来了。
这里不应该有
add   sp,006          ;rebalance the stack
要不堆栈就不平衡了。

4 楼

补充一点给大家,如果大家知道了不要排我板砖呀。

pascal也就是__stdcall的调用方式是被调函数平栈的。就像上面给出的retf 0006
c也就是__cdecl的调用方式是调用者平栈的。这样c才有可能支持类似printf(char *,...)这样的变参方式的函数调用。

5 楼

Thanks Hyrut!
What you said is very good!

我来回复

您尚未登录,请登录后再回复。点此登录或注册