Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

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?

like image 364
Straightfw Avatar asked Nov 30 '22 02:11

Straightfw


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

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.

like image 107
Jerry Coffin Avatar answered Dec 05 '22 06:12

Jerry Coffin


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.

like image 32
el.pescado - нет войне Avatar answered Dec 05 '22 05:12

el.pescado - нет войне