LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to converting the c++ code to assembly (https://www.linuxquestions.org/questions/programming-9/how-to-converting-the-c-code-to-assembly-693010/)

Nejad 12-26-2008 12:32 AM

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.

nc3b 12-26-2008 03:31 AM

gcc -S source.cpp -o out.s

Does that do it?

twwwater 12-26-2008 03:46 AM

g++ -S source.cpp -o out.s
maybe this could be work if the gcc does not work....

knudfl 12-26-2008 04:35 AM

'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.
....

salasi 12-26-2008 04:38 AM

Quote:

Originally Posted by Nejad (Post 3387396)
...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?

ErV 12-26-2008 05:15 AM

Quote:

Originally Posted by nc3b (Post 3387470)
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.

Nejad 12-26-2008 06:44 AM

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

nc3b 12-26-2008 07:33 AM

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:)

Nejad 12-26-2008 08:23 AM

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

pixellany 12-26-2008 09:06 AM

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.

knudfl 12-26-2008 01:46 PM

Nice piece of c++ code, but it will only produce
an executable with 'c++' = 'g++' , not with 'gcc -S'

Nejad 12-27-2008 01:13 AM

Hi All,

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

thanks

salasi 12-27-2008 04:58 AM

Quote:

Originally Posted by Nejad (Post 3388258)
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.

pixellany 12-27-2008 05:59 AM

Quote:

Originally Posted by Nejad (Post 3388258)
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.

twwwater 12-27-2008 07:45 PM

Quote:

Originally Posted by Nejad (Post 3388258)
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 .)


All times are GMT -5. The time now is 07:09 PM.