Passing Parameters Using the Stack

Here, I'd like to explain in more detail the process of passing parameters using the stack. This isn't the only method of passing parameters; however, parameters to API functions are passed using this method. Therefore, it deserves special attention. Fig. 2.2 shows the stack before and after the procedure call.

image from book
Figure 2.2: Method of passing parameters to the procedure (the stack grows in the direction of the lower addresses)

Fig. 2.2 demonstrates the standard entry to the procedure used in high-level programming languages such as C and Pascal. When entering the procedure, the following standard sequence of commands is executed:

 PUSH  EBP    MOV   EBP, ESP    SUB   ESP, N ; N --- Number of bytes for local variables 

The address of the first parameter is defined as [EBP+8h], which you have used several times. The address of the first local variable, if it is reserved, is defined as [ebp - 4] (with the dword variable in mind). When programming in Assembly language, local variables are not convenient ; therefore, do not reserve space for them (see Chapters 11 and 12). At the end of the procedure, the following commands can be found:

 MOV ESP, EBP    POP EBP    RET M 

Here, m is the stack volume taken for passing parameters.

The same result can be achieved using the ENTER N , O ( PUSH EBP\MOV EBP, ESP\SUB ESP ) command at the starting point of the procedure and the LEAVE ( MOV ESP, EBP\POP EBP ) at its end. These commands Were first introduced for Intel's 286 processor. They gave the possibility of optimizing the translated code, especially when dealing with large modules developed using high-level programming languages.

It is necessary to mention another aspect related to the structure of the procedure and the methods used for calling it. There are two main approaches to passing parameters, also called parameter passing agreements. The first approach is conventionally called the C approach, and the second one is the Pascal approach. The first approach assumes that the procedure doesn't know how many parameters are placed into the stack. In this case, parameters must be popped from the stack after the procedure has been called. This can be achieved using the pop command or the add esp, n commands (where n stands for the number of bytes required for parameters). The second approach is based on the fact that the number of parameters is fixed; therefore, the stack can be popped within the procedure. This is achieved by executing the ret n command (where n stands for the number of bytes required for parameters). As you have probably guessed, the calls to API functions are carried out according to the second method. Nevertheless, there are exceptions from this rule, which will be considered later (see Chapter 7).



The Assembly Programming Master Book
The Assembly Programming Master Book
ISBN: 8170088178
EAN: 2147483647
Year: 2004
Pages: 140
Authors: Vlad Pirogov

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net