LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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


Reply
  Search this Thread
Old 10-02-2004, 07:49 PM   #1
browneyes
LQ Newbie
 
Registered: Sep 2004
Distribution: Suse 9.2
Posts: 21

Rep: Reputation: 15
translate C to Assembly?


I want to know how to translate/convert C program to Assembly language?

any advice will be great.
 
Old 10-02-2004, 07:56 PM   #2
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
Code:
$ gcc -S foobar.c
...will create foobar.s
 
Old 10-02-2004, 08:01 PM   #3
browneyes
LQ Newbie
 
Registered: Sep 2004
Distribution: Suse 9.2
Posts: 21

Original Poster
Rep: Reputation: 15
I need more basics...

for example like this C program

#include <stdio.h>

int main( void )
{
int months = 12;
int years;
int days;
int i;
int nr1 = 8;
int nr2 = 10;
int nr3 = 15;


years = 7;
printf( "years 1 = %d\n", years );


days = months * 30;
printf( " days 1 = %d\n", days );

i = years * months;
printf( "i 1 = %d\n", i );

i = years % 12;
printf( "i 2 = %d\n", i );

i = nr1 + 4 + nr2 * i / nr3 * 3;
printf( " i 3 = %d\n\n", i );

printf( "\nStarting FOR loop:\n" );

for( i = -33; i < 75; i+= 4 )
{
printf( "i = %d\n", i + 3 );
}

return 0;
}

How do I convert that to Assembly lanuage? and is there a good learning site?

Thanks.
 
Old 10-02-2004, 08:10 PM   #4
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
http://linuxassembly.org/

should help I think. They have links to documentation and assembly source code.
 
Old 10-02-2004, 08:12 PM   #5
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Quote:
How do I convert that to Assembly lanuage?
Umm, bulliver just told you. If that code is saved in a file foo.c, simply do:
$ gcc -S foo.c
It will create a file foo.s (lowercase s on my system) which contains the corresponding
assembler code. View it in your favorite text editor.
 
Old 10-02-2004, 08:21 PM   #6
browneyes
LQ Newbie
 
Registered: Sep 2004
Distribution: Suse 9.2
Posts: 21

Original Poster
Rep: Reputation: 15
I know, but that code is way beyond what I am learning right now.
 
Old 10-02-2004, 08:27 PM   #7
browneyes
LQ Newbie
 
Registered: Sep 2004
Distribution: Suse 9.2
Posts: 21

Original Poster
Rep: Reputation: 15
I need to know how it changed from that C code to Assembler code, if I just get the code I won't be learning anything. It would be good if I could get some examples of codes that has been changed from c to assembler.
 
Old 10-02-2004, 09:45 PM   #8
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
browneyes - the gcc solution given will help you churn out plenty of examples of C code converted into assembly, but I suspect it won't help you too much with learning.

My advice would be to focus on learning an assembly language (preferably the one for your architecture ), some of the instructions, and how to do some of the simple things you might be used to from other languages. I have never found it too useful to learn by comparing similar programs in two languages, simply because it's not always possible to find a direct correspondence. Some things that are very simple in C are complicated in assembly, while some things that are very simple in assembly are more complicated in C. Being a lower-level language, there are some things you can do in assembly that you can't do at all in C (though don't ask me to name any of them!)

I think you'll find that once you get a good grasp of the basics, you'll be able to see the correspondences on your own, and (even better) begin to utilize the strengths of assembly itself, rather than seeing it as merely a translated C.
 
Old 10-04-2004, 02:06 PM   #9
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
browneyes,
If you want to learn assembly, I dont recommend using 'GCC -S' to do so. If you need a tutorial,
I recommend Dr Carter's assembly book @ http://www.drpaulcarter.com/pcasm/ .
It teaches assembly using intel syntax, which is easiest to learn with IMHO. You are going to need
more than source code to learn assembly.
 
Old 10-04-2004, 02:32 PM   #10
browneyes
LQ Newbie
 
Registered: Sep 2004
Distribution: Suse 9.2
Posts: 21

Original Poster
Rep: Reputation: 15
Thank you.
 
Old 10-04-2004, 02:51 PM   #11
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
There's one thing that may make it simplier. Compile with optimalization. Without the code is muuuch longer and more complicated.
gcc -O2 -S foo.c
 
Old 05-01-2009, 04:35 PM   #12
gowcaizer
LQ Newbie
 
Registered: May 2009
Posts: 1

Rep: Reputation: 0
This command converts the "foo.c" to "foo.s" in GAS of course
gcc -O2 -S foo.c

What about NASM? It means converting to "foo.asm" ?

I need it immediately. Please reply
 
  


Reply



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
translate C to MIPS kponenation Programming 1 10-01-2005 09:12 PM
please translate my dmesg output for me... Adrian Baker Linux - Software 2 07-27-2005 03:26 PM
please, translate to me maginotjr Slackware 6 07-08-2005 06:40 PM
translate hda 5 to grubspeak titanium_geek Linux - General 2 06-21-2005 10:19 PM
Please translate the following for a newbie.... Adrian Baker Linux - Newbie 11 04-30-2004 01:12 PM

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

All times are GMT -5. The time now is 12:59 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