uLib  User mode C/C++ extended API library for Win32 programmers.
_asmop.asm
Go to the documentation of this file.
1 ;
2 ; Low level CPU operations with no direct C/C++ alternative.
3 ; This file contains both 32 and 64 bit versions of the functions.
4 ; Author: B.O. Love Nystrom.
5 ; Public License: NNOSL
6 ;
7 TITLE 32/64 bit assembly routines.
8 
9 IFDEF RAX
10  _WIN64 = 1
11 ELSE
12  _WIN64 = 0
13  .686P
14  .XMM
15  .MODEL FLAT
16 ENDIF
17 
18 ;==============================================================================
19 ; Common x86/x64 - Constants and parameterless functions
20 ;==============================================================================
21 
22 _TEXT SEGMENT
23 ; No common code ATM..
24 _TEXT ENDS
25 
26 ;==============================================================================
27 IF (_WIN64 eq 0) ; 32 bit version
28 ;==============================================================================
29 ECHO === 32-BIT ASSEMBLY (ML) ===
30 
31 ; __cdecl
32 ; Argument passing: Right to left
33 ; Stack-maintenance: Calling function pops the stack
34 ; Name-decoration: Underscore character (_) is prefixed to names,
35 ; except when exporting __cdecl functions that use C linkage.
36 ;
37 ; __stdcall
38 ; Argument-passing: Right to left. By value, unless pointer or reference.
39 ; Stack-maintenance: Called function pops the stack.
40 ; Name-decoration: An underscore (_) is prefixed to the name. The name is
41 ; followed by the at sign (@) followed by the number of bytes (in decimal)
42 ; in the argument list. In other words, a function declared as
43 ; int func( int a, double b ); is decorated as: _func@12
44 ;
45 ; __fastcall
46 ; Argument-passing: The first two DWORD or smaller arguments are passed in
47 ; ECX and EDX registers; all other arguments are passed right to left.
48 ; Stack-maintenance: Called function pops the stack.
49 ; Name-decoration: At sign (@) is prefixed to names; an at sign followed by
50 ; the number of bytes (in decimal) in the parameter list is suffixed to names.
51 
52 _TEXT SEGMENT
53 
54 ; Rotate left. Return rotated value. ;------------------------------------- x86
55 
56 PUBLIC __rol8@8
57 PUBLIC __rol16@8
58 PUBLIC __rol32@8
59 PUBLIC __rol64@12
60 ;
61 ; byte __stdcall _rol8( byte value, byte bits )
62 ; word __stdcall _rol16( word value, byte bits )
63 ; dword __stdcall _rol32( dword value, byte bits )
64 ; qword __stdcall _rol64( qword value, byte bits )
65 ;
66 __rol8@8 PROC
67  mov cl, byte ptr [esp+8] ; bits
68  mov al, byte ptr [esp+4] ; value
69  rol al, cl
70  ret 8
71 __rol8@8 ENDP
72 
73 __rol16@8 PROC
74  mov cl, byte ptr [esp+8] ; bits
75  mov ax, word ptr [esp+4] ; value
76  rol ax, cl
77  ret 8
78 __rol16@8 ENDP
79 
80 __rol32@8 PROC
81  mov cl, byte ptr [esp+8] ; bits
82  mov eax, dword ptr [esp+4] ; value
83  rol eax, cl
84  ret 8
85 __rol32@8 ENDP
86 
87 __rol64@12 PROC
88  mov cl, byte ptr [esp+12] ; bits
89  mov eax, dword ptr [esp+4] ; value lo
90  mov edx, dword ptr [esp+8] ; value hi
91  push ebx
92  mov ebx, edx
93 
94  shld edx, eax, cl
95  shld eax, ebx, cl
96 
97  pop ebx
98  ret 12
99 __rol64@12 ENDP
100 
101 ; Rotate right. Return rotated value. ;------------------------------------ x86
102 
103 PUBLIC __ror8@8
104 PUBLIC __ror16@8
105 PUBLIC __ror32@8
106 PUBLIC __ror64@12
107 ;
108 ; byte __stdcall _ror8( byte value, byte bits )
109 ; word __stdcall _ror16( word value, byte bits )
110 ; dword __stdcall _ror32( dword value, byte bits )
111 ; qword __stdcall _ror64( qword value, byte bits )
112 ;
113 __ror8@8 PROC
114  mov cl, byte ptr [esp+8] ; bits
115  mov al, byte ptr [esp+4] ; value
116  ror al, cl
117  ret 8
118 __ror8@8 ENDP
119 
120 __ror16@8 PROC
121  mov cl, byte ptr [esp+8] ; bits
122  mov ax, word ptr [esp+4] ; value
123  ror ax, cl
124  ret 8
125 __ror16@8 ENDP
126 
127 __ror32@8 PROC
128  mov cl, byte ptr [esp+8] ; bits
129  mov eax, dword ptr [esp+4] ; value
130  ror eax, cl
131  ret 8
132 __ror32@8 ENDP
133 
134 __ror64@12 PROC
135  mov cl, byte ptr [esp+12] ; bits
136  mov eax, dword ptr [esp+4] ; value lo
137  mov edx, dword ptr [esp+8] ; value hi
138  push ebx
139  mov ebx, eax
140 
141  shrd eax, edx, cl
142  shrd edx, ebx, cl
143 
144  pop ebx
145  ret 12
146 __ror64@12 ENDP
147 
148 _TEXT ENDS
149 
150 ;==============================================================================
151 ELSE ; 64 bit version
152 ;==============================================================================
153 ECHO === 64-BIT ASSEMBLY (ML64) ===
154 
155 ; Extern C assembly routines does NOT get an added underscore with MSVC + ML64.
156 ; Hence the x64 assembly routines must be named _exactly_as_the_C_prototypes_,
157 ; or, in case of C++ class members, the mangled C++ identifiers.
158 ;
159 ; Fastcall is used regardless of prototype declaration!
160 ; Arguments -> RCX, RDX, R8, R9, then stack.
161 ;
162 ; The four register args are backed by unused stack cells.
163 ; Ergo, after std prologue the fifth argument is at [RBP+48].
164 ;
165 ; Normal fastcall stack cleanup convention (function pop args) is *not used*.
166 ; Functions end with 'ret 0' even if they had stack args.
167 ;
168 ; RAX, RCX, RDX, R8, R9, R10, R11 are considered volatile.
169 ; RBX, RBP, RDI, RSI, RSP, R12, R13, R14, and R15 are nonvolatile
170 ; and must be saved and restored by a function that use them.
171 ;
172 ; frame$ = 10h ; Offset from rbp to first shadow arg after 'enter 0,0'
173 ; x64 fastcall arguments:
174 ; rcx = arg1 (rbp+10h)
175 ; rdx = arg2 (rbp+18h)
176 ; r8 = arg3 (rbp+20h)
177 ; r9 = arg4 (rbp+28h)
178 ; [rbp+30h] = arg5
179 
180 _TEXT SEGMENT
181 
182 ; Rotate left. Return rotated value. ;------------------------------------- x64
183 
184 PUBLIC _rol8
185 PUBLIC _rol16
186 PUBLIC _rol32
187 PUBLIC _rol64
188 ;
189 ; byte __stdcall _rol8( byte value, byte bits )
190 ; word __stdcall _rol16( word value, byte bits )
191 ; dword __stdcall _rol32( dword value, byte bits )
192 ; qword __stdcall _rol64( qword value, byte bits )
193 ;
194 _rol8 PROC
195  mov al, cl ; value
196  mov cl, dl ; bits
197  rol al, cl
198  ret
199 _rol8 ENDP
200 
201 _rol16 PROC
202  mov ax, cx ; value
203  mov cl, dl ; bits
204  rol ax, cl
205  ret
206 _rol16 ENDP
207 
208 _rol32 PROC
209  mov eax, ecx ; value
210  mov cl, dl ; bits
211  rol eax, cl
212  ret
213 _rol32 ENDP
214 
215 _rol64 PROC
216  mov rax, rcx ; value
217  mov cl, dl ; bits
218  rol rax, cl
219  ret
220 _rol64 ENDP
221 
222 ; Rotate right. Return rotated value. ;------------------------------------ x64
223 
224 PUBLIC _ror8
225 PUBLIC _ror16
226 PUBLIC _ror32
227 PUBLIC _ror64
228 ;
229 ; byte __stdcall _ror8( byte value, byte bits )
230 ; word __stdcall _ror16( word value, byte bits )
231 ; dword __stdcall _ror32( dword value, byte bits )
232 ; qword __stdcall _ror64( qword value, byte bits )
233 ;
234 _ror8 PROC
235  mov al, cl ; value
236  mov cl, dl ; bits
237  ror al, cl
238  ret
239 _ror8 ENDP
240 
241 _ror16 PROC
242  mov ax, cx ; value
243  mov cl, dl ; bits
244  ror ax, cl
245  ret
246 _ror16 ENDP
247 
248 _ror32 PROC
249  mov eax, ecx ; value
250  mov cl, dl ; bits
251  ror eax, cl
252  ret
253 _ror32 ENDP
254 
255 _ror64 PROC
256  mov rax, rcx ; value
257  mov cl, dl ; bits
258  ror rax, cl
259  ret
260 _ror64 ENDP
261 
262 _TEXT ENDS
263 
264 ENDIF ; _WIN64
265 END