3

I have a very simple test routine:

void test()
{

  int a = 15;
  int b = 17;
  int c, d;


  c = a + b;
  d = a | c;
  printf("%d", d);
}

I generate then the object file and then I disassemly the object file to see the instruction word for the ADD and OR operations as follows:

 sparc-elf-objdump -d test.o 

The resulting disassemly looks as follows:

test.o:     file format elf32-sparc

Disassembly of section .text:

00000000 <test>:
   0:   11 00 00 00     sethi  %hi(0), %o0
   4:   90 12 20 00     mov  %o0, %o0   ! 0 <test>
   8:   92 10 20 2f     mov  0x2f, %o1
   c:   82 13 c0 00     mov  %o7, %g1
  10:   40 00 00 00     call  10 <test+0x10>
  14:   9e 10 40 00     mov  %g1, %o7
  18:   01 00 00 00     nop 

As you can see, their is neither an ADD instruction nor an OR instruction to find. Anyeone an idea why this is the case? Quite confusing...

Many thanks, Jim

Jeremiah Willcock
  • 30,161
  • 7
  • 76
  • 78
Jim
  • 41
  • 1

2 Answers2

4

The compiler has optimized your code away -- only d is needed, and its value can be calculated at compile time.

Paul R
  • 208,748
  • 37
  • 389
  • 560
tgdavies
  • 10,307
  • 4
  • 35
  • 40
2

Your instruction are

 c = 15 + 17 = 32

In binary

 100000 | 001111= 101111= 0x2f

At line 8 you will see above number.

   8:   92 10 20 2f     mov  0x2f, %o1

So compiler has actually calculated at compile time to reduce instruction and thus reduce size and time needed for execution.

Zimbabao
  • 8,150
  • 3
  • 29
  • 36