-1

Here is my c function:

bool equalA = true; for (int i = 0; i < 4; i++) { if (str[i] != 'a') { equalA = false; } } if (equalA == true) { if(str.compare(4, 6, "matches")) { printf("%s", "matches\n"); } } 

Here is the patial assembly code:

movzbl (%rax), %eax cmpb $97, %al setne %al testb %al, %al je .L5 movb $0, -981(%rbp) .L5: addl $1, -980(%rbp) jmp .L6 

The code above checks str[i] with 'a', if not equal, movb set equalA to false. If equal, jump to .L5. and continue for loop.

My question is: Shouldn't

 cmpb $97, %al je .L5 

would do the same work?

if str[i] == 'a', zflag will be set, je .L5 would take the branch. if str[i] != 'a', zflag will be cleared. je .L5 would not take the branch.

Why Compiler generates two lines of extra code after cmpb instruction?

4
  • 4
    Without optimization, it makes sense that the compiler interpreted the code very literally: make a flag, then test that flag. That's what you wrote, so that is what happened, because optimizations were off. Commented Aug 31, 2020 at 0:15
  • 4
    You told the compiler not to optimize, so it didn't. It happened to choose to materialize the bool result of the comparison into a register and then test it. It already had to store it to memory because of -O0. Commented Aug 31, 2020 at 0:18
  • 2
    @syacer please don’t assume you know who voted; you asked why the post got a downvote and someone tried to give you feedback. Don’t correlate the two. Voting is a normal action on this site, so not take it personally. Commented Aug 31, 2020 at 9:48
  • 2
    @syaet: please read our code of conduct, and leave handling behaviour to us moderators. Commented Aug 31, 2020 at 9:54

1 Answer 1

2

You are right, it should do the same. I assume you did not compile with optimizations enabled. It is hard to explain why a C compiler generated certain code. A different compiler, btw, may have generated different code. Yet another might generate this code, despite optimizations being enabled.

However, this is an extreme oversimplification, see the excellent comment by @PeterCordes below for more details about a program's optimization.

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

3 Comments

Yes, without optimization.
Enable optimizations, and very likely those extra lines will go away. The code generators of most C/C++ compilers generate some kind of "template code" with room for optimization that is left to a latter optimization phase that can handle such redudancies more easily and across multiple such "templates".
@Tom: That's an over-simplified description to the point of being inaccurate. Optimizations like this mostly happen on an internal representation of the program logic, usually an SSA form. For GCC, it's GIMPLE (SSA), then RTL for register allocation and stuff, then finally generating asm. So it's not like it turns your source into x86 asm templates and then optimizes those; it optimizes before generating asm when the program logic is in a state with fewer machine-specific and register allocation details nailed down.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.