I was observing assembly generated for an equal program in Rust and C in the Compiler Explorer, in "binary" mode to look at the whole linked executable, not just compiler generated assembly for specific functions.
pub fn square(num: i32) -> i32 {
num * num
}
fn main () {
let n = 4;
let _x = square(n);
}
int square(int num) {
return num * num;
}
int main() {
int n = 4;
int x = square(n);
}
But the disassembly generated for these two are very different. I can partially understand the assembly generated for the C program, that is much shorter and more understandable for me. But I can't understand anything from assembly generated for Rust program.
So my question is why there is so much difference in length of these assembly programs? Am I using compiler explorer in a wrong way?
The Rust one seems to be showing a bunch of code from the Rust standard library.
This is simply a problem with Compiler Explorer. It knows how to hide the C standard library but it doesn't know how to hide the Rust one. You can see that if you click on "Filter", with the Rust program the option to hide library functions is greyed out.
Another option is to uncheck "Compile to binary". Then, the compiler will only compile your code and disassemble it before linking in the standard library. The code might be slightly different as it won't have been linked. (Thanks to Stargateur for this suggestion)