汇编语言-子函数找素数

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

测试结果

转载于:https://www.cnblogs.com/Forever-Kenlen-Ja/p/3741140.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值