comparison dyncall/dyncall_call_x86_nasm.asm @ 0:3e629dc19168

initial from svn dyncall-1745
author Daniel Adler
date Thu, 19 Mar 2015 22:24:28 +0100
parents
children 7520e2260097
comparison
equal deleted inserted replaced
-1:000000000000 0:3e629dc19168
1 ;//////////////////////////////////////////////////////////////////////////////
2 ;
3 ; Copyright (c) 2007,2009 Daniel Adler <dadler@uni-goettingen.de>,
4 ; Tassilo Philipp <tphilipp@potion-studios.com>
5 ;
6 ; Permission to use, copy, modify, and distribute this software for any
7 ; purpose with or without fee is hereby granted, provided that the above
8 ; copyright notice and this permission notice appear in all copies.
9 ;
10 ; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 ; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 ; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 ; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 ; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 ;
18 ;//////////////////////////////////////////////////////////////////////////////
19
20 ;///////////////////////////////////////////////////////////////////////
21 ;
22 ; dyncall_call_x86_nasm.nasm
23 ;
24 ; X86 Calls for nasm assembler
25 ;
26 ;///////////////////////////////////////////////////////////////////////
27
28
29 BITS 32
30 section .text
31
32 ;///////////////////////////////////////////////////////////////////////
33 ; CSYM macro
34 ;///////////////////////////////////////////////////////////////////////
35
36 %ifdef BUILD_OS_windows
37
38 %macro EXPORT_C 1
39 global _%1
40 _%1:
41 %endmacro
42
43 %else
44
45 %macro EXPORT_C 1
46 global %1
47 %1:
48 %endmacro
49
50 %endif
51
52 ; -----------------------------------------------------------------------------
53 ; Calling Convention x86 standard C
54 ; - all arguments are on the stack
55 ; - caller cleans up stack
56 ;
57 ; C proto
58 ; dcCallC(DCptr funptr, DCptr args, DCsize size)
59 ; -----------------------------------------------------------------------------
60
61 EXPORT_C dcCall_x86_cdecl
62
63 push ebp ; prolog
64 mov ebp, esp
65
66 ; arguments:
67 ;
68 ; funptr ebp+8
69 ; args ebp+12
70 ; size ebp+16
71 ; result ebp+20
72
73 push esi ; save esi, edi
74 push edi
75
76 mov esi, [ebp+12] ; esi = pointer on args
77 mov ecx, [ebp+16] ; ecx = size
78
79 sub esp, ecx ; cdecl call: allocate 'size' bytes on stack
80 mov edi, esp ; edi = stack args
81
82 rep movsb ; copy arguments
83
84 call [ebp+8] ; call function
85
86 add esp, [ebp+16] ; cdecl call: cleanup stack
87
88 pop edi ; restore edi, esi
89 pop esi
90
91 mov esp, ebp ; epilog
92 pop ebp
93
94 ret
95
96 ; -----------------------------------------------------------------------------
97 ; Calling Convention x86 microsoft thiscall
98 ; - thispointer is in ECX, rest is on the stack
99 ; - callee cleans up stack
100 ;
101 ; C proto
102 ; dcCallThisMS(DCptr funptr, DCptr args, DCsize size)
103 ; -----------------------------------------------------------------------------
104 EXPORT_C dcCall_x86_win32_msthis
105
106 push ebp ; prolog
107 mov ebp, esp
108
109 ; arguments:
110 ;
111 ; funptr ebp+8
112 ; args ebp+12
113 ; size ebp+16
114
115 push esi ; save esi, edi
116 push edi
117
118 mov esi, [ebp+12] ; esi = pointer on args
119 mov ecx, [ebp+16] ; ecx = size
120
121 mov eax, [esi] ; eax = this pointer
122 add esi, 4 ; increment args pointer by thisptr
123 sub ecx, 4 ; decrement size by sizeof(thisptr)
124
125 sub esp, ecx ; allocate argument-block on stack
126 mov edi, esp ; edi = stack args
127
128 rep movsb ; copy arguments
129
130 mov ecx, eax ; ecx = this pointer
131
132 call [ebp+8] ; call function (thiscall: cleanup by callee)
133
134 pop edi ; restore edi, esi
135 pop esi
136
137 mov esp, ebp ; epilog
138 pop ebp
139
140 ret
141
142 ; -----------------------------------------------------------------------------
143 ; Calling Convention x86 win32 stdcall
144 ; - all arguments are passed by stack
145 ; - callee cleans up stack
146 ;
147 ; C proto
148 ; dcCallStd(DCptr funptr, DCptr args, DCsize size)
149 ; -----------------------------------------------------------------------------
150 EXPORT_C dcCall_x86_win32_std
151
152 push ebp ; prolog
153 mov ebp, esp
154
155 ; arguments:
156 ;
157 ; funptr ebp+8
158 ; args ebp+12
159 ; size ebp+16
160
161 push esi ; save esi, edi
162 push edi
163
164 mov esi, [ebp+12] ; esi = pointer on args
165 mov ecx, [ebp+16] ; ecx = size
166
167 sub esp, ecx ; stdcall: allocate 'size'-8 bytes on stack
168 mov edi, esp ; edi = stack args
169
170 rep movsb ; copy arguments
171
172 call [ebp+8] ; call function (stdcall: cleanup by callee)
173
174 pop edi ; restore edi, esi
175 pop esi
176
177 mov esp, ebp ; epilog
178 pop ebp
179
180 ret
181
182 ; -----------------------------------------------------------------------------
183 ; Calling Convention x86 win32 fastcall
184 ; - first two integer (up to 32bits) are passed in ECX and EDX
185 ; - others are passed on the stack
186 ; - callee cleans up stack
187 ;
188 ; C proto
189 ; dcCallFast(DCptr funptr, DCptr args, DCsize size)
190 ; -----------------------------------------------------------------------------
191 EXPORT_C dcCall_x86_win32_fast
192
193 push ebp ; prolog
194 mov ebp, esp
195
196 ; arguments:
197 ;
198 ; funptr ebp+8
199 ; args ebp+12
200 ; size ebp+16
201
202 push esi ; save esi, edi
203 push edi
204
205 mov esi, [ebp+12] ; esi = pointer on args
206 mov ecx, [ebp+16] ; ecx = size
207 mov eax, [esi] ; eax = first argument
208 mov edx, [esi+4] ; edx = second argument
209 add esi, 8 ; increment source pointer
210 sub ecx, 8 ; decrement size by 8
211
212 sub esp, ecx ; fastcall: allocate 'size'-8 bytes on stack
213 mov edi, esp ; edi = stack args
214
215 rep movsb ; copy arguments
216
217 mov ecx, eax ; ecx = first argument
218
219 call [ebp+8] ; call function (fastcall: cleanup by callee)
220
221 pop edi ; restore edi, esi
222 pop esi
223
224 mov esp, ebp ; epilog
225 pop ebp
226
227 ret
228
229 ; Stack markings for ELF/GNU to specify no executable stack */
230
231 %ifidn __OUTPUT_FORMAT__,elf
232 section .note.GNU-stack noalloc noexec nowrite progbits
233 %endif
234