首页 > 代码库 > 汇编语言-子函数找素数

汇编语言-子函数找素数

1. 题目:找出100~200内所有的素数。

2. 要求:将100~200内所有的素数找出并显示值,要求每行显示5个素数。

3. 要求素数的判定算法用子程序来实现,子程序在判定数是否为素数后有不同的返回参数。

技术分享
 1 ; Example assembly language program -- adds two numbers
 2 ; Author:  Karllen
 3 ; Date:    revised 05/2014
 4 
 5 .386
 6 .MODEL FLAT
 7 
 8 ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
 9 
10 INCLUDE io.h            ; header file for input/output
11 
12 cr      EQU     0dh     ; carriage return character
13 Lf      EQU     0ah     ; line feed
14 
15 .STACK  4096            ; reserve 4096-byte stack
16         
17 .DATA                   ; reserve storage for data
18        promot   BYTE   "The program is to find primes from 100 to 200",cr,Lf,0
19        cmm      BYTE   " ",0
20        crlf     BYTE   cr,Lf,0
21        number   DWORD  ?
22        flai     DWORD  ?
23        value    BYTE   11 DUP(?)
24  PUBLIC _start                   ; make entry point public      
25 .CODE                           ; start of main program code
26 _start:
27        output   promot
28        mov      number,99
29        mov      flai,0
30        
31        doFirstWhile:
32                  inc number
33                  cmp number,200
34                  jg  endFirstWhile
35                  mov   eax,number
36                  push  eax
37                  call  findPrime
38                  add   esp,4
39                  ;
40                      cmp  eax,0
41                      je   endSecond
42                      inc  flai
43                      dtoa value,number
44                      output value
45                      output cmm
46                         mov eax,flai
47                         mov ecx,5
48                         cdq
49                         idiv ecx
50                         cmp  edx,0
51                         je   printCrlf
52                         
53                  endSecond: 
54                      jmp doFirstWhile
55                  printCrlf:
56                         output crlf
57                      jmp doFirstWhile
58              
59        endFirstWhile:
60         INVOKE  ExitProcess, 0  ; exit with return code 0
61 
62 
63 findPrime PROC  NEAR32
64               push  ebp
65               mov   ebp,esp
66               
67               mov   ebx,2
68               mov   eax,[ebp+8]
69               mov   ecx,eax
70               docuWhile:
71                     cmp ebx,ecx
72                     je  found
73                     mov eax,ecx
74                     cdq
75                     idiv ebx
76                     cmp  edx,0
77                     je   noFound
78                     inc  ebx
79                     jmp  docuWhile
80               found:
81                     mov eax,1
82                     jmp endcuWhile
83               noFound:
84                     mov eax,0    
85               endcuWhile:
86               pop   ebp
87               ret
88    findPrime ENDP
89 
90 END                             ; end of source code
技术分享

测试结果

技术分享

汇编语言-子函数找素数