3

Why is the assembly output of this code:

code

int 
f(int n) {
        return n % 2;
}

asm

f:
        mov     edx, edi
        shr     edx, 31
        lea     eax, [rdi+rdx]
        and     eax, 1
        sub     eax, edx
        ret

different from this one:

code

int 
g(int n) {
        return n & 1;
}

asm

g:
        mov     eax, edi
        and     eax, 1
        ret

I couldn't find anything online (maybe I didn't use the right keywords).

NOTE:

  • compiler: gcc
  • args: -O3
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847

0 Answers0