Jun 18, 2017 If you are using gcc, you can get the assembly as gcc -S -o a.s a.c if your source code is a.c.If you are using Visual Studio, you can get it when you debug by selecting the 'disassembly' window. Here is the output of Visual studio (I named the subrountine/function called 'common' that's why 'common' appears).
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.
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.
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?
StraightfwStraightfw
5 Answers
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.
![Assembly Assembly](/uploads/1/2/4/8/124866180/380633164.jpg)
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.
Jerry CoffinJerry Coffin
Take look at
gcc -S
:Other compilers that maintain at lest partial gcc compatibility may also accept this flag. LLVM's
clang
, for example, does.el.pescadoel.pescado
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
LeeorLeeor
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'.
MachtlMachtl
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.
ScottMcP-MVPScottMcP-MVP