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
7 TITLE 32/64 bit assembly routines.
18 ;==============================================================================
19 ; Common x86/x64 - Constants and parameterless functions
20 ;==============================================================================
23 ; No common code ATM..
26 ;==============================================================================
27 IF (_WIN64 eq 0) ; 32 bit version
28 ;==============================================================================
29 ECHO === 32-BIT ASSEMBLY (ML) ===
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.
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
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.
54 ; Rotate left. Return rotated value. ;------------------------------------- x86
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 )
67 mov cl, byte ptr [esp+8] ; bits
68 mov al, byte ptr [esp+4] ; value
74 mov cl, byte ptr [esp+8] ; bits
75 mov ax, word ptr [esp+4] ; value
81 mov cl, byte ptr [esp+8] ; bits
82 mov eax, dword ptr [esp+4] ; value
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
101 ; Rotate right. Return rotated value. ;------------------------------------ x86
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 )
114 mov cl, byte ptr [esp+8] ; bits
115 mov al, byte ptr [esp+4] ; value
121 mov cl, byte ptr [esp+8] ; bits
122 mov ax, word ptr [esp+4] ; value
128 mov cl, byte ptr [esp+8] ; bits
129 mov eax, dword ptr [esp+4] ; value
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
150 ;==============================================================================
151 ELSE ; 64 bit version
152 ;==============================================================================
153 ECHO === 64-BIT ASSEMBLY (ML64) ===
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.
159 ; Fastcall is used regardless of prototype declaration!
160 ; Arguments -> RCX, RDX, R8, R9, then stack.
162 ; The four register args are backed by unused stack cells.
163 ; Ergo, after std prologue the fifth argument is at [RBP+48].
165 ; Normal fastcall stack cleanup convention (function pop args) is *not used*.
166 ; Functions end with 'ret 0' even if they had stack args.
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.
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)
182 ; Rotate left. Return rotated value. ;------------------------------------- x64
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 )
222 ; Rotate right. Return rotated value. ;------------------------------------ x64
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 )