如何将变量从C ++函数传递到内联ASM [英] How to pass variables from a C++ function into inline ASM

查看:75
本文介绍了如何将变量从C ++函数传递到内联ASM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到内联汇编程序和/或链接器的
Dev-Cpp看到我的变量'已被传递到C ++ fn其中

内联ASM代码是。任何人都可以帮我弄清楚如何让这个工作?
工作?具体错误是[链接器错误]未定义引用

`val''。这是'我的计划:


#include< iostream>

#include< cstdlib>

使用std :: cout;

使用std :: cin;

使用std :: endl;

使用std :: system;


int Double(int val)

{

asm(" mov eax,val \ n");

asm(add eax,eax \\\
);

asm(" mov val,eax \ n");

return val;

}


int main()

{

int val;

cout<< 输入值: << endl;

cin> val;

cout<< val<< " * 2 =" <<双(val)<<结束;

系统(PAUSE);

返回EXIT_SUCCESS;

}


如果有人可以提供帮助,那就太好了。谢谢!!!!

I''m having the trouble getting the inline assembler and/or linker of
Dev-Cpp to see my variable that''s been passed into the C++ fn where the
inline ASM code is. Can anyone help me figure out how to get this to
work? The specific error is "[Linker error] undefined reference to
`val''". And here''s my program:

#include <iostream>
#include <cstdlib>
using std::cout;
using std::cin;
using std::endl;
using std::system;

int Double(int val)
{
asm("mov eax,val \n");
asm("add eax,eax \n");
asm("mov val,eax \n");
return val;
}

int main()
{
int val;
cout << "Enter a value: " << endl;
cin >val;
cout << val << "*2=" << Double(val) << endl;
system("PAUSE");
return EXIT_SUCCESS;
}

If anyone could help, that''d be great. Thanks!!!!

推荐答案

" Protoman" < sp ****** @ crayne.orgwrote:
"Protoman" <sp******@crayne.orgwrote:

>
我在获取内联汇编程序时遇到了麻烦和/或
Dev-Cpp的链接器,看看我的变量是否被传递到了内联ASM代码所在的C ++ fn。任何人都可以帮我弄清楚如何使这个工作?具体错误是[链接器错误]未定义引用
`val''。这是我的计划:

#include< iostream>
#include< cstdlib>
使用std :: cout;
使用std :: cin;
使用std :: endl;
使用std :: system;

int double(int val)
{
asm(" mov) eax,val \ n");
asm(" add eax,eax \ n");
asm(" mov val,eax \ n");
返回val;
}
>
I''m having the trouble getting the inline assembler and/or linker of
Dev-Cpp to see my variable that''s been passed into the C++ fn where the
inline ASM code is. Can anyone help me figure out how to get this to
work? The specific error is "[Linker error] undefined reference to
`val''". And here''s my program:

#include <iostream>
#include <cstdlib>
using std::cout;
using std::cin;
using std::endl;
using std::system;

int Double(int val)
{
asm("mov eax,val \n");
asm("add eax,eax \n");
asm("mov val,eax \n");
return val;
}



这里的问题是名称val无法生存到汇编程序中gcc创建的
。当它变成汇编时,它就像是

The problem here is that the name "val" does not survive into the assembler
that gcc creates. By the time it becomes assembly, it''s something like


ebp + 4.


gcc中的内联汇编程序是非平凡的,大师级的话题。总而言之,

你需要给gcc一个约束列表。告诉它

变量在哪里。例如,您的函数可能如下所示:


int Double(int val)

{

asm(" add) %% eax,%% eax"

:" a"(val),

:" a"(val)

: );

}


第一个a子句告诉它注册eax是这个

序列的输出,应该存储在val中。第二个a表示第二个a。子句告诉它

注册eax是这个序列的输入,应该从val加载。


也可以让编译器通过使用r

约束来选择寄存器,并使用%0和%0。在说明书中,但我不知道你是否可以

只有一个寄存器既可以是输入也可以是该方法的输出。

-

- Tim Roberts, ti**@probo.com

Providenza& Boekelheide,Inc。

ebp+4.

Inline assembler in gcc is a non-trivial, guru-level topic. To summarize,
you need to give gcc a list of "constraints" that tell it where the
variable is. For example, your function might look like this:

int Double(int val)
{
asm( "add %%eax, %%eax"
: "a" (val),
: "a" (val)
: );
}

The first "a" clause tells it that "register eax is an output from this
sequence and should be stored in val". The second "a" clause tells it that
"register eax is an input to this sequence and should be loaded from val."

It is also possible to let the compiler choose the register by using a "r"
constraint, and use "%0" in the instructions, but I don''t know if you can
have a single register be both an input and an output with that method.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.




Tim Roberts写道:

Tim Roberts wrote:

Protoman < sp ****** @ crayne.orgwrote:
"Protoman" <sp******@crayne.orgwrote:


我在获取内联汇编程序和/或链接器时遇到了麻烦

Dev-Cpp看到我的变量已被传递到C ++ fn,其中

内联ASM代码是。任何人都可以帮我弄清楚如何让这个工作?
工作?具体错误是[链接器错误]未定义引用

`val''。这是'我的计划:


#include< iostream>

#include< cstdlib>

使用std :: cout;

使用std :: cin;

使用std :: endl;

使用std :: system;


int Double(int val)

{

asm(" mov eax,val \ n");

asm(add eax,eax \\\
);

asm(" mov val,eax \ n");

return val;

}

I''m having the trouble getting the inline assembler and/or linker of
Dev-Cpp to see my variable that''s been passed into the C++ fn where the
inline ASM code is. Can anyone help me figure out how to get this to
work? The specific error is "[Linker error] undefined reference to
`val''". And here''s my program:

#include <iostream>
#include <cstdlib>
using std::cout;
using std::cin;
using std::endl;
using std::system;

int Double(int val)
{
asm("mov eax,val \n");
asm("add eax,eax \n");
asm("mov val,eax \n");
return val;
}



这里的问题是名称val无法生存到汇编程序中gcc创建的
。当它变成装配时,它就像是


The problem here is that the name "val" does not survive into the assembler
that gcc creates. By the time it becomes assembly, it''s something like


这篇关于如何将变量从C ++函数传递到内联ASM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆