Accueil > > > TEST DE LUCAS LEHMER
TEST DE LUCAS LEHMER
Information sur la source
Description
Ce programme permet de trouver les nombres de Mersenne premier c'est à dire de la forme 2^p-1 avec p premier. J'utilise le test de Lucas Lehmer: on definit la suite d'entiers u(n+1)=u(n)*u(n)-2, u(0)=4 alors 2^p-1 est premier si et seulement si u(p-2) est divisible par 2^p-1. La fonction TestLucas, renvoie 1 si le nombre de Mersenne est premier et 0 si il est composé.
Source
-
- ; test de Lucas Lehmer
- ; recherche des nombres premiers de la forme 2^n-1
- ; compilation avec masm32 (console)
-
- .486 ; create 32 bit code
- .model flat, stdcall ; 32 bit memory model
- option casemap :none ; case sensitive
-
- include \masm32\include\windows.inc ; always first
-
- include \masm32\include\gdi32.inc
- include \masm32\include\user32.inc
- include \masm32\include\kernel32.inc
-
- includelib \masm32\lib\gdi32.lib
- includelib \masm32\lib\user32.lib
- includelib \masm32\lib\kernel32.lib
-
-
- StdOut PROTO :DWORD
- TestPremier PROTO :DWORD
- TestLucas PROTO :DWORD
- .data
- outHandle dd 0
- p0 dd 3
- A dd 0
- B dd 0
- max dd 2000 ;valeur maximale a tester
- strFormat db "%7d ",0
- strTempsCalcul db 13,10,"Temps de calcul %d ms",13,10,0
- strInit db "2^n-1 est premier pour n=",13,10,0
- strMemErr db "pas assez de memoire",0
-
- strSortie db 32 dup (0)
- .code
-
- start:
- invoke GetStdHandle,STD_OUTPUT_HANDLE
- mov outHandle,eax
-
- invoke GetTickCount
- push eax
-
- mov ebx,max
- add ebx,31
- shr ebx,3
- and ebx,-4
- push ebx
- invoke GlobalAlloc,GMEM_FIXED,ebx
- pop ebx
- or eax,eax
- jz err
- mov A,eax
- shl ebx,1
- invoke GlobalAlloc,GMEM_FIXED,ebx
- or eax,eax
- jz err
- mov B,eax
- invoke StdOut,offset strInit
- suivant:
- invoke TestPremier,p0
- or eax,eax
- jz main1
- push esi
- push edi
- invoke TestLucas,p0
- pop edi
- pop esi
- or eax,eax
- jz main1
- invoke wsprintf,offset strSortie,offset strFormat,p0
- invoke StdOut,offset strSortie
- main1:
- add p0,2
- mov eax,max
- cmp p0,eax
- jb suivant
-
- ; on a fini
- invoke GetTickCount
- pop ebx
- sub eax,ebx
- invoke wsprintf,offset strSortie,offset strTempsCalcul,eax
- invoke StdOut,offset strSortie
- invoke ExitProcess,0
-
-
- err: invoke StdOut,offset strMemErr
- invoke ExitProcess,0
-
-
- StdOut proc chaine: DWORD
- sub esp,4
- mov ecx,chaine
- xor ebx,ebx
- SO1:
- mov al,[ebx+ecx]
- inc ebx
- or al,al
- jnz SO1
- dec ebx
- lea ecx,[ebp-4]
- invoke WriteConsole,outHandle,chaine,ebx,ecx,0
- ret
- StdOut endp
-
- TestPremier proc p: DWORD
-
- cmp p,4
- jb TP2
- mov ebx,1
- TP1: add ebx,2
- xor edx,edx
- mov eax,p
- div ebx
- or edx,edx
- jz compose
- cmp ebx,eax
- jb TP1
- TP2: mov eax,1
- ret
-
- compose:
- xor eax,eax
- ret
- TestPremier endp
-
-
-
-
- TestLucas proc p: DWORD
- p3 equ [ebp-4] ;Taille de A en octets
- p2 equ [ebp-8]
- p1 equ [ebp-12]
- n equ [ebp-16]
-
- cld
- sub esp,16
- mov eax,p
- sub eax,2
- mov n,eax
-
- pushad
- mov eax,p
- add eax,31
- shr eax,3
- and eax,-4
- mov p3,eax
- mov ecx,p
- and ecx,31
- mov p1,ecx
- mov eax,1
- shl eax,cl
- mov p2,eax
- ; A=4
- mov edi,A
- mov eax,4
- stosd
- mov ecx,p3
- shr ecx,2
- dec ecx
- jz pas
- xor eax,eax
- rep stosd
- pas:
- ; B = A*A
- mov ecx,p3
- shr ecx,1
- xor eax,eax
- mov edi,B
- rep stosd
-
- mov edi,A
- mov ecx,edi
- add ecx,p3
-
- l1: mov esi,edi
- add esi,4
- cmp esi,ecx
- jnb l2b
- mov ebx,esi
- sub ebx,A
- shl ebx,1
- sub ebx,4
- add ebx,B
-
- l0: lodsd
- mul dword ptr [edi]
- add [ebx],eax
- adc [ebx+4],edx
- jnb l2
- push ebx
- l3: add ebx,4
- inc dword ptr [ebx+4]
- jz l3
- pop ebx
- l2: add ebx,4
- cmp esi,ecx
- jb l0
- add edi,4
- jmp l1
-
- l2b: push ecx
- mov esi,B
- mov ecx,p3
- shr ecx,1
- mov edi,esi
- l2c: lodsd
- rcl eax,1
- stosd
- loop l2c
- pop ecx
-
- mov esi,A
- mov edi,B
- xor ebx,ebx
- l2d: lodsd
- mul eax
- shr ebx,1
- adc [edi],eax
- adc [edi+4],edx
- rcl ebx,1
- add edi,8
- cmp esi,ecx
- jb l2d
-
- ; B = B - 2
- mov ebx,2
- mov edi,B
- cmp [edi],ebx
- jnb l4b
- xor eax,eax
- mov ecx,p3
- shl ecx,1
- sub ecx,4
-
- add edi,4
- repe scasb
- jnz l4
- l5: mov eax,p2
- mov edi,B
- add edi,p3
- add [edi-4],eax
- inc ebx
- l4: mov edi,B
-
- l4b: sub [edi],ebx
- jnb l7
- l6: add edi,4
- dec dword ptr [edi]
- cmp dword ptr [edi],-1
- jz l6
- ;A = B mod 2^p -1
-
- l7:
- mov ecx,p1
- mov esi,B
- add esi,p3
- sub esi,4
- mov edi,A
- lodsd
- mov edx,edi
- add edx,p3
- l10:
- mov ebx,eax
- lodsd
- shrd ebx,eax,cl
- mov [edi],ebx
- add edi,4
- cmp edi,edx
- jb l10
- mov ecx,p2
- dec ecx
- sub esi,p3
- and [esi-4],ecx
- mov esi,B
- mov edi,A
- mov ecx,p3
- shr ecx,2
- clc
- l11:
- lodsd
- adc eax,[edi]
- stosd
- loop l11
- sub eax,p2
- jb l12
- mov [edi-4],eax
- mov esi,A
- sub esi,4
- l13: add esi,4
- inc dword ptr [esi]
- jz l13
-
- l12:
- dec dword ptr n
- jnz pas
-
- ;test si le reste est nul
- mov edi,A
- mov ecx,p3
- shr ecx,2
- dec ecx
- jz l21
- mov eax,-1
- repe scasd
- jnz l20
- l21:
- mov eax,p2
- dec eax
- scasd
- jz premier
-
- l20:
- mov ecx,p3
- mov edi,A
- xor eax,eax
- repe scasb
- jz premier
-
- popad
- mov eax,0
- ret
-
- premier:
- popad
- mov eax,1
- ret
-
- TestLucas endp
-
-
-
- end start
; test de Lucas Lehmer
; recherche des nombres premiers de la forme 2^n-1
; compilation avec masm32 (console)
.486 ; create 32 bit code
.model flat, stdcall ; 32 bit memory model
option casemap :none ; case sensitive
include \masm32\include\windows.inc ; always first
include \masm32\include\gdi32.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\gdi32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
StdOut PROTO :DWORD
TestPremier PROTO :DWORD
TestLucas PROTO :DWORD
.data
outHandle dd 0
p0 dd 3
A dd 0
B dd 0
max dd 2000 ;valeur maximale a tester
strFormat db "%7d ",0
strTempsCalcul db 13,10,"Temps de calcul %d ms",13,10,0
strInit db "2^n-1 est premier pour n=",13,10,0
strMemErr db "pas assez de memoire",0
strSortie db 32 dup (0)
.code
start:
invoke GetStdHandle,STD_OUTPUT_HANDLE
mov outHandle,eax
invoke GetTickCount
push eax
mov ebx,max
add ebx,31
shr ebx,3
and ebx,-4
push ebx
invoke GlobalAlloc,GMEM_FIXED,ebx
pop ebx
or eax,eax
jz err
mov A,eax
shl ebx,1
invoke GlobalAlloc,GMEM_FIXED,ebx
or eax,eax
jz err
mov B,eax
invoke StdOut,offset strInit
suivant:
invoke TestPremier,p0
or eax,eax
jz main1
push esi
push edi
invoke TestLucas,p0
pop edi
pop esi
or eax,eax
jz main1
invoke wsprintf,offset strSortie,offset strFormat,p0
invoke StdOut,offset strSortie
main1:
add p0,2
mov eax,max
cmp p0,eax
jb suivant
; on a fini
invoke GetTickCount
pop ebx
sub eax,ebx
invoke wsprintf,offset strSortie,offset strTempsCalcul,eax
invoke StdOut,offset strSortie
invoke ExitProcess,0
err: invoke StdOut,offset strMemErr
invoke ExitProcess,0
StdOut proc chaine: DWORD
sub esp,4
mov ecx,chaine
xor ebx,ebx
SO1:
mov al,[ebx+ecx]
inc ebx
or al,al
jnz SO1
dec ebx
lea ecx,[ebp-4]
invoke WriteConsole,outHandle,chaine,ebx,ecx,0
ret
StdOut endp
TestPremier proc p: DWORD
cmp p,4
jb TP2
mov ebx,1
TP1: add ebx,2
xor edx,edx
mov eax,p
div ebx
or edx,edx
jz compose
cmp ebx,eax
jb TP1
TP2: mov eax,1
ret
compose:
xor eax,eax
ret
TestPremier endp
TestLucas proc p: DWORD
p3 equ [ebp-4] ;Taille de A en octets
p2 equ [ebp-8]
p1 equ [ebp-12]
n equ [ebp-16]
cld
sub esp,16
mov eax,p
sub eax,2
mov n,eax
pushad
mov eax,p
add eax,31
shr eax,3
and eax,-4
mov p3,eax
mov ecx,p
and ecx,31
mov p1,ecx
mov eax,1
shl eax,cl
mov p2,eax
; A=4
mov edi,A
mov eax,4
stosd
mov ecx,p3
shr ecx,2
dec ecx
jz pas
xor eax,eax
rep stosd
pas:
; B = A*A
mov ecx,p3
shr ecx,1
xor eax,eax
mov edi,B
rep stosd
mov edi,A
mov ecx,edi
add ecx,p3
l1: mov esi,edi
add esi,4
cmp esi,ecx
jnb l2b
mov ebx,esi
sub ebx,A
shl ebx,1
sub ebx,4
add ebx,B
l0: lodsd
mul dword ptr [edi]
add [ebx],eax
adc [ebx+4],edx
jnb l2
push ebx
l3: add ebx,4
inc dword ptr [ebx+4]
jz l3
pop ebx
l2: add ebx,4
cmp esi,ecx
jb l0
add edi,4
jmp l1
l2b: push ecx
mov esi,B
mov ecx,p3
shr ecx,1
mov edi,esi
l2c: lodsd
rcl eax,1
stosd
loop l2c
pop ecx
mov esi,A
mov edi,B
xor ebx,ebx
l2d: lodsd
mul eax
shr ebx,1
adc [edi],eax
adc [edi+4],edx
rcl ebx,1
add edi,8
cmp esi,ecx
jb l2d
; B = B - 2
mov ebx,2
mov edi,B
cmp [edi],ebx
jnb l4b
xor eax,eax
mov ecx,p3
shl ecx,1
sub ecx,4
add edi,4
repe scasb
jnz l4
l5: mov eax,p2
mov edi,B
add edi,p3
add [edi-4],eax
inc ebx
l4: mov edi,B
l4b: sub [edi],ebx
jnb l7
l6: add edi,4
dec dword ptr [edi]
cmp dword ptr [edi],-1
jz l6
;A = B mod 2^p -1
l7:
mov ecx,p1
mov esi,B
add esi,p3
sub esi,4
mov edi,A
lodsd
mov edx,edi
add edx,p3
l10:
mov ebx,eax
lodsd
shrd ebx,eax,cl
mov [edi],ebx
add edi,4
cmp edi,edx
jb l10
mov ecx,p2
dec ecx
sub esi,p3
and [esi-4],ecx
mov esi,B
mov edi,A
mov ecx,p3
shr ecx,2
clc
l11:
lodsd
adc eax,[edi]
stosd
loop l11
sub eax,p2
jb l12
mov [edi-4],eax
mov esi,A
sub esi,4
l13: add esi,4
inc dword ptr [esi]
jz l13
l12:
dec dword ptr n
jnz pas
;test si le reste est nul
mov edi,A
mov ecx,p3
shr ecx,2
dec ecx
jz l21
mov eax,-1
repe scasd
jnz l20
l21:
mov eax,p2
dec eax
scasd
jz premier
l20:
mov ecx,p3
mov edi,A
xor eax,eax
repe scasb
jz premier
popad
mov eax,0
ret
premier:
popad
mov eax,1
ret
TestLucas endp
end start
Historique
- 23 février 2007 15:33:23 :
- modification du calcul du carré : plus rapide.
- 16 novembre 2008 16:36:16 :
- application entierement en assembleur
- 18 février 2009 16:40:51 :
- Remplacement de la macro print par la proc StdOut.
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Test souris && pause clavier [tasm] [ par merzhin ]
Bonjour voila, jutilise 2 procédure :une pour faire un test soursi (ki me donne les coordonné de la position de la souris ainsi que l'etat des buttons
structure de test an assembleur [ par 71julien ]
BonjourJe voudrais savoir comment faire pour un test entre deux données sur un pic 16f84.Viola exactement ce que je veux: si t1=
Probleme de compilation je pense [ par Merzhin79 ]
ALors c'est assez compliqué alors je vais expliquer ca point par point : 1-j'ai un bootsect en assembleur qui reste en mode reel, qui charge un progr
Instruction test [ par RM50Man ]
Bonjour, question qui pourrait paraitre debile, mais ca sert a quoi de faire par ex:test edx, edxjnz suivantLe test sera toujours égal.Mais aussi
Utilisation d'assembleur dans Visual Studio C++ 2008 [ par epineurien ]
Bonjour à tous !Je suis passé il y a quelques jours de Masm32 à Visual Studio 2008 et j'ai (comme d'hab.) quelques problêmes.Tant qu'il s'agit d'affic
[NASM avec RadASM] Erreur lors du Link : "File not found: test.res" [ par orax ]
Bonjour, j'essaie de débuter la programmation en assembleur, pour cela j'ai installé RadASM et NASM. Le problème est que quand je crée un nouveau proj
Création d'une lib sous tasm et linkage avec du code tc avec tlink [ par rdany62 ]
Bonjour, Je cherche à créer une librairie statique avec tasm et la lier avec un code écrit avec turbo c. tout ce passe bien (assemblage, compilation e
|
Derniers Blogs
[MIX10] KEYNOTE DEUXIèME JOURNéE - INTERNET EXPLORER 9, HTML5, VISUAL STUDIO 2010, ODATA[MIX10] KEYNOTE DEUXIèME JOURNéE - INTERNET EXPLORER 9, HTML5, VISUAL STUDIO 2010, ODATA par cyril
Le deuxième keynote du mix fut très riche en contenu. Internet Explorer 9 Juste un après le lancement de Internet Explorer 8, Microsoft a dévoilé les nouveautés de Internet Explorer 9. Désormais, IE supportera HTML5, SVG et CSS3. L'élément ...
Cliquez pour lire la suite de l'article par cyril CERTIFICATIONS BETA .NET 4CERTIFICATIONS BETA .NET 4 par KooKiz
Les inscriptions pour les certifications beta .NET 4 ont commencé. L'inscription est offerte pour les examens suivants : - 71-511, TS: Windows Applications Development with Microsoft .NET Framework 4 - 71-515, TS: Web Applications Development with...
Cliquez pour lire la suite de l'article par KooKiz [MIX 2010] - MICROSOFT TRANSLATOR TECHNOLOGY PREVIEW V2[MIX 2010] - MICROSOFT TRANSLATOR TECHNOLOGY PREVIEW V2 par redo
J'imagine que la plupart d'entre vous connaissent bien et utilisent le service de traduction de Google, mais connaissez-vous celui de Microsoft . Microsoft Translator ? Effectivement, Microsoft nous annoncé le lancement version 2 de la Technologie Preview...
Cliquez pour lire la suite de l'article par redo LANCEMENT EN PREVIEW DE CYCLONE LORS DES TECHDAYS 2010!LANCEMENT EN PREVIEW DE CYCLONE LORS DES TECHDAYS 2010! par MPOWARE
Toutes les vidéos de ce lancement sont en ligne!
Partie I - Intro
http://www.youtube.com/watch?v=LkQzTQ8T6CA
Partie II - Démo 1
http://www.youtube.com/watch?v=drAhYQ7lqvo
Partie III - Démo 2
http://www.youtube.com/watch?v=c8KM_1Gqybc...
Cliquez pour lire la suite de l'article par MPOWARE
Forum
RE : ASSEMBLEURRE : ASSEMBLEUR par ghuysmans99
Cliquez pour lire la suite par ghuysmans99 RE : ASSEMBLEURRE : ASSEMBLEUR par ghuysmans99
Cliquez pour lire la suite par ghuysmans99 ASSEMBLEURASSEMBLEUR par solleil
Cliquez pour lire la suite par solleil
Logiciels
Academy System (10.9.4.0)ACADEMY SYSTEM (10.9.4.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Xilisoft Convertisseur Vidéo Ultimate (5.1.39.0305)XILISOFT CONVERTISSEUR VIDéO ULTIMATE (5.1.39.0305)Xilisoft Convertisseur Vidéo Ultimate est un outil puissant de conversion vidéo, facile à utilise... Cliquez pour télécharger Xilisoft Convertisseur Vidéo Ultimate Xilisoft DVD Ripper Ultimate (5.0.64.0304)XILISOFT DVD RIPPER ULTIMATE (5.0.64.0304)Xilisoft DVD Ripper Ultimate est un logiciel excellent pour copier et convertir DVD vers presque ... Cliquez pour télécharger Xilisoft DVD Ripper Ultimate Rigs of Rods (63.3)RIGS OF RODS (63.3)c'est un jeu de multi-simulation camions,autobus voitures, avions, bateaux, hélicoptère avec défo... Cliquez pour télécharger Rigs of Rods
Comparez les prix

HTC Hero
Entre 550€ et 550€
|