• C

View Number of machine instructions for my code.

I have a code in C which I have compiled using VC++ 6.0. I need to see the total number of machine instructions of my entire code. How can I do that?

Thank you.
Who is Participating?
grg99Connect With a Mentor Commented:
Wait a sec, you asked for the size of YOUR code, and that's what my code is supposed to tell you.

BUT if you want to know how long YOUR code is, plus any and all other routines that get linked to it, that's a very different thing.

To find that out, have your linker generate a load map.    There should be a summary in there that shows the size of each block of code and data, and a total of all too.

( and if this code is going to be run inside the chip, you'd better check with the chip-makers, they probably cannot handle any old VC++ format file, it probably has to be linked with special libraries that understand they're in a ROM)

I am not sure if this helps, but just my two cents. Just debug your code (F5) with a breakpoint on the first line of your code. Now go to Debug->Windows->Disassembly (The shortcut is normally Alt + 8). It shows you the source code with the corresponding assembly code.
prateek_shriAuthor Commented:
But this shows me lot of BYTE code, and some of which is not even relevant to my code as belongs to header files for windows.

I need the count of number of machine instructions just for my code.

How can I have the count?

Thank you.
Prepare for an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program curriculum features two internationally recognized certifications from the EC-Council at no additional time or cost.

prateek_shriAuthor Commented:
And also could you tell me, what is the typical number for "number of machine instruction" for a C program with around 1,000 lines of code.

Thank you.
In Visual Studio .NET, there is this option in the project settings that allows you to produce both assembly and binary along with the source code. I found it under C/C++ -> Output Files in the project settings. You can get there by hitting Alt + F7.
Here is the output I got for a two line program. I guess a lot of this is garbage like you pointed out

This is the source code

int main(int argc, char* argv[])
      int i = 10;
      int j = i + 20;
      return 0;

and here is the output of the assembly and machine code. I guess if you need a count you can look at the statements. It gives you the relevant assembly. I am not sure of how much assembly a 1000 line code will produce

PUBLIC      _main
EXTRN      __RTC_InitBase:NEAR
EXTRN      __RTC_Shutdown:NEAR
;      COMDAT rtc$IMZ
; File c:\education\sample\sample.cpp
rtc$IMZ      SEGMENT
__RTC_InitBase.rtc$IMZ DD FLAT:__RTC_InitBase
rtc$IMZ      ENDS
;      COMDAT rtc$TMZ
rtc$TMZ      SEGMENT
__RTC_Shutdown.rtc$TMZ DD FLAT:__RTC_Shutdown
; Function compile flags: /Odt /RTCsu /ZI
rtc$TMZ      ENDS
;      COMDAT _main
_j$ = -20                                    ; size = 4
_i$ = -8                                    ; size = 4
_argc$ = 8                                    ; size = 4
_argv$ = 12                                    ; size = 4
_main      PROC NEAR                              ; COMDAT

; 6    : {

  00000      55             push       ebp
  00001      8b ec             mov       ebp, esp
  00003      81 ec d8 00 00
      00             sub       esp, 216            ; 000000d8H
  00009      53             push       ebx
  0000a      56             push       esi
  0000b      57             push       edi
  0000c      8d bd 28 ff ff
      ff             lea       edi, DWORD PTR [ebp-216]
  00012      b9 36 00 00 00       mov       ecx, 54                  ; 00000036H
  00017      b8 cc cc cc cc       mov       eax, -858993460            ; ccccccccH
  0001c      f3 ab             rep stosd

; 7    :       int i = 10;

  0001e      c7 45 f8 0a 00
      00 00             mov       DWORD PTR _i$[ebp], 10      ; 0000000aH

; 8    :       int j = i + 20;

  00025      8b 45 f8       mov       eax, DWORD PTR _i$[ebp]
  00028      83 c0 14       add       eax, 20                  ; 00000014H
  0002b      89 45 ec       mov       DWORD PTR _j$[ebp], eax

; 9    :       return 0;

  0002e      33 c0             xor       eax, eax

; 10   : }

  00030      5f             pop       edi
  00031      5e             pop       esi
  00032      5b             pop       ebx
  00033      8b e5             mov       esp, ebp
  00035      5d             pop       ebp
  00036      c3             ret       0
_main      ENDP
A better question might be: Why do you want to know the number of bytes?

The number of bytes isnt a very significant metric AFAICS.

One way of directly measuring the number of bytes of code is to add two dummy functions to your code:

void BeginCode( void ) {  }

// put all your code you want to count here....

void EndCodde( void ) { }

int CodeLength = (int) EndCode - (int) BeginCode;

prateek_shriAuthor Commented:
I need "number of machine instructions" for my code, which needs to be sent to one of my client. They makes chipsets and we would like our code to be inside their chip. But for this they have asked how many number of instructions does your code have.

Now, I tried this one given by grg99.

I put void BeginCode( void ) {  } just after my header files #includes, then all my code (like 6 functions) below, then void EndCode( void ) { }, and then at the last, my main() function.

Inside the main function, I wrote int CodeLength = (int) EndCode - (int) BeginCode;

but the value of CodeLength remains ZERO.

Am I doing it the right way?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.