你找到门路了,此生可教。
此题给函数传入3.0和3.1得出的结果迥异得让C老手都感到惊异;此时,从汇编代码看看方能很快明白其个中道理。
此题给函数传入3.0和3.1得出的结果迥异得让C老手都感到惊异;此时,从汇编代码看看方能很快明白其个中道理。
以下是引用playmyself在2010-7-29 12:54:15的发言:
080483b4 <sqr>:
double sqr(x) {
80483b4: 55 push %ebp
80483b5: 89 e5 mov %esp,%ebp
80483b7: 83 ec 04 sub $0x4,%esp #这里双精度应该偏移8个就对了
return (x*x);
80483ba: 8b 45 08 mov 0x8(%ebp),%eax
80483bd: 0f af 45 08 imul 0x8(%ebp),%eax
80483c1: 89 45 fc mov %eax,-0x4(%ebp)
80483c4: db 45 fc fildl -0x4(%ebp)
}
80483c7: c9 leave
80483c8: c3 ret
080483c9 <main>:
int main(void)
{
80483c9: 55 push %ebp
80483ca: 89 e5 mov %esp,%ebp
80483cc: 83 e4 f8 and $0xfffffff8,%esp
80483cf: 83 ec 18 sub $0x18,%esp
double ret;
ret = sqr(3.0);
80483d2: dd 05 b0 84 04 08 fldl 0x80484b0
80483d8: dd 1c 24 fstpl (%esp)
80483db: e8 d4 ff ff ff call 80483b4 <sqr>
80483e0: dd 5c 24 10 fstpl 0x10(%esp)
return 0;
80483e4: b8 00 00 00 00 mov $0x0,%eax
}开始以为是9,调试了下的确是0.
080483b4 <sqr>:
double sqr(x) {
80483b4: 55 push %ebp
80483b5: 89 e5 mov %esp,%ebp
80483b7: 83 ec 04 sub $0x4,%esp #这里双精度应该偏移8个就对了
return (x*x);
80483ba: 8b 45 08 mov 0x8(%ebp),%eax
80483bd: 0f af 45 08 imul 0x8(%ebp),%eax
80483c1: 89 45 fc mov %eax,-0x4(%ebp)
80483c4: db 45 fc fildl -0x4(%ebp)
}
80483c7: c9 leave
80483c8: c3 ret
080483c9 <main>:
int main(void)
{
80483c9: 55 push %ebp
80483ca: 89 e5 mov %esp,%ebp
80483cc: 83 e4 f8 and $0xfffffff8,%esp
80483cf: 83 ec 18 sub $0x18,%esp
double ret;
ret = sqr(3.0);
80483d2: dd 05 b0 84 04 08 fldl 0x80484b0
80483d8: dd 1c 24 fstpl (%esp)
80483db: e8 d4 ff ff ff call 80483b4 <sqr>
80483e0: dd 5c 24 10 fstpl 0x10(%esp)
return 0;
80483e4: b8 00 00 00 00 mov $0x0,%eax
}开始以为是9,调试了下的确是0.