1

How do you get the size of a C instruction? For example I want to know, if I am working on Windows XP with Intel Core2Duo processor, how much space does the instruction:

 while(1==9)

{
  printf("test");
}   

takes?

gpuguy
  • 4,607
  • 17
  • 67
  • 125
  • 2
    It depends. Things it can depend on is the compiler you use, the _version_ of the compiler you use, optimization levels, debug information, etc. In short, there is no way of knowing. Although in the case of the code you show in your question, the size will most likely be zero, as the compiler will optimize away a loop that never happens. – Some programmer dude May 15 '13 at 08:44
  • You can use `sizeof()` for this purpose.......NOT! – Rüppell's Vulture May 15 '13 at 08:44
  • Generate the assembler code and the object code using your compiler to see what it spits out. – Ahmed Masud May 15 '13 at 08:47

5 Answers5

7

C does not have a notion of "instruction", let alone a notion of "size of an instruction", so the question doesn't make sense in the context of "programming in C".

The fundamental element of a C program is a statement, which can be arbitrarily complex.

If you care about the details of your implementation, simply inspect the machine code that your compiler generates. That one is broken down in machine instructions, and you can usually see how many bytes are required to encode each instruction.

For example, I compile the program

#include <cstdio>
int main() { for (;;) std::printf("test"); }

with

g++ -c -o /tmp/out.o /tmp/in.cpp

and disassemble with

objdump -d /tmp/out.o -Mintel

and I get:

00000000 <main>:
   0:   55                      push   ebp
   1:   89 e5                   mov    ebp,esp
   3:   83 e4 f0                and    esp,0xfffffff0
   6:   83 ec 10                sub    esp,0x10
   9:   c7 04 24 00 00 00 00    mov    DWORD PTR [esp],0x0

  10:   e8 fc ff ff ff          call   11 <main+0x11>

  15:   eb f2                   jmp    9 <main+0x9>

As you can see, the call instruction requires 5 bytes in my case, and the jump for the loop requires 2 bytes. (Also note the conspicuous absence of a ret statement.)

You can look up the instruction in your favourite x86 documentation and see that E8 is the single-byte call opcode; the subsequent four bytes are the operand.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
3

gcc has an extension that returns the address of a label as a value of type void*.

Maybe you can use that for your intents.

/* UNTESTED --- USE AT YOUR OWN RISK */

labelbegin:
while (1 == 9) {
    printf("test\n");
}
labelend:

printf("length: %d\n",
      (int)((unsigned char *)&&labelend - (unsigned char *)&&labelbegin));
pmg
  • 106,608
  • 13
  • 126
  • 198
2

You can find size of a binary code block like following (x86 target, gcc compiler):

#define CODE_BEGIN(name)  __asm__ __volatile__ ("jmp ."name"_op_end\n\t""."name"_op_begin:\n\t")
#define CODE_END(name)    __asm__ __volatile__ ("."name"_op_end:\n\t""movl $."name"_op_begin, %0\n\t""movl $."name"_op_end, %1\n\t":"=g"(begin), "=g"(end));

void func() {
    unsigned int begin, end;
    CODE_BEGIN("func");

    /* ... your code goes here ... */

    CODE_END("func");

    printf("Code length is %d\n", end - begin);
}

This technique is used by "lazy JIT" compilers that copy binary code written in high level language (C), instead of emitting assembly binary codes, like normal JIT compilers do.

Archie
  • 2,644
  • 21
  • 21
0

It's usually a job for a tracer or a debugger that includes a tracer, there are also external tools that can inspect and profile the memory usage of your program like valgrind.

Refer to the documentation of your IDE of choice or your compiler of choice.

user2384250
  • 548
  • 5
  • 13
0

To add on previous answers, there are occasions on which you can determine the size of the code pieces - look into the linker-generated map file, you should be able to see there global symbols and their size, including global functions. See here for more info: What's the use of .map files the linker produces? (for VC++, but the concept is similar for evey compiler).

Community
  • 1
  • 1
SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85