LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Closed Thread
  Search this Thread
Old 12-26-2008, 12:32 AM   #1
Nejad
LQ Newbie
 
Registered: Nov 2006
Posts: 9

Rep: Reputation: 0
How to converting the c++ code to assembly


Hi guys,

i really need help how to converting the c++ code to assembly. can anybody help me. for example this code

#include <stdio.h>
float average(float x, float y);
int main() {
float a, b, c;
a = 1;
b = 3;
c = average(a, b);
printf("The average of %f and %f is %f.\n", a, b, c);
}
float average(float x, float y) {
float z;
z = (x + y) / 2; /* The average */
return z;
}

i would really appreciate if anybody could help me convert to assembly language.
thank and best regards.
 
Old 12-26-2008, 03:31 AM   #2
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
gcc -S source.cpp -o out.s

Does that do it?
 
Old 12-26-2008, 03:46 AM   #3
twwwater
Member
 
Registered: Nov 2008
Posts: 113
Blog Entries: 1

Rep: Reputation: 15
g++ -S source.cpp -o out.s
maybe this could be work if the gcc does not work....
 
Old 12-26-2008, 04:35 AM   #4
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,511

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
'g++ source.cpp' produces a.out .

'./a.out' displays
The average of 1.000000 and 3.000000 is 2.000000.

And as shown in #2 and #3, the -o option allows
you to choose any name for the produced object.
....

Last edited by knudfl; 12-26-2008 at 04:41 AM.
 
Old 12-26-2008, 04:38 AM   #5
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
Quote:
Originally Posted by Nejad View Post
...help me convert to assembly language.
You should specify the processor architecture, if you want something that you can work with.

I can see few circumstances in which the direct answer to the question asked is directly all that helpful (except in a zen kind of sense, and that is somewhat indirect); is this related to homework?
 
Old 12-26-2008, 05:15 AM   #6
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by nc3b View Post
gcc -S source.cpp -o out.s
It is actually "gcc -S source.c". "-o out.s" isn't necessary at all, because gcc will create source.s automatically for each file. See "man gcc" for more info about -S switch.
 
Old 12-26-2008, 06:44 AM   #7
Nejad
LQ Newbie
 
Registered: Nov 2006
Posts: 9

Original Poster
Rep: Reputation: 0
Hi All,

But this gcc command where I can writing, because For the first time I use it.
and what you mean with 'out.s'.

thanks
 
Old 12-26-2008, 07:33 AM   #8
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
Open a terminal, navigate to the location of your source file, and then issue the command. The file produced (out.s or source.s) will contain the assembly code (try cat out.s).

@ErV:
Thank you, you are right, but I believe specifying the output file is a good habit (for instance if you say gcc source.c it will output to a.out), and I see no harm in using it. Cheers
 
Old 12-26-2008, 08:23 AM   #9
Nejad
LQ Newbie
 
Registered: Nov 2006
Posts: 9

Original Poster
Rep: Reputation: 0
Hi All,

thanks for your replay, but I still do not know how to use the gcc command from a terminal or how to run the terminal from where running, and what is the exactly syntax of gcc command.
e.g
#gcc -S progectname.cpp -o outputfile.s (how to write this in a terminal??)
the outputfile include the assembly code.

thanks
 
Old 12-26-2008, 09:06 AM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
I still do not know how to use the gcc command from a terminal
Open the terminal and enter the command??
Quote:
or how to run the terminal from where running,
I don't understand
Quote:
and what is the exactly syntax of gcc command.
Have you read thru the man page for gcc and g++? What part of the syntax do you not understand---please be specific.
 
Old 12-26-2008, 01:46 PM   #11
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,511

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
Nice piece of c++ code, but it will only produce
an executable with 'c++' = 'g++' , not with 'gcc -S'
 
Old 12-27-2008, 01:13 AM   #12
Nejad
LQ Newbie
 
Registered: Nov 2006
Posts: 9

Original Poster
Rep: Reputation: 0
Hi All,

you can give me the gcc command for windows because i don't have it.

thanks
 
Old 12-27-2008, 04:58 AM   #13
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
Quote:
Originally Posted by Nejad View Post
Hi All,

you can give me the gcc command for windows because i don't have it.

thanks
Are you saying you don't have gcc (well, get it, then), that you don't understand what to type on the command line (in spite of the fact that you have been told several times) or that you don't have windows (not what I'd call a problem, but maybe a fact all the same)?

I'm going to re-iterate that assembler is assembler for a particular processor architecture or maybe even more specific than that, although, say, any arm assembler variant ought to be comprehensible to anyone who understands other arm assembler formats - after all, you do not seem to be trying to execute anything.

So without specifying the architecture there cannot be a single answer to your question; answers given may be totally useless to you if your understanding of other architectures is not good enough.
 
Old 12-27-2008, 05:59 AM   #14
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by Nejad View Post
Hi All,

you can give me the gcc command for windows because i don't have it.

thanks
When you post this kind of non-sequitor, but don't respond to our other attempts to help you, I'm not sure what we can do for you.

In your other thread, you have not replied to any of the recent comments.

I will recommend that this be closed.
 
Old 12-27-2008, 07:45 PM   #15
twwwater
Member
 
Registered: Nov 2008
Posts: 113
Blog Entries: 1

Rep: Reputation: 15
Quote:
Originally Posted by Nejad View Post
Hi All,

you can give me the gcc command for windows because i don't have it.

thanks
You make me ...

I am not sure if you can use
1 "start"
2 cmd
3 "find your ELF(in windows it should be a file like this abc.exe,and lets' assume the ELF file is named abc.exe) and cd(change directory) in that "Directory" "
4 use debug abc.exe.
5 type u.

now,look,does that you want ?
if it is now,maybe you should add another information to it.
like type u 00 /100/someother data ; specify the lines' that your program start at.



(well,I did not use debug for a long time,and I can not remember it clearly,maybe I got some mistakes,you should go and google some information about "debug command" ,then do it .)

Last edited by twwwater; 12-27-2008 at 07:48 PM. Reason: Maybe I got wrong...
 
  


Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to see assembly code on Runtime Azrai Programming 1 07-18-2006 11:44 PM
assembly code on linux omerlh Programming 1 06-01-2006 03:25 AM
gcc/Assembly code gigantas1985 Programming 0 02-28-2006 05:52 AM
Command for getting assembly code aizkorri Programming 1 06-18-2002 05:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration