473,326 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 13480
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: publictom | last post by:
I just happened to read item 47, "Control Flow", in Exceptional C++ by Herb Sutter (which is based on Guru of the Week 12), just after reading item 33, "Use Inlining Judiciously", in Effective C++...
8
by: Leo jay | last post by:
dear all, i want to write log on disk file when macro NEED_DO_LOG is defined. otherwise, do nothing. so i wrote code like this: #ifdef NEED_DO_LOG fstream fsLogFile (...); #else CDummyStream...
58
by: Jorge Peixoto de Morais Neto | last post by:
I was reading the code of FFmpeg and it seems that they use malloc just too much. The problems and dangers of malloc are widely known. Malloc also has some overhead (although I don't know what is...
6
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as...
5
by: Martin Wells | last post by:
C89 doesn't have inline but I wanted to use inline in my C89-compliant program. Someone suggested to me that I use macros to determine whether inline was supported, and if necessary, simply define...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
21
by: raylopez99 | last post by:
In the otherwise excellent book C# 3.0 in a Nutshell by Albahari et al. (3rd edition) (highly recommended--it's packed with information, and is a desktop reference book) the following statement is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.