home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Window Info / PROLOG.TXT next >
Encoding:
Text File  |  2000-05-25  |  8.3 KB  |  201 lines

  1.  
  2. Data-Segment Initialization
  3.  
  4. The Windows prolog and epilog contain instructions that 
  5. initialize the DS register, setting the register to the 
  6. segment address of the application or DLL. Windows requires 
  7. callback functions, such as window, dialog box, and 
  8. enumeration procedures, to initialize the DS register 
  9. whenever they are called by Windows or an application. This 
  10. guarantees that the function accesses its own data segment 
  11. rather than the data segment of the caller. 
  12.  
  13. Exported Far Functions
  14.  
  15. The Windows prolog used with exported far functions, such as 
  16. dialog box and enumeration procedures, ensures that the DS 
  17. register receives the data segment address for the 
  18. application when Windows or an application calls the exported 
  19. function. In Windows version 3.0 and earlier, the prolog and 
  20. epilog for exported far functions have the following form: 
  21. push    ds          ; put DS in AX, take 3 bytes to do it,
  22. pop     ax          ;   so the code can be rewritten as
  23. nop                 ;   MOV AX, IMM when appropriate
  24. inc     bp          ; push odd BP to indicate this stack
  25. push    bp          ;   frame corresponds to a far CALL
  26. mov     bp, sp      ; set up BP to access arguments and
  27.                     ;   local variables
  28. push    ds          ; save DS
  29. mov     ds, ax      ; set DS to proper data segment
  30. sub     sp, const   ; allocate local storage (optional)
  31.  ...
  32. sub     bp, 2       ; restore registers
  33. mov     sp, bp
  34. pop     ds
  35. pop     bp
  36. dec     bp
  37. retf
  38. Because Windows 3.1 does not support real mode, the inc bp and 
  39. dec bp instructions are not required. Also, a variety of other 
  40. changes can be made to the prolog and epilog to improve speed 
  41. and reduce the size of the code. If a far function is part of 
  42. an application (not part of a DLL), the SS register is already 
  43. the proper value for the DS register, so calling the 
  44. ________________
  45. MakeProcInstance function is not necessary. The prolog and 
  46. epilog can be modified as follows: 
  47. push    bp          ; set up stack frame (optional)
  48. mov     bp, sp
  49. push    ds          ; save calling function's DS
  50. push    ss          ; move SS to DS
  51. pop     ds
  52.  ...
  53. pop     ds          ; restore registers
  54. pop     bp
  55. retf
  56. An alternative form of the prolog and epilog for far functions 
  57. follows: 
  58. push    bp          ; set up stack frame (optional)
  59. mov     bp, sp
  60. push    ds          ; save calling function's DS
  61. mov     ax, ss      ; move SS to DS
  62. mov     ds, ax
  63. sub     sp, const   ; (optional) allocate local storage
  64.  ...
  65. mov     ds, [bp-2]  ; restore registers
  66. leave
  67. retf
  68. Each of the variations of prolog and epilog code discussed 
  69. previously works whether or not a far function is exported. 
  70. The code can be called by an application or DLL as well as by 
  71. the system. 
  72. If an application copies the contents of the SS register to 
  73. the DS register, it doesn't need to call the ________________
  74.                                              MakeProcInstance 
  75. function to obtain a procedure-instance address before calling 
  76. an exported far function. Similarly, if a DLL moves the DGROUP 
  77. data segment to the DS register through the AX register, the 
  78. DLL doesn't need to call MakeProcInstance before calling an 
  79. exported far function. 
  80. Although window procedures for an application require this 
  81. same prolog, Windows loads the AX register before calling 
  82. these procedures. An application, therefore, never needs to 
  83. create a procedure-instance address for its window procedures. 
  84.  
  85. Nonexported Far Functions
  86.  
  87. Although not required, nonexported far functions can also 
  88. include prolog code that initializes the DS register. In this 
  89. case, it is assumed that the function is never called by 
  90. Windows or an application and that the DS register contains 
  91. the correct segment address when the function is called. The 
  92. prolog for a nonexported function has the following form: 
  93. mov     ax, ds      ; copy DS to AX
  94. nop
  95. push    bp          ; set up stack frame (optional)
  96. mov     bp, sp
  97. push    ds          ; save calling function's DS
  98. mov     ds, ax      ; move same value back to DS
  99.  ...
  100. pop     ds          ; pop same value back to DS
  101. pop     bp
  102. retf
  103. An alternative form of the prolog for a nonexported function 
  104. follows: 
  105. push    ds          ; copy DS to AX
  106. pop     ax
  107. nop
  108. push    bp          ; set up stack frame (optional)
  109. mov     bp, sp
  110. push    ds          ; save calling function's DS
  111. mov     ds, ax      ; move same value back to DS
  112.  ...
  113. pop     ds          ; pop same value back to DS
  114. pop     bp
  115. retf
  116. A compiler should not generate the preceding code by default 
  117. because it reloads the DS register with the same value two 
  118. times per far call. Loading segment registers is a slow 
  119. operation in protected mode and should be avoided as much as 
  120. possible. 
  121.  
  122. Exported Far Functions in a Dynamic-Link Library
  123.  
  124. Exported far functions in DLLs also require a prolog. The 
  125. prolog code in a DLL must generate a reference to the DGROUP 
  126. data segment. The SS register cannot be used because 
  127. execution occurs on the calling function's stack. Exported 
  128. far functions cannot use this method because fixups to DGROUP 
  129. are illegal for a multiple instance application. 
  130. The prolog and epilog for exported far functions in a DLL has 
  131. the following form: 
  132. mov     ax, DGROUP      ; get DGROUP value
  133. push    bp              ; set up stack frame (optional)
  134. mov     bp, sp
  135. push    ds              ; save calling function's DS
  136. mov     ds, ax          ; move DGROUP to DS
  137.  ...
  138. pop     ds              ; restore registers
  139. pop     bp
  140. retf
  141. Following is an alternative form of the prolog for exported 
  142. far functions in a DLL: 
  143. mov     ax, DGROUP      ; get DGROUP value
  144. push    bp              ; set up stack frame (optional)
  145. mov     bp, sp
  146. push    ds              ; save calling function's DS
  147. mov     ds, ax          ; move DGROUP to DS
  148. sub     sp, const       ; allocate local storage (optional)
  149.  ...
  150. mov     ds, [bp-2]      ; restore registers
  151. leave
  152. Windows inserts the current data segment address as the second 
  153. operand (DGROUP) of the initial mov instruction. 
  154.  
  155. Prologs in Real Mode
  156.  
  157. When Windows 3.0 and earlier is running in real mode, Windows 
  158. must walk each application stack whenever it moves or 
  159. discards segments. In particular, it must check each stack 
  160. for any segment addresses that may have been affected by the 
  161. segment operations. 
  162. To help Windows locate segment addresses associated with the 
  163. stack frames of far functions, the Windows prolog increments 
  164. the old frame pointer, contained in the BP register, before 
  165. saving it on the stack. Because all stack offsets, including 
  166. frame pointers, are expected to be word-aligned, incrementing 
  167. the BP register gives Windows a quick way to locate all far 
  168. function stack frames. 
  169. Windows only walks the stack in real mode. In protected mode, 
  170. selector values do not change even though Windows may move 
  171. and discard segments. Therefore, functions in protected mode 
  172. do not need to increment the BP register when they save it.
  173.  
  174. Prologs in Protected Mode
  175.  
  176. Although exported functions in protected-mode, 
  177. single-instance applications need to set the DS register, 
  178. these functions do not require the exported prolog described 
  179. in the previous section. Instead, they can use code similar 
  180. to that generated by the _loadds keyword of Borland C++. 
  181. The code generated by __loadds copies the data segment 
  182. selector to the DS register whenever the function is called. 
  183. Because a selector does not change value when the 
  184. corresponding segment is moved, there is no need to set the AX 
  185. register to the appropriate data segment address before 
  186. calling the function (or to mark the stack frame). 
  187.                                                    
  188.                                                    Functions 
  189. that use the _loadds code can be used as callback functions. 
  190. Because no prolog code is required, the functions do not need 
  191. to be exported when used in an application. Functions in DLLs 
  192. can also use the _loadds code. However, the functions must be 
  193. exported to ensure that other applications can link 
  194. dynamically to them. 
  195. In multiple-instance applications, the Windows prolog is 
  196. needed only for far functions called by Windows. For these 
  197. functions, procedure-instance addresses are required. The _
  198. _loadds code cannot be used in multiple-instance applications. 
  199. Instead, applications should copy the SS register to the DS 
  200. register. 
  201.