4

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?

Straightfw
  • 2,143
  • 5
  • 26
  • 39
  • 17
    There are programs that to that... they are called _compilers_ ;-). Just try `gcc -S` for example. – rodrigo Nov 13 '13 at 17:55
  • 1
    The process of translating one language into another is called compiling, we usually compile C/C++ into binary, but compiling to another language, be it assembly or java is also quite possible, most C/C++ toolchains allow you to compile directly to assembly however, in GCC and Clang, this is done using the -S flag. – Skeen Nov 13 '13 at 17:58
  • Also generally speaking we mostly do compilations that reduce abstractions, and compiling from a low level language to a high level one, is sometimes referred to as 'decompiling' indeed when compiling from the low level language to the language, from which this low level translation was created. This however usually means a loss of all or most abstractions, as these are usually lost doing the compilation from high to low level. – Skeen Nov 13 '13 at 18:01
  • A last note, when compiling from a high level language to a low level one, it may be a good idea to disable debugging symbols and optimization, to make the output assembly more fit for human readers. – Skeen Nov 13 '13 at 18:02
  • Related: [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) – Peter Cordes May 04 '22 at 07:39

5 Answers5

11

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.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Thanks a lot. I have a question regarding the registers you mentioned: as I said, I'm going to need an assembly code such that it works under Intel 8080 or similar "ancient" processors. Is there any way to simulate (or somehow "tell") the compiler what kind of CPU it's compiling for so that I end up with an assembly code that doesn't use registers which simply aren't present in 8080 or 8086? – Straightfw Nov 13 '13 at 18:09
  • @Straightfw: From a practical viewpoint, about all you can (probably) do is find an older compiler that targets (for example) an 8086. Most compilers are divided into a front-end devoted to the source language and a back-end devoted to the target processor, so (in theory) you could write a back-end targeting the processor in question, but it's generally non-trivial. For a really limited processor could be quite difficult indeed. – Jerry Coffin Nov 13 '13 at 18:36
  • @Straightfw: It's more than just a few registers missing. Most practically, _all_ 32 and 64 bits registers are missing pre-80386. And `long` is still a 32 bits type. That means that your compiler must be able to do something which no modern compiler cares for, synthesizing 32 bits operations using only 16 bits registers. – MSalters Nov 14 '13 at 01:22
6

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.

2

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

VoidStar
  • 936
  • 1
  • 7
  • 17
2

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

Leeor
  • 19,260
  • 5
  • 56
  • 87
0
(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.

ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15