; TITLE prime number tester ; Version is 32-bit only, so it uses 32-bit calling conventions ; and has access only to the standard 8 x86 integer registers SECTION .data max equ 10000000 cacheSize equ 1000 cache: TIMES cacheSize dd 0 count: dd 0 printStr: db "%d",10,0 SECTION .text global main extern printf asmCheckPrimes: push ebp mov ebp, esp sub esp, 4 pushad mov ecx, 1 mov edx, 1 mov ebx, [ebp+16] mov esi, [ebp+12] mov edi, esi add edi, 4*cacheSize mov DWORD [esi], 2 add esi, 4 TOP: add ecx, 2 cmp ecx, ebx jge DONE sub esp, 8 push ecx push DWORD [ebp+12] push esi call asmIsPrime add esp, 20 test eax,1 jz TOP inc edx cmp esi, edi jge TOP mov [esi], ecx add esi, 4 jmp TOP DONE: mov [ebp-4], edx popad pop eax pop ebp ret asmIsPrime: push ebp mov ebp, esp sub esp, 4 pushad mov esi, [ebp+16] mov ebx, esi TOPSQRT: mov eax, esi xor edx, edx div ebx add eax, ebx shr eax, 1 mov ecx, eax sub eax, ebx cdq xor eax, edx sub eax, edx mov ebx, ecx cmp eax, 1 jg TOPSQRT mov ecx, 1 mov edx, 1 mov esi, [ebp+12] mov edi, [ebp+8] TOPCACHELOOP: cmp esi, edi je TOPFINDPRIME mov ecx, [esi] add esi, 4 cmp ecx, ebx jg ISAPRIME mov eax, [ebp+16] xor edx, edx div ecx test edx, 0FFFFFFFFh jz NOTAPRIME jmp TOPCACHELOOP TOPFINDPRIME: add ecx, 2 cmp ecx, ebx jg ISAPRIME mov eax, [ebp+16] xor edx, edx div ecx test edx, 0FFFFFFFFh jz NOTAPRIME jmp TOPFINDPRIME ISAPRIME: mov DWORD [ebp-4], 1 jmp PRIMEDONE NOTAPRIME: mov DWORD [ebp-4], 0 PRIMEDONE: popad pop eax pop ebp ret main: mov eax, max push eax push cache push count call asmCheckPrimes add esp, 8 push eax push printStr call printf add esp, 12 ret