本文发表在 rolia.net 枫下论坛一个简单的问题, 答案是56 93, 或是57 94 似乎都make sense.应为这是是和编译器相关的问题;
昨天顺手加上 volatile 希望关掉优化编译, 但得到 2259的结果是无论无何都不应该算对的; 才贴上了这个问题;
各位侠哥都意识到编译器相关,或标准相关,但有些忽略这是个不能接受的结果;
忘情水哥概念入手, 坚持真理,直逼问题核心,思路正确;
猫哥艺高人胆大, google一把,就蒙对了答案------- 编译器在此处出错了!!
咱也google 一下 X86 汇编指令, 分析错误原因如下:
#include "stdio.h"
void func()
{
volatile int x=20, y=35;
x=y++ + x++;
y= ++y + ++x;
printf("%d%d ",x,y);
}
main()
{
func();
}
....
080483c4 <func>:
80483c4: 55 push %ebp <----- push stack
80483c5: 89 e5 mov %esp,%ebp <------ point to top of stack
80483c7: 83 ec 28 sub $0x28,%esp <----- allocate stack space why is 0x28 i don't know
80483ca: c7 45 fc 14 00 00 00 movl $0x14,-0x4(%ebp) <--- now we confirmed offset 0x4 is x memory space
80483d1: c7 45 f8 23 00 00 00 movl $0x23,-0x8(%ebp) <---- offset 0x8 is y memory space
80483d8: 8b 55 f8 mov -0x8(%ebp),%edx <------load Y to register edx
80483db: 8b 4d fc mov -0x4(%ebp),%ecx <------- load X to register ecx
80483de: 8d 04 0a lea (%edx,%ecx,1),%eax <----- reg eax = edx( y value) + ecx(x value) + 1 (?)
80483e1: 89 45 fc mov %eax,-0x4(%ebp) <---- reg eax load to X memory space( correct here )....
80483e4: 8d 42 01 lea 0x1(%edx),%eax <----- eax = edx + 1
80483e7: 89 45 f8 mov %eax,-0x8(%ebp) <------- reg eax load to Y memory space (valie y++)
80483ea: 8d 41 01 lea 0x1(%ecx),%eax <------eax = ecx + 1; (value 20 +1 )
80483ed: 89 45 fc mov %eax,-0x4(%ebp) <-------- load to X memory again !!!!!!!!! wrong..!!!!!!!
80483f0: 8b 45 f8 mov -0x8(%ebp),%eax
80483f3: 83 c0 01 add $0x1,%eax
80483f6: 89 45 f8 mov %eax,-0x8(%ebp)
80483f9: 8b 55 f8 mov -0x8(%ebp),%edx
80483fc: 8b 45 fc mov -0x4(%ebp),%eax
80483ff: 83 c0 01 add $0x1,%eax
8048402: 89 45 fc mov %eax,-0x4(%ebp)
8048405: 8b 45 fc mov -0x4(%ebp),%eax
8048408: 8d 04 02 lea (%edx,%eax,1),%eax
804840b: 89 45 f8 mov %eax,-0x8(%ebp)
804840e: 8b 45 f8 mov -0x8(%ebp),%eax
8048411: 8b 55 fc mov -0x4(%ebp),%edx
8048414: 89 44 24 08 mov %eax,0x8(%esp)
8048418: 89 54 24 04 mov %edx,0x4(%esp)
804841c: c7 04 24 10 85 04 08 movl $0x8048510,(%esp)
8048423: e8 d0 fe ff ff call 80482f8 <printf@plt>
8048428: c9 leave
8048429: c3 ret
...
<本文发表于: 相约加拿大:枫下论坛 www.rolia.net/forum >更多精彩文章及讨论,请光临枫下论坛 rolia.net
昨天顺手加上 volatile 希望关掉优化编译, 但得到 2259的结果是无论无何都不应该算对的; 才贴上了这个问题;
各位侠哥都意识到编译器相关,或标准相关,但有些忽略这是个不能接受的结果;
忘情水哥概念入手, 坚持真理,直逼问题核心,思路正确;
猫哥艺高人胆大, google一把,就蒙对了答案------- 编译器在此处出错了!!
咱也google 一下 X86 汇编指令, 分析错误原因如下:
#include "stdio.h"
void func()
{
volatile int x=20, y=35;
x=y++ + x++;
y= ++y + ++x;
printf("%d%d ",x,y);
}
main()
{
func();
}
....
080483c4 <func>:
80483c4: 55 push %ebp <----- push stack
80483c5: 89 e5 mov %esp,%ebp <------ point to top of stack
80483c7: 83 ec 28 sub $0x28,%esp <----- allocate stack space why is 0x28 i don't know
80483ca: c7 45 fc 14 00 00 00 movl $0x14,-0x4(%ebp) <--- now we confirmed offset 0x4 is x memory space
80483d1: c7 45 f8 23 00 00 00 movl $0x23,-0x8(%ebp) <---- offset 0x8 is y memory space
80483d8: 8b 55 f8 mov -0x8(%ebp),%edx <------load Y to register edx
80483db: 8b 4d fc mov -0x4(%ebp),%ecx <------- load X to register ecx
80483de: 8d 04 0a lea (%edx,%ecx,1),%eax <----- reg eax = edx( y value) + ecx(x value) + 1 (?)
80483e1: 89 45 fc mov %eax,-0x4(%ebp) <---- reg eax load to X memory space( correct here )....
80483e4: 8d 42 01 lea 0x1(%edx),%eax <----- eax = edx + 1
80483e7: 89 45 f8 mov %eax,-0x8(%ebp) <------- reg eax load to Y memory space (valie y++)
80483ea: 8d 41 01 lea 0x1(%ecx),%eax <------eax = ecx + 1; (value 20 +1 )
80483ed: 89 45 fc mov %eax,-0x4(%ebp) <-------- load to X memory again !!!!!!!!! wrong..!!!!!!!
80483f0: 8b 45 f8 mov -0x8(%ebp),%eax
80483f3: 83 c0 01 add $0x1,%eax
80483f6: 89 45 f8 mov %eax,-0x8(%ebp)
80483f9: 8b 55 f8 mov -0x8(%ebp),%edx
80483fc: 8b 45 fc mov -0x4(%ebp),%eax
80483ff: 83 c0 01 add $0x1,%eax
8048402: 89 45 fc mov %eax,-0x4(%ebp)
8048405: 8b 45 fc mov -0x4(%ebp),%eax
8048408: 8d 04 02 lea (%edx,%eax,1),%eax
804840b: 89 45 f8 mov %eax,-0x8(%ebp)
804840e: 8b 45 f8 mov -0x8(%ebp),%eax
8048411: 8b 55 fc mov -0x4(%ebp),%edx
8048414: 89 44 24 08 mov %eax,0x8(%esp)
8048418: 89 54 24 04 mov %edx,0x4(%esp)
804841c: c7 04 24 10 85 04 08 movl $0x8048510,(%esp)
8048423: e8 d0 fe ff ff call 80482f8 <printf@plt>
8048428: c9 leave
8048429: c3 ret
...
<本文发表于: 相约加拿大:枫下论坛 www.rolia.net/forum >更多精彩文章及讨论,请光临枫下论坛 rolia.net