C++中通過指標,引用方式做傳回值的彙編程式碼分析

來源:互聯網
上載者:User
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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.