找素数

来源:互联网 发布:天猫交易额实时数据 编辑:程序博客网 时间:2024/04/28 01:22

.model small

.stack 200h


.CODE

;***************************************

;显示正整数子程序

;功能描述:以十进制方式输出任意的16位正整数

;入口参数:AX=要输出的整数

write proc

  push si

  push bx

  push cx

  push dx

  xor si,si

mov bx,10

tostring:

   xor dx,dx

div bx

add dx,30h

push dx

inc si

test ax,ax

jnz tostring

mov cx,si

display:

pop dx

mov ah,2

int 21h

loop display

pop dx

pop cx

pop bx

pop si

ret

write endp

;***************************************


;***************************************

;入口参数:AX=所要判断的数

;出口参数:BX=1或0,1代表是素数,0则不是素数

isprime proc

   push ax

   push cx

   push dx

   push si

   push di

   mov si,ax

   mov cx,2

loop1:   

   mov di,ax

   xor dx,dx

   div cx

   mov ax,di

   test dx,dx

   jz  decide

   inc cx 

   cmp cx,si

   jbe loop1

decide:   

   cmp cx,ax

   jb not_prime   

   mov bx,1

   jmp over

not_prime:   

   xor bx,bx

over:   

   pop di

   pop si

   pop dx

   pop cx

   pop ax

   ret

isprime endp

;***************************************



START:

mov ax,@data

mov ds,ax

xor si,si

mov cx,9999

loop2: 

mov ax,cx

call isprime

test bx,bx

jz next

call write

inc si

cmp si,10

jnz tab

mov dl,0ah

mov ah,2

int 21h

mov dl,0dh

mov ah,2

int 21h

tab: 

mov dl,' '

mov ah,2

int 21h

next: 

dec cx

dec cx

cmp cx,2

jg  loop2 


mov ah,1

int 21h

mov ah,4ch

int 21h


END START   

原创粉丝点击