View Number of machine instructions for my code.

Posted on 2004-08-17
Medium Priority
Last Modified: 2008-03-03
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.
Question by:prateek_shri
  • 3
  • 2
  • 2

Expert Comment

ID: 11825527
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.

Author Comment

ID: 11826166
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.

Author Comment

ID: 11826314
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.
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why


Expert Comment

ID: 11826608
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
LVL 22

Expert Comment

ID: 11829399
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;


Author Comment

ID: 11830525
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?

LVL 22

Accepted Solution

grg99 earned 200 total points
ID: 11832869
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)


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question