C + + Disassembly Learning Note (v) working forms of various arithmetic operations (4)

Source: Internet
Author: User

1. Conditional expression (? :)

Expression 1? Expression 2: Expression 3

The composition of conditional expressions should be judged first and then chosen. But this is not necessarily the case with compiler optimizations. When expression 1 is a constant, the compiler gets an answer during compilation and no conditional expression exists.

Conversion scenarios:

①: Expression 2 and expression 3 are constants and the difference between the two values is 1;

②: Other

First of all, say 3 instructions:Cmove and Cmovle,cmovne, conditional delivery instructions, there should be similar instructions, not found the information of these instructions, the following is based on the assembly code inverse push, do not know right, look Master Guide!

Cmp........

Cmove/cmovle/cmovne REG1,REG2;

Depending on the effect of the preceding instruction on the flag bit:

Cmove: If zf=0, the value of REG2 is transferred to REG1

Cmovle: If Zf!=of or zf=1, the value of REG2 is transferred to REG1

Cmovne: If zf=0, the value of REG2 is transferred to REG1.

Debug version and release version is very different, debug for debugging convenience, use is still the conditional judgment of the branch structure.

C + + source

Debug version

Release version

#include <iostream>

using namespace Std;

int main ()

{

int A, B;

Cin >> a >> b;

cout << (A = = 5? 5:6);

Ii

cout << (A = = 6? 3:10);

cout << (a <= 7? 2:11);

cout << (a 8:b);

System ("pause");

return 0;

}

#include <iostream>

using namespace Std;

int main ()

{

............. Slightly

int A, B;

Cin >> a >> b;

............... Slightly

cout << (A = = 5? 5:6);

003d5eb6 cmp DWORD ptr [a],5

003d5eba jne main+68h (03d5ec8h)

003D5EBC mov dword ptr [ebp-0e0h],5

003D5EC6 jmp main+72h (03D5ED2H)

003D5EC8 mov dword ptr [ebp-0e0h],6

003D5ED2 mov Esi,esp

003D5ED4 mov eax,dword ptr [ebp-0e0h]

003d5eda push EAX

003d5edb mov ecx,dword ptr ds:[3e10a8h]


cout << (A = = 5? 5:6);

003d5ee1 call DWORD ptr ds:[3e1094h]

003d5ee7 CMP ESI,ESP

003d5ee9 call __rtc_checkesp (03d1339h)

//②

cout << (A = = 6? 3:10);

003D5EEE cmp DWORD ptr [a],6

003d5ef2 jne main+0a0h (03d5f00h)

003D5EF4 mov dword ptr [ebp-0e0h],3

003d5efe jmp Main+0aah (03d5f0ah)

003D5F00 mov dword ptr [ebp-0e0h],0ah

003D5F0A mov Esi,esp

003D5F0C mov eax,dword ptr [ebp-0e0h]

003D5F12 push EAX

003D5F13 mov ecx,dword ptr ds:[3e10a8h]

003d5f19 call DWORD ptr ds:[3e1094h]

003D5F1F CMP ESI,ESP

003D5F21 call __rtc_checkesp (03d1339h)

cout << (a <= 7? 2:11);

003D5F26 cmp DWORD ptr [a],7

003D5F2A JG main+0d8h (03d5f38h)

003D5F2C mov dword ptr [ebp-0e0h],2

003D5F36 jmp main+0e2h (03D5F42H)

003D5F38 mov dword ptr [EBP-0E0H],0BH

003D5F42 mov Esi,esp

003D5F44 mov eax,dword ptr [ebp-0e0h]

003D5F4A push EAX

003D5F4B mov ecx,dword ptr ds:[3e10a8h]

003d5f51 call DWORD ptr ds:[3e1094h]

003D5F57 CMP ESI,ESP

003d5f59 call __rtc_checkesp (03d1339h)

cout << (a 8:b);

003D5F5E cmp DWORD ptr [a],0

cout << (a 8:b);

003d5f62 JE main+110h (03d5f70h)

003D5F64 mov dword ptr [ebp-0e0h],8

003d5f6e jmp main+119h (03d5f79h)

003D5F70 mov Eax,dword ptr [b]

003D5F73 mov dword ptr [Ebp-0e0h],eax

003D5F79 mov Esi,esp

003D5F7B mov ecx,dword ptr [ebp-0e0h]

003D5F81 push ECX

003D5F82 mov ecx,dword ptr ds:[3e10a8h]

003d5f88 call DWORD ptr ds:[3e1094h]

003D5F8E CMP ESI,ESP

003d5f90 call __rtc_checkesp (03d1339h)

System ("pause");

............ Slightly

return 0;

003d5fac XOR Eax,eax

}

.......... Slightly

#include <iostream>

using namespace Std;

int main ()

{

012612A0 Push EBP

012612A1 mov Ebp,esp

012612a3 Sub Esp,0ch

012612A6 mov eax,dword ptr ds:[01264000h]

012612AB XOR EAX,EBP

012612AD mov dword ptr [Ebp-4],eax

int A, B;

Cin >> a >> b;

012612B0 mov ecx,dword ptr ds:[1263038h]

int A, B;

Cin >> a >> b;

012612B6 Lea Eax,[b]

012612B9 push EAX

012612BA Lea Eax,[a]

012612BD push EAX

012612BE call DWORD ptr ds:[1263028h]

012612C4 mov ecx,eax

012612c6 call DWORD ptr ds:[1263028h]

//①

cout << (A = = 5? 5:6);

012612CC mov ecx,dword ptr ds:[126303ch]

012612D2 XOR Eax,eax

012612D4 cmp DWORD ptr [a],5

If a==5, the EAX is set to 0, otherwise the EAX is set to 1, then eax+5,

Results of final output eax+5

012612d8 Setne AL

012612DB Add eax,5

012612DE push EAX

012612DF call DWORD ptr ds:[1263024h]

//②

cout << (A = = 6? 3:10);

012612E5 cmp DWORD ptr [a],6

012612E9 mov ecx,3

012612EE mov eax,0ah

Based on the influence of the CMP's most ZF bit, if zf=1, then the a==6 is represented, the ECX (3) is transmitted to the EAX for output, otherwise the value of EAX is preserved (10) for output

012612F3 Cmove EAX,ECX

012612F6 mov ecx,dword ptr ds:[126303ch]

012612FC push EAX

012612FD call DWORD ptr ds:[1263024h]

cout << (a <= 7? 2:11);

01261303 cmp DWORD ptr [a],7

01261307 mov ecx,2

0126130C mov eax,0bh

The judgment here is ZF and of, if zf=1 (equal) or zf!=of (overflow, i.e. a<7), the value of ECX (2) is transferred to the EAX for output, otherwise the value of EAX (11) is preserved for output

01261311 Cmovle EAX,ECX

01261314 mov ecx,dword ptr ds:[126303ch]

0126131A push EAX

0126131B call DWORD ptr ds:[1263024h]

cout << (a 8:b);

01261321 cmp DWORD ptr [a],0

cout << (a 8:b);

01261325 mov ecx,8

0126132A mov Eax,dword ptr [b]

Collation is to judge A's true and false, to see the ZF flag bit

0126132D Cmovne EAX,ECX

01261330 mov ecx,dword ptr ds:[126303ch]

01261336 push EAX

01261337 call DWORD ptr ds:[1263024h]

System ("pause");

0126133D Push 126319Ch

01261342 call DWORD ptr ds:[12630c4h]

return 0;

}

........ Slightly

C + + Disassembly Learning Note (v) working forms of various arithmetic operations (4)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.