For a Computer Science assignment, I am supposed to write a small program in any language and determine the ratio of source code instructions to compiled machine code instructions - how do I do this?
1 Answers
Depending on your compiler you can use a switch to also create the assembler version of your code. The assembler code is the direct mnemonic representation of the opcodes ("machine language").
With gcc
(consider adding -masm=intel
if you prefer the Intel over the default AT&T syntax):
gcc -S ...
to my knowledge other compilers on unixoid systems also support -S
. This will skip compilation and you'll want to look for files named after your source files, but with the extension replaced by .s
.
Or with MSVC:
cl.exe /FA
... you may add c
(i.e. /FAc
) which will also compile it. You'll want to look for .asm
files then.
Also see: How can I see the assembly code for a C++ program?
After this is done, you basically compare (relevant!) source lines. What's relevant depends on your own (or your teacher's) judgement, however. There are tools out there to count SLOC, e.g. the one referenced in this Wikipedia article: SLOCCount.
NB: the statement in the first paragraph about direct representation is slightly incorrect. If we're talking macro assembler then this doesn't hold, because of the macros. But even though MSVC uses a macro assembler as backend, the compiler doesn't make much use of the macro capabilities, so they shouldn't interfere (much) in the SLOC-counting.

- 1
- 1

- 20,597
- 9
- 86
- 152