码迷,mamicode.com
首页 > 编程语言 > 详细

汇编语言-判断素数:找出前100以内的素数

时间:2014-05-24 04:23:06      阅读:450      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   c   code   java   

1. 题目:找出前100个素数。

2. 要求:将前100个素数找出并显示值,要求每行显示5个素数。

 

先列出求100以内素数的C++代码

然后根据C++代码写汇编代码,相对来说比较容易。

bubuko.com,布布扣
 1 //The program is to find the primes from 1 to 100 with C++
 2 //By Karllen
 3 //time: 05/19/2014
 4 
 5 #include <iostream>
 6 int main(void)
 7 {
 8 
 9     int number = 2;
10     int flag   = 1;
11     while (number<=100)
12     {
13         int i = 2;
14         while (i<=number)
15         {
16             if (number%i==0)
17             {
18                 break;
19             }
20             else
21             {
22                 ++i;
23             }
24         }
25         if (i==number)
26         {
27             
28             std::cout<<number<< " ";
29             if (flag%5==0)
30             {
31                 std::cout<<std::endl;
32             }
33             ++flag;    
34         }
35         ++number;
36     }
37     system("pause");
38     return 0;
39 }
bubuko.com,布布扣

汇编代码如下:

bubuko.com,布布扣
 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    
18                ; reserve storage for data
19         mm      DWORD  ?
20         number  DWORD  ?
21         flai    DWORD  ?
22         flaj    DWORD  ?
23         promot  BYTE  "The programer is to find all prime from 1 to 100 ",cr,Lf,0
24         answer  BYTE  "All prime is "
25                 BYTE  cr,Lf,0 
26         crlf    BYTE  cr,Lf,0  
27         value   BYTE   11 DUP(?) 
28 
29 .CODE                           ; start of main program code
30 _start:
31         output promot
32         mov    mm,5
33         mov    number,2
34         mov    ecx,2
35         mov    flaj,0        ;计数,统计素数个数
36         doWhile:
37                mov flai,2
38                mov ecx,number
39                cmp ecx,100
40                jg  endWhile
41                doFindWhile:
42                    cmp  flai,ecx
43                    je   FindPrime
44                         mov eax,ecx
45                         cdq
46                         idiv flai
47                         cmp edx,0
48                         je  endFindWhile
49                            
50                         inc flai
51                         jmp doFindWhile  
52                         
53                  FindPrime: 
54                         mov eax,flaj
55                         cdq
56                         idiv mm
57                         cmp edx,0
58                         jne  PrintNoCrlf      
59                         output crlf
60                  printNoCrlf:
61                         dtoa value,ecx
62                         output value
63                         inc flaj
64                         
65            endFindWhile:   
66                inc number
67                jmp doWhile
68         endWhile:
69         
70         INVOKE  ExitProcess, 0  ; exit with return code 0
71 
72 PUBLIC _start                   ; make entry point public
73 
74 END                             ; end of source code
bubuko.com,布布扣

 测试结果:

bubuko.com,布布扣

 

汇编语言-判断素数:找出前100以内的素数,布布扣,bubuko.com

汇编语言-判断素数:找出前100以内的素数

标签:style   class   blog   c   code   java   

原文地址:http://www.cnblogs.com/Forever-Kenlen-Ja/p/3736586.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!