|
发表于 2020-2-25 21:46:45
|
显示全部楼层
你的操作是赋值,不是判断呀,判断是用==来对比
我用VC6.0反编译9: int a = 1;
0040D748 C7 45 FC 01 00 00 00 mov dword ptr [ebp-4],1
10: int b = 3;
0040D74F C7 45 F8 03 00 00 00 mov dword ptr [ebp-8],3
11: (a = 0) && (b = 5);
0040D756 C7 45 FC 00 00 00 00 mov dword ptr [ebp-4],0
0040D75D 83 7D FC 00 cmp dword ptr [ebp-4],0
0040D761 74 07 je main+3Ah (0040d76a)
0040D763 C7 45 F8 05 00 00 00 mov dword ptr [ebp-8],5
12: printf("a = %d, b = %d\n", a, b);
0040D76A 8B 45 F8 mov eax,dword ptr [ebp-8]
0040D76D 50 push eax
0040D76E 8B 4D FC mov ecx,dword ptr [ebp-4]
0040D771 51 push ecx
0040D772 68 AC 2F 42 00 push offset string "a = %d, b = %d\n" (00422fac)
0040D777 E8 24 39 FF FF call printf (004010a0)
0040D77C 83 C4 0C add esp,0Ch
13: (a = 3) && (b = 5);
0040D77F C7 45 FC 03 00 00 00 mov dword ptr [ebp-4],3
0040D786 83 7D FC 00 cmp dword ptr [ebp-4],0
0040D78A 74 07 je main+63h (0040d793)
0040D78C C7 45 F8 05 00 00 00 mov dword ptr [ebp-8],5
14: printf("a = %d, b = %d\n", a, b);
0040D793 8B 55 F8 mov edx,dword ptr [ebp-8]
0040D796 52 push edx
0040D797 8B 45 FC mov eax,dword ptr [ebp-4]
0040D79A 50 push eax
0040D79B 68 AC 2F 42 00 push offset string "a = %d, b = %d\n" (00422fac)
0040D7A0 E8 FB 38 FF FF call printf (004010a0)
0040D7A5 83 C4 0C add esp,0Ch
15: (a = 0) || (b = 4);
0040D7A8 C7 45 FC 00 00 00 00 mov dword ptr [ebp-4],0
0040D7AF 83 7D FC 00 cmp dword ptr [ebp-4],0
0040D7B3 75 07 jne main+8Ch (0040d7bc)
0040D7B5 C7 45 F8 04 00 00 00 mov dword ptr [ebp-8],4
16: printf("a = %d, b = %d\n", a, b);
0040D7BC 8B 4D F8 mov ecx,dword ptr [ebp-8]
0040D7BF 51 push ecx
0040D7C0 8B 55 FC mov edx,dword ptr [ebp-4]
0040D7C3 52 push edx
0040D7C4 68 AC 2F 42 00 push offset string "a = %d, b = %d\n" (00422fac)
0040D7C9 E8 D2 38 FF FF call printf (004010a0)
0040D7CE 83 C4 0C add esp,0Ch
17: (a = 1) || (b = 6);
0040D7D1 C7 45 FC 01 00 00 00 mov dword ptr [ebp-4],1
0040D7D8 83 7D FC 00 cmp dword ptr [ebp-4],0
0040D7DC 75 07 jne main+0B5h (0040d7e5)
0040D7DE C7 45 F8 06 00 00 00 mov dword ptr [ebp-8],6
18: printf("a = %d, b = %d\n", a, b);
0040D7E5 8B 45 F8 mov eax,dword ptr [ebp-8]
0040D7E8 50 push eax
0040D7E9 8B 4D FC mov ecx,dword ptr [ebp-4]
0040D7EC 51 push ecx
0040D7ED 68 AC 2F 42 00 push offset string "a = %d, b = %d\n" (00422fac)
0040D7F2 E8 A9 38 FF FF call printf (004010a0)
0040D7F7 83 C4 0C add esp,0Ch
它这个短路好像是 &&或者 || 的左值与0进行一个比较,也就是a进行判断,其实a无非就两种可能0与非0,0是false 非0是true
然后根据
逻辑与 &&
&& 操作符的左操作数总是首先进行求值, 如果它的值为真, 则继续计算右操作数的值, 然后执行与操作得到表达式结果; 如果它的值为假, 根据与操作 有假则假 的性质可以断定该表达式的值为假, 所以不再计算右操作数的值.
逻辑或 ||
|| 操作符的左操作数也是首先进行求值, 如果它的值为假, 则继续计算右操作数的值, 然后执行与操作得到表达式结果; 如果它的值为真, 根据或操作 有真则真 的性质可以断定该表达式的值为真, 所以不再计算右操作数的值. (a = 0) && (b = 5); //a=0 假 短路返回
printf("a = %d, b = %d\n", a, b);
(a = 3) && (b = 5); //a=3 真 不短路
printf("a = %d, b = %d\n", a, b);
(a = 0) || (b = 4); //a=0 假 不短路
printf("a = %d, b = %d\n", a, b);
(a = 1) || (b = 6); //a=0 假 短路返回
printf("a = %d, b = %d\n", a, b);
|
|