C++中通過指標,引用方式做傳回值的彙編程式碼分析
write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie
原始碼因為是《加密與解密》(第3版)一書中的代碼,我就不貼了,不然好像是侵犯著作權吧。見書中79面。
基本原理可以講講,其實就是一個max(int*,int*)的函數,將大的值放入第一個參數返回,原書可能是在debug下編譯的版本,我是在release下編譯的,反組譯碼結果如下:
.text:00401040 ; void __cdecl max(int *, int *)
.text:00401040 void __cdecl max(int *, int *) proc near ; CODE XREF: _main+1Dp
.text:00401040
.text:00401040 arg_0 = dword ptr 4
.text:00401040 arg_4 = dword ptr 8
.text:00401040
.text:00401040 000 mov eax, [esp+arg_4]
.text:00401044 000 mov ecx, [esp+arg_0]
.text:00401048 000 mov eax, [eax] ; 相當於C語言中的dereference操作
.text:0040104A 000 mov edx, [ecx]
.text:0040104C 000 cmp edx, eax ; Compare Two Operands
.text:0040104E 000 jge short locret_401052 ; Jump if Greater or Equal (SF=OF)
.text:00401050 000 mov [ecx], eax ; 假如dex>=eax(a>=b)直接跳到locret_401052返回,即不變
.text:00401050 ; 假如dex
.text:00401050 ; 此處相當於*ecx = eax,:)注釋扭曲了
.text:00401052
.text:00401052 locret_401052: ; CODE XREF: max(int *,int *)+Ej
.text:00401052 000 retn ; Return Near from Procedure
.text:00401052 void __cdecl max(int *, int *) endp
這裡唯一需要注意的就是
mov eax, [esp+arg_4]
mov ecx, [esp+arg_0]
mov eax, [eax]
mov edx, [ecx]
這4句了,先將一個地址傳入,eax/ecx,然後通過[eax]/[ecx]取值,就相當於dereference的操作,即相當於*操作符的作用。
write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie