Solved

View Number of machine instructions for my code.

Posted on 2004-08-17
9
213 Views
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.
0
Comment
Question by:prateek_shri
  • 3
  • 2
  • 2
9 Comments
 
LVL 1

Expert Comment

by:pearljam145
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.
0
 

Author Comment

by:prateek_shri
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.
0
 

Author Comment

by:prateek_shri
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.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Expert Comment

by:pearljam145
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
_TEXT      SEGMENT
_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
_TEXT      ENDS
END
0
 
LVL 22

Expert Comment

by:grg99
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;

0
 

Author Comment

by:prateek_shri
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?

Thanks
0
 
LVL 22

Accepted Solution

by:
grg99 earned 50 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)

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now