5
BOOL32 doStuff() { return TRUE; } 

gcc 2.95 for vxworks 5.x, compiling the above code with -O0 for 32-bit x86 generated following code:

 doStuff: 0e9de190: push %ebp 0e9de191: mov %esp,%ebp 308 return TRUE; 0e9de193: mov $0x1,%eax 0e9de198: jmp 0xe9de1a0 <doStuff+16> 312 { 0e9de19a: lea 0x0(%esi),%esi // The JMP jumps here 0e9de1a0: mov %ebp,%esp 0e9de1a2: pop %ebp 0e9de1a3: ret 

Everything looks normal until the JMP and LEA instruction. What are they for?


My guess is that it is some kind of alignment, but I am not sure about this.

I would have done something like this:

 doStuff: 0e9de190: push %ebp 0e9de191: mov %esp,%ebp 308 return TRUE; 0e9de193: mov $0x1,%eax 0e9de1XX: mov %ebp,%esp 0e9de1XX: pop %ebp 0e9de1XX: ret 0e9de1XX: fill with lea 0x0, %esi 
4
  • What code did you write that was eventually translated into the assembly generated above? You should include this information to increase the chances of getting help. Commented Aug 31, 2016 at 11:41
  • Did you compile with optimizations? What platform/OS? Commented Aug 31, 2016 at 11:42
  • Added infos hope this helps Commented Aug 31, 2016 at 11:45
  • 4
    Looking at code compiled at -O0 is a complete and utter waste of time. The compiler not only doesn't care about generating optimal code, it will sometimes generate pathologically sub-optimal code in order to make debugging easier (e.g., allow you to set breakpoints). The only disassembly you should look at is for optimized code. Commented Aug 31, 2016 at 12:28

1 Answer 1

8

lea 0x0(%esi),%esi is a long NOP, and the jmp is jumping over it. You probably have an ancient version of binutils (containing as) to go with your ancient gcc version.

So when gcc put a .p2align to align a label in the middle of the function that isn't otherwise a branch target (for some bizarre reason, but it's -O0 so it's not even supposed to be good code), the assembler made a long NOP and jumped over it.

Normally you'd only jump over a block of NOPs if there were a lot of them, especially if they were all single-byte NOPs. This is really dumb code, so stop using such crusty tools. You could try upgrading your assembler (but still using gcc2.95 if you need to). Or check that it doesn't happen at -O2 or -O3, in which case it doesn't matter.

If you have to keep using gcc2.95 for some reason, then just be aware that it's ancient, and this is part of the tradeoff you're making to keep using whatever it is that's forcing you to use it.

Sign up to request clarification or add additional context in comments.

1 Comment

With -O2 it looks much better ^^

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.