Functions in C # (ii) functions with parameter return values

Source: Internet
Author: User

A function in C # (-) with no parameter and no return value

Http://www.cnblogs.com/fzxiaoyi/p/8502613.html

In this study, the function in C # (ii) has a function with the return value of the parameter

Still write a small example that is used to test

Similar to the previous example, the difference is that MyFunction has two parameters, A, B, and returns a value of two numbers added

F5 Debug Run, go to disassembly after break

It's obviously a different place to see.

Here we have to talk about the way the parameters are passed, and the parameters are left-to-right in registers ecx edx

But different programming languages have different ways of passing parameters, and you can write an article about it.

If you have learned the assembly, then there is a question, if each argument is stored in the register, that exceeds the number of registers how to do?

If you have doubts, you have to test it, it's always the principle, okay, open another project, write the same function with 3 parameters.

Go to Disassembly, look, a third argument is a direct stack

What if five parameters, then write a function test

It's clear that you're here.

Summary: If the number of arguments passed is less than 2 ecx edx register used, if it is greater than 2 parameters,

Use the stack for delivery.

OK, understand the way the parameter is passed back to the first example, further analysis of the internal myfunction function

F10 single Step two times, stop at 001F2DD5

Then F11 into the function to see inside

The disassembly code here is too long, no, just paste the disassembly code

001F2DF8 push EBP//front These two sentences to save ESP
001F2DF9 mov ebp,esp//At this time EBP points to the top of the stack
001F2DFB Push EDI
001F2DFC push ESI
001F2DFD push EBX//Save the EDI esi EBX register stack

The beginning of this is the focus of our research, can be F10 stepping, passing through the register and the value of the stack

In the previous article here is sub esp,2ch, and here is 3ch

The extra 10h here is the return value address, two parameter address, a temporary variable sum address

001f2dfe Sub esp,3ch//joins the above three sentences, at this time ESP moved 0xC + 0x3C = 0x48 bytes

001F2E01 mov esi,ecx //This ecx is 1, representing the first argument, saved in ESI

001f2e03 Lea EDI,[EBP-38H]//Take address operation ESP + 0x10 = Fourth position from top of stack
001F2E06 mov ecx,0bh//This three line puts 0xb* 4 = 44 bytes of space clear 0
001f2e0b xor Eax,eax
001f2e0d Rep stos dword ptr Es:[edi]
001f2e0f mov ecx,esi//save parameter 1 in ECX
001F2E11 mov dword ptr [EBP-3CH],ECX//Save parameter 1 at [ebp-0x3c]
001F2E14 mov dword ptr [EBP-40H],EDX//Save parameter 2 at [ebp-0x40h]
001f2e17 cmp dword ptr ds:[0018c7a8h],0
001f2e1e JE 001f2e25
001f2e20 Call 71c6cb2d
001f2e25 xor Edx,edx
001f2e27 mov dword ptr [Ebp-48h],edx
001f2e2a xor Edx,edx
001F2E2C mov dword ptr [Ebp-44h],edx
001f2e2f NOP

This is where the exact calculations are.
22:int sum = a + b;  

001F2E30 mov eax,dword ptr [ebp-3ch]//Store the first parameter in a EAX
001f2e33 add Eax,dword ptr [ebp-40h]//Then via add EAX + second parameter, the result is 1 + 2 = 3
001f2e36 mov dword ptr [EBP-44H],EAX//Then put the added result in a temporary variable
001f2e39 mov eax,dword ptr [ebp-44h]//Remove the result from the TEMP variable in eax (this sentence is completely redundant)
001F2E3C mov dword ptr [EBP-48H],EAX//Put the result in the return value address. The return value is stored in the case of a return value EAX

This is the case in the current stack

The code behind this is the stack balancing process that must be done when the current function returns

001f2e3f NOP
001f2e40 jmp 001f2e42
001f2e42 mov eax,dword ptr [ebp-48h]
001f2e45 Lea Esp,[ebp-0ch]
001f2e48 pop ebx
001f2e49 pop ESI
001F2E4A Pop EDI
001F2E4B Pop EBP
001F2E4C ret

More than two parameters, also take a look at the core code

As mentioned earlier, the first two parameters are stored in the register ECX edx when the extra two parameters are stored in the stack

18:int sum = a + B + C + D;
001A34B0 mov eax,dword ptr [ebp-3ch]//eax = first parameter
001A34B3 add Eax,dword ptr [ebp-40h]//eax = eax + second parameter
001a34b6 add Eax,dword ptr [ebp+0ch]//eax + = The third parameter is taken directly from the stack without the need to open up the stack space
001a34b9 add Eax,dword ptr [ebp+8]//eax + = Fourth parameter takes no more stack space directly from the stack
001A34BC mov dword ptr [EBP-44H],EAX//Temp variable sum = First four numbers
19:return sum;
001A34BF mov eax,dword ptr [ebp-44h]//Put temporary variable sum to EAX
001A34C2 mov dword ptr [EBP-48H],EAX//Put the result in the return value address. The return value is stored in the case of a return value EAX
001A34C5 NOP
001A34C6 jmp 001a34c8

Functions in C # (ii) functions with parameter return values

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.