In Win32 compilation, we often have to deal with, and Api, another will often use their own prepared with parameters similar to Api's subroutines, this article is to describe the process in the subroutine call parameter passing concepts and analysis. General in the program, passing parameters is carried out through the stack, that is, the caller to be passed to the subroutine (or callee) parameters onto the stack, subroutine stack out the corresponding value in another use, such as that if you want to call SubRouting (Var1, Var2, Var3), compiled the final code may be
push Var3
push Var2
push Var1
call SubRouting
add esp, 12
That is, first of all the parameters of the caller onto the stack, then call the subroutine, in complete, as the stack number of the previous indentation is no longer useful, the caller or the callee must be a party to the stack pointer before the call to amend the state. Parameter is the first into the far right or far left of the first stack into the stack, as well as by the caller or the callee to modify the stack must have agreed, or will produce incorrect results, that is, before I use "may" The reason the word: language in the agreement calling subroutines are different, their differences as follows:
C SysCall StdCall Basic Fortran Pascal
Parameter from left to right yes yes yes
Parameters from right to left yes yes yes
The caller stack is cleared
Allows the use of: VARARG yes yes yes
VARARG that the number of parameters can be uncertain, one example is the C in the printf statement, in the table, StdCall definitions have a place to show that if StdCall use: VARARG when cleared by the caller stack, but in the absence of: VARARG when removed from the stack by the caller.
In Win32 compilation, have agreed to StdCall way, so we have to be used when the program started. Model stdcall statement. In other words, the API or subroutine, the most right parameters into the stack first, then return when the subroutine is responsible for correction in the stack, for example, if we want to call the MessageBox in this API, because it is defined as MessageBox (hWnd, lpText , lpCaption, UType) so in the program to be used this way:
push MB_OK
push offset szCaption
push offset szText
push hWnd
call MessageBox
...
We do not have the time to return with an API add sp, 4 * 4 to fix the stack, because this has been done by the MessageBox in this subroutine. In the Windows API, the only special API is wsprintf, the API is a C agreement, which is defined wsprintf (lpOut, lpFormat, Var1, Var2 ...), so we should use:
push 1111
push 2222
push 3333
push offset szFormat
push offset szOut
call wsprintf
add esp, 4 * 5
The following is the subroutine talk about how to access parameters, because the default operation of the register on the stack have ESP and EBP, and ESP is the stack pointer, can not be borrowed to use, so the general use of EBP to access the stack, assuming that a call There are two parameters, but also push the first argument before the stack pointer ESP is X, then press into the two parameters of the ESP for the X-8, process started call instruction, call instruction the return address onto the stack, this time ESP for the XC, then already in subroutine, we can begin to access the parameters using the EBP, but to restore order to return the value of EBP, we then need a push ebp-come, first saved EBP value, then ESP for the X-10, then the implementation of a mov ebp, esp, under the right picture can be seen, in fact, at this time [ebp + 8] is the parameter 1, [ebp + c] is the parameter 2. In addition, the local variable is defined in the stack, and their general location on the push ebp saved EBP value in the back of the address of local variables corresponding to 1,2, respectively [ebp-4], [ebp-8], the following is A typical subroutine, you can complete the first argument minus the second argument, which is defined as:
MyProc proto Var1, Var2; has two parameters
local lVar1, lVar2; there are two local variables
Note that the two local variables is not actually used, just to demonstrate the use of, the specific implementation of the code is:
MyProc proc
push ebp
mov ebp, esp
sub esp, 8
mov eax, dword ptr [ebp + 8]
sub eax, dword ptr [ebp + c]
add esp, 8
pop ebp
ret 8
MyProc endp
Now an analysis of this subroutine, push ebp / mov ebp, esp a routine save and set the EBP of the code, sub esp, 8 in the stack, leaving the space of two local variables, mov / add to complete adding the statement, add esp, 8 modified to use two local variables, stack, ret 8 modified to use the stack of two parameters, the equivalent of ret / add esp, 8 2 code results. Can see, this is a standard Stdcall agreed subroutine, use the last parameter before the stack, return stack by the subroutine for amendment. Of course, this subroutine to demonstrate the implementation process, using the manual saved ebp and set the local variable method, in fact, there are two dedicated 386-processor instruction is used to complete this function, that is, Enter and Leave, Enter statement function is to push ebp / mov ebp, esp / sub esp, xxx, the xxx is the Enter the, Leave the completed add esp, xxx / pop ebp function, so the above procedure can be changed to:
MyPorc proc
enter 8,0
mov eax, dword ptr [ebp + 8]
sub eax, dword ptr [ebp + c]
leave
ret 8
MyProc endp
Well, Speaking of which, the principle parameters should be clear, but also concluded that the use of Masm32 Win32 assembler compilation time, we do not need to remember [ebp + xx] address such problems, or local variables need to set aside its own calculation of stack space, as well as in ret for calculating the value to add, Masm32 macros have these well, as in Masm32, the above procedure as long as you write:
MyProc proc Var1, Var2
local lVar1, lVar2
mov eax, Var1
sub eax, Var2
ret
MyProc endp
Compiler automatically in the mov eax, Var1 Enter a statement in front plug, its parameters will be defined according to local automatically specify the number of local variables, in ret will be automatically added before a Leave, the same compiler under the parameters How much to replace ret ret xxx, the mov eax, Var1 replaced mov eax, dword ptr [ebp + 8] and so on.
Finally, using the invoke macro Masm32, in front of you can see, calling a subroutine with parameters, we need to push the parameters onto the stack, if not careful the wrong number of parameters would make the stack imbalance to make the program removed from the stack error caused the return address of unpredictable consequences, it is necessary to have a statement to accomplish the task of automatic test, invoke such a statement is, in fact, it is automatically push all the parameters, test parameters were , the type is correct, and use the call to invoke a macro, for the above push / push / call MyProc instructions, one instruction can be completed are:
invoke MyProc, Var1, Var2
Of course, when the program compiled machine code will look after you find it to be properly replaced with the same push / push / call instructions. However, before using the invoke, in order to test it for the correct parameters, you need to declare functions, as in C, as stated in the statement is:
MyProc proto: DWORD,: DWORD
Proto is the keyword statement that stated,: DWORD that the type parameter is of type double word, that there are several on several parameters, the parameters are double word Win32-based, affirming statements to write before the invoke Therefore, we usually include it in the include file, well, comprehensive look at the Masm32 parameters used in a subroutine or Api, we only need to use:
...
MyProc proto: dword,: dword
...
. Data
x dd?
y dd?
dwResult dd?
...
mov x, 1
mov y, 2
invoke MyProc x, y
mov dwResult, eax
...
On the line, how, is not very simple ah? But I can suffer, and this article took my one night ... ##%$^&(*&^(*&(^&(* ...
Recommended links:
ps3 rmvb
Infomation Online Gaming
With JSF + Spring + IBatis build a simple framework
No drag and drop the title of the window
Dell's real ambition
.SWF file
Simple Components And Libraries
"To 3Hmis Take A Chinese Name" 4999 Yuan Prize Zhengminghuodong
How Far From The Focus To The Expertise?
New Worm VBS / VBSWG.an @ MM Small Files
total video CONVERTER
MSA 1500 How to link up with the EVA 4000?
DB2 on the machine operating instructions guide
swf FILES
Baidu second home people FORMALLY announced tonight
No comments:
Post a Comment