-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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
syacer
  • 157
  • 6
  • 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. – harold Aug 31 '20 at 00: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`. – Peter Cordes Aug 31 '20 at 00:18
  • 2
    @syacer please [don’t assume you know who voted](https://meta.stackoverflow.com/questions/388686/why-shouldnt-i-assume-i-know-who-downvoted-my-post); 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. – Martijn Pieters Aug 31 '20 at 09:48
  • 2
    @syaet: please read our [code of conduct](https://stackoverflow.com/conduct), and leave handling behaviour to us moderators. – Martijn Pieters Aug 31 '20 at 09:54

1 Answers1

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.

Tom
  • 749
  • 4
  • 16
  • Yes, without optimization. – syacer Aug 31 '20 at 00:14
  • 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 Aug 31 '20 at 00:18
  • 2
    @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](https://en.wikipedia.org/wiki/Static_single_assignment_form) 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. – Peter Cordes Aug 31 '20 at 03:00