【问题标题】:Adding more than four params in Assembly在 Assembly 中添加四个以上的参数
【发布时间】:2013-12-10 17:35:03
【问题描述】:

我有如下所示的 C++ 文件和 .asm 文件。我正在尝试添加我在求和函数中传递的所有参数

summation(int a ,int b ,int c ,int d, int e,int f)

c++ 文件如下所示:

#include <iostream>
#include <conio.h>
#include<stdlib.h>

using namespace std;

extern "C" int summation(int a ,int b ,int c ,int d, int e,int f);


int main(){

cout << "Summation : "<<summation(1,2,7,1,8,10)<<endl;
return 0;
}

asm 文件看起来像这样:

.code

summation proc

    sub rsp,30h
    mov eax,ecx
    add eax,edx
    add eax,r8d
    add eax,r9d

    add eax,dword ptr [rsp+20h]
    add eax,dword ptr [rsp+28h]
    add rsp,30h
    ret 
summation endp

end

有了这些代码,结果总是 22(应该是 29)。我的代码有什么问题?

【问题讨论】:

  • 你正在使用 fastcall,它不会处理超过 4 个。其余的为你压入堆栈。
  • 那么我该如何处理它们(那些被压入堆栈的)? @ScarletAmaranth
  • 为什么不让 64 位 C 编译器告诉你答案?

标签: c++ assembly masm


【解决方案1】:

您不需要为堆栈帧保留空间,除非您正在执行 calling convention,快速调用仅允许 4 个参数,请尝试使用 RSP 直接使用堆栈,

summation proc

    mov eax,ecx
    add eax,edx
    add eax,ebx
    add eax,r8d
    add eax,r9d

    add eax,[rsp+50o]
    add eax,[rsp+60o]


    ret
summation endp

我使用了八进制,如果您愿意,可以使用十进制

【讨论】:

    【解决方案2】:

    rsp+20h 不指向参数区。您自己在开始时从 rsp 中减去 30h,不知道为什么 - 为什么您希望推送参数在该区域内?它们位于框架下方。

    回顾一下:在程序入口处,返回地址在 rsp 处占用 8 个字节。在此之下,有两个推送参数 - rsp+8 和 rsp+0ch。如果您将 rsp 再减少 30h,则它们为 rsp+38h,rsc+3ch。

    【讨论】:

    • 是的,我犯了错误..从我写的“dword ptr [rsp+20h]”和“dword ptr [rsp+20h]”不能容纳其余两个数字。但我没有不知道如何将它们推入堆栈..如果您找到解决方案,请写下我的 asm 文件的完整版本..@Seva Alekseyev
    • C 代码在执行调用时将它们推入堆栈。组装过程只需要知道在堆栈上的哪个位置可以找到它们。
    【解决方案3】:

    您的stack Frame 有问题,您需要先修复它 :) 但是我有一个解决方案给你,试试这个

    #include <iostream>
    #include <conio.h>
    using namespace std;
    extern "C" int sum2(int *i,int a);  
    int main()
    {
    int i2[10] = {1,2,3,4,5,10,20,30,40,50};
    cout << "The Sum is using Array : "<< sum2 (i2,10)<<endl;
    _getch();
    return 0;
    }
    

    在你的 asm.asm 中放这个

    sum2 proc
    
    ;{1,2,3,4,5,10,20,30,40,50}; //165
    mov rax,0 ;Initialize it to zero 
    mov rbx,rdx
    
    adding:
    add rax,[rcx]
    add rcx,4
    dec rbx
    jnz adding;
    
    finish: 
        ret
    
    sum2 endp
    

    它还对数字求和,但这次来自一个数组 希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-18
      • 1970-01-01
      • 1970-01-01
      • 2021-10-28
      • 2016-12-15
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多