2 dovahkiinx DovahkiinX17:25 2016.01.26 questions

C++ Visual embedded in the compilation of the problem

As follows, is a use of embedded assembly to achieve the exchange of two integer program. The output result is 2,1; 2,1; 1,2; 2,1. can see Swap2 this function does not work, in the function of the two variables do exchange, but after the call a and B did not exchange, is still 2,1. Just like a pass, and no address, I'm confused.

<cstdio> #include
Swap1 void (&_int1 int, &_int2 int);
Swap2 void (&_int1 int, &_int2 int);
Main int () {
A int = 1, B = 2;
Swap1 (a, B);
Printf ("%d,%d;", a, B);
Swap2 (a, B);
Printf ("%d,%d.", a, B);
Return 0;
}
Swap1 void (&_int1 int, &_int2 int) {
C unsigned = D, _int1 = _int2;
__asm {
Ebx [c], mov;
Ebx [d], xchg;
[c] ebx, mov;
}
_int1 = c;
_int2 = d;
Printf ("%d,%d;", _int1, _int2);
}
Swap2 void (&_int1 int, &_int2 int) {
__asm {
Ebx [_int1], mov;
Ebx [_int2], xchg;
[_int1] ebx, mov;
}
Printf ("%d,%d;", _int1, _int2);
}

Ask my friends to help you to see why this is so.

5 answers

Caozhy
Caozhy   Ds   Rxr 19:32 2016.01.26
Has adopted
Swap2 21: (a, B);
Lea ECX 0040107B, [ebp-8]
Push ECX 0040107E
Lea EDX 0040107F, [ebp-4]
00401082 EDX push
00401083 @ILT+0 call (Swap2) (00401005)
00401088 ESP add, 8
Printf 22: ("\n-%d,%d., a",, B);
Mov eax DWORD, PTR [ebp-8] 0040108B
Push eax 0040108E
Mov ECX DWORD, PTR [ebp-4] 0040108F
00401092 ECX push
00401093 offset string "\n-%d,%d." (0042201c) (push)
00401098 printf call (004011d0)
Add ESP 0040109D, 0Ch

According to the above code, printf uses the function stack on the INT2 INT1, the lack of a write back action

DovahkiinX
DovahkiinX Thank you
9 months ago Reply
Caozhy
Caozhy   Ds   Rxr 19:20 2016.01.26
Void Swap2 36: (&_int1 int, &_int2 int) {
00401170 EBP push
00401171 EBP ESP, mov
00401173 ESP 40H, sub
00401176 ebx push
00401177 ESI push
00401178 EDI push
00401179 EDI [ebp-40h], lea
Mov ECX 0040117C, 10h
00401181 eax 0CCCCCCCCh, mov
00401186 STOs rep DWORD PTR [edi]
__asm 37: {
Mov ebx 38:, [_int1];
00401188 ebx DWORD, PTR [ebp+8] mov
Xchg ebx 39:, [_int2];
Xchg ebx DWORD, PTR [ebp+0Ch] 0040118B
Mov [_int1] 40:, ebx;
Mov DWORD PTR [ebp+8] 0040118E, ebx
41:}
Printf 42: ("\n2-%d,%d;", _int1, _int2);
00401191 eax DWORD, PTR [ebp+0Ch] mov
00401194 ECX DWORD, PTR [eax] mov
00401196 ECX push
00401197 EDX DWORD, PTR [ebp+8] mov
Mov eax DWORD, PTR [edx] 0040119A
Push eax 0040119C
Push offset string \n2-%d "0040119D,%d;" (00422040)
Call printf 004011A2 (004011d0)
Add ESP 004011A7, 0Ch
43:}
Void Swap1 25: (&_int1 int, &_int2 int) {
Push EBP 004010E0
Mov EBP 004010E1, ESP
Sub ESP 004010E3, 48H
Push ebx 004010E6
Push ESI 004010E7
Push EDI 004010E8
Lea EDI 004010E9, [ebp-48h]
Mov ECX 004010EC, 12h
Mov eax 004010F1, 0CCCCCCCCh
Rep STOs DWORD PTR [edi] 004010F6
Unsigned C 26: = D, _int1 = _int2;
Mov eax DWORD, PTR [ebp+8] 004010F8
Mov ECX DWORD, PTR [eax] 004010FB
Mov DWORD PTR [ebp-4] 004010FD, ECX
00401100 EDX DWORD, PTR [ebp+0Ch] mov
00401103 eax DWORD, PTR [edx] mov
00401105 DWORD mov PTR [ebp-8], eax
__asm 27: {
Mov ebx 28:, [c];
00401108 ebx DWORD, PTR [ebp-4] mov
Xchg ebx 29:, [d];
Xchg ebx DWORD, PTR [ebp-8] 0040110B
Mov [c] 30:, ebx;
Mov DWORD PTR [ebp-4] 0040110E, ebx
31:}
_int1 32: = c;
00401111 ECX DWORD, PTR [ebp+8] mov
00401114 EDX DWORD, PTR [ebp-4] mov
00401117 DWORD mov PTR [ecx], EDX
_int2 33: = d;
00401119 eax DWORD, PTR [ebp+0Ch] mov
Mov ECX DWORD, PTR [ebp-8] 0040111C
Mov DWORD PTR [eax] 0040111F, ECX
Printf 34: ("\n1-%d,%d;", _int1, _int2);
00401121 EDX DWORD, PTR [ebp+0Ch] mov
00401124 eax DWORD, PTR [edx] mov
00401126 eax push
00401127 ECX DWORD, PTR [ebp+8] mov
Mov EDX DWORD, PTR [ecx] 0040112A
Push EDX 0040112C
Push offset string \n1-%d "0040112D,%d;" (00422034)
00401132 printf call (004011d0)
00401137 ESP 0Ch, add
35:}

WinsenJiansbomber
WinsenJiansbomber   07:16 2016.01.30

The so-called delegate to fish than fish, C++ VISUAL have a good tool called File Listing, a lot of people are not, but the students and ASM are the necessary tools: Files Listing

By setting the different types of Files Listing, you can generate a corresponding COD file and the source code, which can contain the assembly code, machine code and source code. Through these content, you can clearly see the VC compiler to do some changes to the source code.

VC engineering settings

With the current ChildView.cpp as an example, the compiler will generate ChildView.cod files, open it, this is what you want:
COD

WinsenJiansbomber
WinsenJiansbomber I received a praise MVP, feel immediately in front of a shiny}:
9 months ago Reply
Caozhy
Caozhy Fabulous
9 months ago Reply
Lm_whales
Lm_whales   Rxr 22:02 2016.01.26

Manually write a swap function
Then view the disassembly, as well as automatic generation of code

WinsenJiansbomber
WinsenJiansbomber   14:24 2016.03.05

This should be called:

Swap1 (&a, &b);

User default icon Csdn
Upload medium...
Upload pictures
Insert picture

Recommended knowledge base更多>>

Hive knowledge base

Objective-C knowledge base

Block chain knowledge base