筛选法求素数问题请教
我用筛选法求素数,能得到结果,但是只能求1-32632以内的素数,如果输入32633,会出错,求解释,谢谢。以下是我的代码(更新于2010/11/27 23:18)
出现新问题,当输入大于4889时,显示不按控制的来排列:
程序代码:
DATA SEGMENT INITIALN DW ? NUM DW 10 DUP(?) IBUF DB 10,0,9 DUP(?) OBUF DW 10 DUP(?) SQRTN DW ? INF1 DB "PLEASE INPUT A NUMBER OF RANGE:",0AH,0DH,'$' INF2 DB "THE SQRT IS:",0AH,0DH,'$' INF0 DB 0AH,0DH,'$' INF3 DB 20H,20H,'$' DATA ENDS EDATA SEGMENT ARRAY DW 32767 DUP(?) EDATA ENDS CODE SEGMENT ASSUME CS:CODE,DS:DATA,ES:EDATA START: MOV AX,DATA MOV DS,AX MOV AX,EDATA MOV ES,AX ;显示字符的宏 DSTRING MACRO STRING PUSH DX PUSH AX MOV DX,OFFSET STRING MOV AH,09H INT 21H POP AX POP DX ENDM DSTRING INF1 CALL DTOB DSTRING INF0 CALL SQRT MOV SQRTN,BX DSTRING INF2 MOV AX,BX CALL BTOAD DSTRING INF0 MOV CX,INITIALN CALL CREAT_ARRAY CALL QSS LEA SI,ARRAY MOV CX,INITIALN NEXT_SS: INC SI INC SI MOV AX,ES:[SI] CMP AX,0 JE NEXT_S_ ;不是零就把这个数输出, CALL BTOAD ;DSTRING INF3 INC BX CMP BX,12 JNE NEXT_S_ DSTRING INF0 ;每输出 12 个数换行 MOV BX,0 NEXT_S_: LOOP NEXT_SS MOV AH,4CH INT 21H ;从键盘输入小于一串数字并拼数,能拼成的数最大值是 65535 DTOB PROC PUSH BX PUSH CX LEA DX,IBUF MOV AH,0AH INT 21H MOV CL,IBUF+1 MOV CH,0 DEC CX MOV SI,OFFSET IBUF+2 MOV AL,[SI] XOR AH,AH XOR AL,30H CMP CX,0 JE DONE MOV BX,10 AGAIN: MUL BX INC SI MOV BH,[SI] XOR BH,30H ADD AL,BH ADC AH,0 XOR BH,BH LOOP AGAIN DONE: POP CX POP BX RET DTOB ENDP ;用牛顿迭代法求平方根 ,只精确到整数位 ;入口参数 ax ;出口参数 bx SQRT PROC PUSH AX PUSH CX MOV CX,11 MOV INITIALN,AX MOV BX,AX ND: MOV DX,0 DIV BX ADD AX,BX SHR AX,1 MOV BX,AX MOV AX,INITIALN LOOP ND POP CX POP AX RET SQRT ENDP ;按十进制输出 ;入口参数 ax BTOAD PROC PUSH AX PUSH BX PUSH CX PUSH DX PUSH SI XOR CX,CX XOR DX,DX MOV BX,10 DODIV: DIV BX PUSH DX INC CX XOR DX,DX CMP AX,0 JNZ DODIV MOV BX,CX SHOW_R: POP AX ADD AL,30H MOV DL,AL MOV AH,02H INT 21H LOOP SHOW_R MOV CX,6 SUB CX,BX SPACE_N: MOV DL,20H MOV AH,02H INT 21H LOOP SPACE_N POP SI POP DX POP CX POP BX POP AX RET BTOAD ENDP ;入口参数是 cx,要多大的数组就给cx传多大的值 ;array是一个数组的存放地址 CREAT_ARRAY PROC PUSH AX PUSH SI PUSH CX MOV AX,1 LEA SI,ARRAY ARRAY_: MOV ES:[SI],AX INC SI INC SI INC AX LOOP ARRAY_ POP CX POP SI POP AX RET CREAT_ARRAY ENDP ;筛选法求素数 QSS PROC MOV CX,SQRTN LEA SI,ARRAY SQRT_N: INC SI INC SI MOV BX,ES:[SI] CMP BX,0 JE SQRT_N_N PUSH CX PUSH SI MOV CX,INITIALN DIV_T_0: INC SI INC SI MOV AX,ES:[SI] CMP AX,0 JE NEXT_N MOV DX,0 DIV BX CMP DX,0 JNE NEXT_N MOV ES:[SI],WORD PTR 0 NEXT_N: LOOP DIV_T_0 POP SI POP CX SQRT_N_N: LOOP SQRT_N RET QSS ENDP CODE ENDS END START
[ 本帖最后由 mi53 于 2010-11-27 23:19 编辑 ]