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
[FRAMEWORK 4] LES TASKS ET LE THREAD UI[FRAMEWORK 4] LES TASKS ET LE THREAD UI par fathi
Je viens de passer quelques temps au TechDay's et j'ai pu voir pas mal de session intéressante. Par contre une chose m'a un peu étonné lors de certaines de ces sessions qui abordaient les améliorations du framework .NET (donc le 4.5) : en gros, bea...
Cliquez pour lire la suite de l'article par fathi WORKFLOW FOUNDATION 3 A UN PIED DANS LA TOMBEWORKFLOW FOUNDATION 3 A UN PIED DANS LA TOMBE par JeremyJeanson
Depuis déjà un an, je conseille vivement les utilisateurs de Workflow Foundation 3 à migrer vers la version 4. L'information qui va suivre ne devrait donc pas trop prendre au dépourvu les personnes qui m'ont suivi. Je profite de ce poste, pour faire le re...
Cliquez pour lire la suite de l'article par JeremyJeanson TECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PCTECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PC par ROMELARD Fabrice
Speakers: Thierry Rapatout, Antoine Petit et Xavier Trebbia Cette session entre dans le cadre des RDV Décideurs des TechDays 2012, elle est liée à la consumérisation de l'IT et la mise en place du "DeskTop as a Service" dans de plus en ...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLETECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLE par ROMELARD Fabrice
Speakers: Julien Marechal, Gautier Confiant, Sébastien MEYER La session débute par le positionnement de la solution System Center par rapport aux concepts d'organisation ITIL. Le portail du catalogue de se...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : PLEINIèRE SECOND JOURTECHDAYS PARIS 2012 : PLEINIèRE SECOND JOUR par ROMELARD Fabrice
Après une première journée dédiée aux développeurs, cette seconde journée est dédiée au monde des entreprises et de ses applications. Ainsi, cette pleinière est dédiée à faire un 360 de l'évolution des applications Business aux demandes ac...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|