PDA

View Full Version : In line…Asm..


jackall
May 12th, 2008, 01:17
The code below compiles all right....
Code:
int main ()
{
int a, b, c;

cout<<"Enter a number\n";
cin>>a;
cout<<" A Second number to add\n";
cin>>b;
cin.ignore ();
c=a+b;

cout<<"Result is :"<< c<<endl;
cin.get ();
return 0;
}

Here i tried to introduce a block of asm code into c++ ; to substitute the c=a+b part of the code

Code:
#include<iostream>
using namespace std;
int main ()
{
int a,b,c;

cout<<"Enter a number\n";
cin>>a;
cout<<"A Second number to add\n";
cin>>b;
cin.ignore();

_asm {
mov eax,a
mov ebx,b
add eax,ebx
mov c,eax
}
cout<<"Result is:"<<c<<endl;
cin.get();
return 0;
}

But it doesnot compile..??
Error is shown at the line _asm .!!
What is/are the errors ?

The platform :
Xp professional version 2002 service pack2
Intel(R) Pentium(R)D CPU 2.66GHz 2.67GHz

Any suggestion, modification or observation would be greatly appreciated .
Thank you.

asteri
May 12th, 2008, 02:41
What compiler did you use? I tried it with borland c++ builder and it works fine

Aimless
May 12th, 2008, 11:29
Did you make sure that its __asm {} (Note, that is TWO underscores!!)

Have Phun

jackall
May 12th, 2008, 21:33
asteri….

i use the Bloodshed Dev-C++ package. i believe the compiler used here is MinGW.

iam not very well-versed in the use of any compiler. whenever i required its occasional help, it was rendered mutely like a disciplined hard working entity behind the scene.
Now that u have mentioned this …..Need to check over this poltergeist...

Thanks….

OHPen
May 13th, 2008, 02:22
@jackall: if you use devc++, then its definitly gcc. gcc itself is using inline asm in at/t syntax, which is different from the "microsoft inline asm", which probably also follows a well know standardization.

just google for "at&t syntax inline assembler gcc". this you give you the informations you need. but a small warning, at&t syntax is not very "nice",
in my eyes is even awful. maybe you should switch to an other compiler, they mostly use the syntax you used above.

regards,

OHPen

jackall
May 13th, 2008, 03:35
Aimless …suggested _ _

The Double underscore ... I was not aware of this requirement…

So i fed one more _ to this Compiler spirit.

Mastermind had a stern look at my narrow-minded approach and

kept on staring at me …..Disappointed.

till next time……

jackall
May 13th, 2008, 22:44
OHpen …says it is Gcc that is used in DevC++... Well. i took a dip in that line and surfaced grabbing the following information.

The GNU C Compiler uses the assembler `AS' as a backend. Front-end and back-end refer to the initial and the end stages of a process. The front-end is responsible for collecting input in various forms from the user and processing it to conform to a specification the back-end can use.

This assembler AS uses AT&T syntax.

Further AT&T syntax requires:
-prefix registers with `%', -Prefix numeric constants with `$'. - Add the '\n\t' at the end of each line. -Each line is to be enclosed in quotes. May be more ……I have to find out more to apply it meaningfully.

A long row to hoe.

Regards...

personmans
May 14th, 2008, 09:56
Since you decided to try and research, I figured I could give you an example of the rules you just posted, applied to some familiar code.

Code:

__asm {
movl %eax,$a
movl %ebx,$b
addl %eax,%ebx
movl $c,%eax
}

ZaiRoN
May 14th, 2008, 14:22
One rule to rule them all: start from the FAQ!

http://www.bloodshed.net/dev/faq.html#asm

After that you can follow all the good advices given by the other members

jackall
May 15th, 2008, 02:44
Code:
• “ For static "C" variables also need prefix ’$’ “
I suppose a,b,c are static variable as i see personmans has prefixed $ to a,b,c.
#include<iostream>
using namespace std;
int main ()
{
int a,b,c;

cout<<"Enter a number\n";
cin>>a;
cout<<"A Second number to add\n";
cin>>b;
cin.ignore();

Code:
asm {
"movl $a,%eax\n\t" ; AT&T format is: mnemonic: source, destination
"movl $b,ebx\n\t" ; If we've more than one instructions, we write
"addl %ebx,%eax\n\t" ; one per line in double quotes.
"movl %eax,$c"; ;and also suffix a '\n' and '\t' to the instruction.
}

cout<<"Result is:"<<c<<endl;
cin.get();
return 0;
}
Some error is holding back the compilation!!

ZaiRoN
May 15th, 2008, 04:32
I'm not used to at&t syntax, but this is what I get reading some at&t manuals.

You can perform an add operation using one simple (!?!) instruction. Look at this block:
Code:

asm ( assembler template
: output operands /* optional */
: input operands /* optional */
: list of clobbered registers /* optional */
);
You can use an asm instruction with some parameters. It's what we want, infact we can use an add instruction with two input operands and one output operand. I changed the name of your static variables because a,b,c,d,.. refer to general registers. Here is the result:
Code:
#include<iostream>
using namespace std;
int main ()
{
int n1;
int n2;
int res;

cout<<"Enter a number\n";
cin>>n1;
cout<<"A Second number to add\n";
cin>>n2;
cin.ignore();

__asm ("addl %%ebx, %%eax" : "=a" (res) : "a" (n1), "b" (n2));

cout<<"Result is:"<<res<<endl;
cin.get();
return 0;
}

jackall
May 15th, 2008, 07:04
Thank you …ZaiRoN...

The result is...Compiler flashes a DONE smile when I presented your code.

Code:
We can use an add instruction with two input operands and
one output operand. I changed the name of your static variables because a,b,c,d,..
refer to general registers.


I need further reading to understand the above lines properly; of course I will seek the help of FAQ.

Regards…

ZaiRoN
May 15th, 2008, 10:50
You can use a,b and c for your variables of course... It's all on the net

blabberer
May 20th, 2008, 10:58
well i havent read this thread but if its gcc and you want to do asm in intel on gcc or gas

youcan try going through this links

http://www.reversing.be/article.php?story=20051222184434392

http://www.reversing.be/article.php?story=20051203194931893