I teach C ++ and examining one project I stumbled upon the fact that the author implemented some of the functions (core) on MASM, and I know very little about MASM, and I really didn’t find anything on the Internet on this topic. Can anyone translate these 3 functions in C ++, with comments like that and how? Then I'll figure it out myself.
The code itself on MASM ( source code , header file ):
RES equ 52 HED equ 16 ;normalizace [edi] - mantisa nebude zaинnat ani konиit na nulu ;zmмnн esi,edi norm proc cmp dword ptr [edi-12],0 jbe @@ret ;zlomek nebo nula call trim mov ecx,[edi-12] test ecx,ecx jz @@ret ;trim vэsledek vynulovalo xor edx,edx cmp [edi],edx jnz @@ret mov eax,edi @@lp: add eax,4 dec dword ptr [edi-4] jo @@nul dec ecx jz @@nul cmp [eax],edx jz @@lp @@e: mov esi,eax mov [edi-12],ecx cld rep movsd @@ret: ret @@nul: mov dword ptr [edi-12],0 ret norm endp @NORMX@4: mov eax,ecx @normx proc uses esi edi mov edi,eax call norm ret @normx endp ;alokuje инslo s mantisou dйlky eax @ALLOCX@4: mov eax,ecx @allocx proc push eax lea eax,[eax*4+HED+RES] ;vиetnм hlaviиky a rezervovanйho mнsta push eax call Alloc ; вызывает функцию из C++ - void *Alloc(int size) { return operator new(size); } pop edx pop ecx test eax,eax jz @@ret add eax,HED mov [eax-16],ecx mov dword ptr [eax-12],0 ;inicializuj na nulu mov dword ptr [eax-4],1 mov dword ptr [eax-8],0 @@ret: ret @allocx endp ALLOCN: ALLOCNX proc mov eax,[esp+8] lea eax,[eax*4+HED+RES] ;vиetnм hlaviиky a rezervovanйho mнsta push eax mul dword ptr [esp+8] push eax call Alloc pop edx pop ecx test eax,eax jz @@ret lea eax,[eax+edx+HED] @@lp: sub eax,ecx mov edx,[esp+8] mov [eax-16],edx mov dword ptr [eax-12],0 ;inicializuj na nulu mov dword ptr [eax-4],1 mov dword ptr [eax-8],0 mov edx,[esp+4] mov edx,[esp+4*edx+8] mov [edx],eax dec dword ptr [esp+4] jnz @@lp @@ret: ret ALLOCNX endp And in C ++, they are written as:
Pint __fastcall ALLOCX(Tint len); // Allocate number (len is in Tint units), initialize to zero, return pointer to mantissa Pint __cdecl ALLOCN(int n, Tint len, ...); // Allocate n numbers, vararg are variables for pointers void __fastcall NORMX(Pint x); // Normalize mantissa I will be grateful.
ALLOCXallocates memory for the base unit,FREEXfrees memory,ALLOCNallocates memory for N blocks,NORMXnormalizes the mantissa (see Normal and normalized forms ). - insolor