473,883 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13537
"Protoman" <sp******@crayn e.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 "constraint s" 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******@crayn e.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 "constraint s" 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"(va l),:"a"(val));
return val;
}

Aug 4 '06 #3
"Protoman" <sp******@crayn e.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******@crayn e.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******@crayn e.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******@crayn e.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"(va l),:"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******@crayn e.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"(va l),:"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******@crayn e.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"(va l),:"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******@crayn e.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2019
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++ by Scott Meyers. Now I'm a little confused. Scott Meyers says that if an inline function can't be inlined by the compiler it is treated like a normal non-inlined function. However, he says that there is an older behaviour where compilers would...
8
2945
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 fsLogFile; #endif // #ifdef ASSAENGINE_NEED_DO_LOG
58
4703
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 the overhead of automatic variable sized arrays, I suspect it is smaller than that of malloc), although I'm not too worried about it. I was thinking that, with C99's variable length arrays, malloc shouldn't be needed most of the time. But I'm...
6
2722
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 pass by reference in C since you are just passing an address (or a pointer value address I guess?). So I was wondering is this correct?
5
6527
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 it like: #define inline /* nothing */ , and then define the functions within header files with static inline: static inline void Func(void) {}
12
3034
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. The global variables and global functions are hidden to prevent from accessing by the programmers. All global functions share global variables. Only very few global functions are allowed to be reusability for the programmers to use. Few...
12
11129
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. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
17
8392
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 the function if possible" (because if the compiler has the function definition available at an instantiation point, it will estimate whether to inline it or not, and do so if it estimates it would be beneficial, completely regardless of whether...
21
1905
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 made: (p. 39: "The ref modifier is essential in implementing a swap method") This is untrue. The following program demonstrates it. See how method "func4Swap" does a swap of two int variables without using ref, but using a 'helper' class and...
0
9797
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10863
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10422
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7978
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5807
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4622
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.