Straightfw
Straightfw

Reputation: 2211

Is it possible to convert C/C++ source code to assembly?

Is it possible to somehow convert a simple C or C++ code (by simple I mean: taking some int as input, printing some simple shapes dependent on that int as output) to assembly language? If there isn't I'll just do it manually but since I'm gonna be doing it for processors like Intel 8080, it just seemed a bit tedious. Can you somehow automate the process?

Also, if there is a way, how good (as in: elegant) would the output assembly file source code be when compared to just translating it manually?

Upvotes: 4

Views: 21139

Answers (5)

ScottMcP-MVP
ScottMcP-MVP

Reputation: 10425

(by simple I mean: taking some int as input, printing some simple shapes dependent on 
 that int as output) to assembly language?

Looking at the output of an x86 compiler is not going to be very helpful, since inputting and outputting are done by a C or C++ library. With an 8080 there is no such library so you have to develop your own I/O routines for some particular hardware. That's lots and lots of additional work.

Upvotes: 0

Leeor
Leeor

Reputation: 19746

Most folks here are right, but seem to have missed the note about 8080 (no wonder, it's not in the title :). However, Google comes to the rescue as always - looking for compiler for 8080 produces some nice results like these:

Most of these are pretty old and might be poorly maintained. You might also try 8085 which is fairly similar

Upvotes: 2

Jerry Coffin
Jerry Coffin

Reputation: 490728

Most compilers will let you produce assembly output. For a couple of obvious examples, Clang and gcc/g++ use the -S flag, and MS VC++ uses the -Fa flag to do so.

A few compilers don't support this directly (e.g., if memory serves Watcom didn't). The ones I've seen like this had you produce an object file, and then included a disassembler that would produce an assembly language file from the object file. I don't remember for sure, but it wouldn't surprise me if this is what you'd need to do with the Digital Mars compiler.

To somebody who's accustomed to writing assembly language, the output from most compilers typically tends to look at least somewhat inelegant, especially on a CPU like an x86 that has quite a few registers that are now really general purpose, but have historically had more specific meanings. For example, if some piece of code needs both a pointer and a counter, a person would probably put the pointer in ESI or EDI, and the counter in ECX. The compiler might easily reverse those. That'll work fine, but an experienced assembly language programmer will undoubtedly find it more readable using ESI for the pointer and ECX for the counter.

Upvotes: 11

Take look at gcc -S:

gcc -S hello.c # outputs hello.s file

Other compilers that maintain at lest partial gcc compatibility may also accept this flag. LLVM's clang, for example, does.

Upvotes: 7

VoidStar
VoidStar

Reputation: 984

Well, yes there is such a program. It's called "Compiler"

To answer your edit: The elegance of the output depends on the optimization of your compiler. Usually compilers do not generate code we humans would call "elegant".

Upvotes: 2

Related Questions