Consider the following code:
#include <stdio.h>
void foo() {
printf("Hello world
");
}
void bar() {
printf("Hello world");
}
The assembly produced by both these two functions is:
.LC0:
.string "Hello world"
foo():
mov edi, OFFSET FLAT:.LC0
jmp puts
bar():
mov edi, OFFSET FLAT:.LC0
xor eax, eax
jmp printf
Now I know the difference between puts and printf, but I find this quite interesting that gcc is able to introspect the const char* and figure out whether to call printf or puts.
Another interesting thing is that in bar
, compiler zero'ed out the return register (eax
) even though it is a void
function. Why did it do that there and not in foo
?
Am I correct in assuming that compiler 'introspected my string', or there is another explanation of this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…