468,463 Members | 2,030 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,463 developers. It's quick & easy.

How to pass variables from a C++ function into inline ASM

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!!!!

Aug 2 '06 #1
10 12988
"Protoman" <sp******@crayne.orgwrote:
>
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;
}
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.

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.

Aug 4 '06 #2

Tim Roberts wrote:
"Protoman" <sp******@crayne.orgwrote:

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;
}

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.

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.
Now it's saying: 21 C:\Dev-Cpp\ASM.cpp expected string-literal before
':' token

Code:

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

int Double(int val);

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

Aug 4 '06 #3
"Protoman" <sp******@crayne.orgwrote:
And here's my program:

#include <iostream>
#include <cstdlib>
Aren't those items supposed to have an .h extension? For
example...

#include <iostream.h>
#include <cstdlib.h>

Hope this helps.

--
Jim Carlock
Post replies to the group.

Aug 5 '06 #4
Jim Carlock wrote:
"Protoman" <sp******@crayne.orgwrote:
>>And here's my program:

#include <iostream>
#include <cstdlib>


Aren't those items supposed to have an .h extension? For
example...

#include <iostream.h>
#include <cstdlib.h>
No!

--
Ian Collins.

Aug 5 '06 #5

Jim Carlock wrote:
"Protoman" <sp******@crayne.orgwrote:
And here's my program:

#include <iostream>
#include <cstdlib>

Aren't those items supposed to have an .h extension? For
example...

#include <iostream.h>
#include <cstdlib.h>

Hope this helps.

--
Jim Carlock
Post replies to the group.
Hey, Jimmy-boy, ever heard of a little thing called "using namespace
std;"? Have you even heard of namespace std, the C++ Standard
namespace? And I REALLY don't think THAT's the problem here.

Aug 5 '06 #6
"Protoman" <sp******@crayne.orgwrote:
>
Now it's saying: 21 C:\Dev-Cpp\ASM.cpp expected string-literal before
':' token
My fault, I put an extra comma in there. Replace the ,: with just :
>int Double(int val)
{
asm("add eax,eax":"a"(val),:"a"(val));
^
>return val;
}
Do you have a Linux system handy? Almost everything I learned about gcc
inline assembler, I learned from two places: perusing the include files in
a Linux distribution, and this very handy tutorial:

http://www.delorie.com/djgpp/doc/bre...ine_djgpp.html
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.

Aug 6 '06 #7

Tim Roberts wrote:
"Protoman" <sp******@crayne.orgwrote:

Now it's saying: 21 C:\Dev-Cpp\ASM.cpp expected string-literal before
':' token

My fault, I put an extra comma in there. Replace the ,: with just :
int Double(int val)
{
asm("add eax,eax":"a"(val),:"a"(val));
^
return val;
}

Do you have a Linux system handy? Almost everything I learned about gcc
inline assembler, I learned from two places: perusing the include files in
a Linux distribution, and this very handy tutorial:

http://www.delorie.com/djgpp/doc/bre...ine_djgpp.html
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Nope. WinXP rules!!!!

Aug 6 '06 #8

Tim Roberts wrote:
"Protoman" <sp******@crayne.orgwrote:

Now it's saying: 21 C:\Dev-Cpp\ASM.cpp expected string-literal before
':' token

My fault, I put an extra comma in there. Replace the ,: with just :
int Double(int val)
{
asm("add eax,eax":"a"(val),:"a"(val));
^
return val;
}

Do you have a Linux system handy? Almost everything I learned about gcc
inline assembler, I learned from two places: perusing the include files in
a Linux distribution, and this very handy tutorial:

http://www.delorie.com/djgpp/doc/bre...ine_djgpp.html
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
OK, I fixed that, now it says: 21 C:\Dev-Cpp\ASM.cpp output operand
constraint lacks `=' Which one is output?

Aug 6 '06 #9
"Protoman" <sp******@crayne.orgwrites:
Tim Roberts wrote:
Do you have a Linux system handy? Almost everything I learned about gcc
inline assembler, I learned from two places: perusing the include files in
a Linux distribution, and this very handy tutorial:
http://www.delorie.com/djgpp/doc/bre...ine_djgpp.html

OK, I fixed that, now it says: 21 C:\Dev-Cpp\ASM.cpp output operand
constraint lacks `=' Which one is output?
Oh for fuck's sake. That is answered quite near the top of the page
Tim pointed you towards. From this we can only infer bad things
about the intelligence of people who think "Win-XP rules". Thanks
for reinforcing the stereotype.

Phil
--
The man who is always worrying about whether or not his soul would be
damned generally has a soul that isn't worth a damn.
-- Oliver Wendell Holmes, Sr. (1809-1894), American physician and writer

Aug 6 '06 #10

Phil Carmody wrote:
"Protoman" <sp******@crayne.orgwrites:
Tim Roberts wrote:
Do you have a Linux system handy? Almost everything I learned about gcc
inline assembler, I learned from two places: perusing the include files in
a Linux distribution, and this very handy tutorial:
http://www.delorie.com/djgpp/doc/bre...ine_djgpp.html
OK, I fixed that, now it says: 21 C:\Dev-Cpp\ASM.cpp output operand
constraint lacks `=' Which one is output?

Oh for fuck's sake. That is answered quite near the top of the page
Tim pointed you towards. From this we can only infer bad things
about the intelligence of people who think "Win-XP rules". Thanks
for reinforcing the stereotype.

Phil
--
The man who is always worrying about whether or not his soul would be
damned generally has a soul that isn't worth a damn.
-- Oliver Wendell Holmes, Sr. (1809-1894), American physician and writer
Actually, about two nanoseconds after I posted "WinXP rules!!!!", I
found and fixed the problem myself, but my router went down, so I
couldn't tell you. Bye.

Aug 7 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by publictom | last post: by
8 posts views Thread by Leo jay | last post: by
58 posts views Thread by Jorge Peixoto de Morais Neto | last post: by
6 posts views Thread by lisp9000 | last post: by
5 posts views Thread by Martin Wells | last post: by
12 posts views Thread by Bryan Parkoff | last post: by
17 posts views Thread by Juha Nieminen | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.