1

I want to see how == is compiled in C. Thus I try a simple code:

#include <stdio.h>

int f(int a,int b) {
    return a==b; // NOTE THIS
}

int main(void) {
    int a,b,c;
    scanf("%d", &a,&b);
    printf("%d", f(a,b));
}

Here is the attempt on Godbolt. It is very counter-intuitive...

enter image description here

Question:

  1. Why instruction after unconditional jump? IMHO the sltu will never be executed.
  2. Why one single xor enough to express ==? IMHO need xor + sltu together.

Thanks!

ch271828n
  • 15,854
  • 5
  • 53
  • 88
  • 1
    Unrelated, just ignoring the fact that you're passing two `int*` to a `scanf` call whose format only demands one? – WhozCraig Apr 26 '21 at 11:02
  • 2
    As for (1), this is a *branch delay slot*. The `sltu` instruction is executed before the branch takes effect. As for (2), both `xor` and `sltu` are indeed used together, see the response to (1). – fuz Apr 26 '21 at 11:03
  • [Why does this load instruction come after a jump?](https://stackoverflow.com/q/53715539/995714), [What is the point of delay slots?](https://stackoverflow.com/q/15375084/995714) – phuclv Apr 26 '21 at 11:11
  • 2
    Also unrelated, the function is not called at all, it is inlined. However, because it is global, the compiler needed to include it. – the busybee Apr 26 '21 at 11:17
  • Related: [Tweak mips-gcc output to work with MARS](https://stackoverflow.com/q/13052444) – Peter Cordes Apr 26 '21 at 11:18
  • @WhozCraig fuz phuclv thebusybee PeterCordes Thanks friends! I fully get it. – ch271828n Apr 26 '21 at 12:09

0 Answers0