x86 Assembly function return variable

Dear Friends,

Today I am going to continue writing about assembly related to function return. So basically whenever you call a function and you can expect return value which maybe returning a value from function internal process.

A function by its nature can only return one variable but if you require to return more than one variable then you should pass reference variable that link to the variable in the function caller

Lets do check return value using 1 variable. the C code will be like below

Let check the assembly code below

Main Function
Calculate Function

Following the standard convention that every return variable will be stored in EAX so that we can see that in the calclate function there is statement add eax, edx which as you know that the result of that statement will be store in the left variable where it is EAX.

We also check after the function call the next operation is to assign return value to the new varibale in main function that directly use EAX where the complete statement is mov [esp+1Ch], eax.

Lets prove it with debuger. We can see the value of EAX is now 5 where it is from 2+3. this is EAX value in calculate function

Lets check the EAX value after it return to the main function. Yes we can see EAX has values 5.

So that is it. By the standard of assembly language the return variable for function will be assigned to EAX.

I will explain the return using passing variable with reference

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s