assembly 素数检测的汇编语言程序

f5emj3cl  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(151)

我正在尝试用汇编语言编写一个程序,以检测一个输入的数字在0-9的范围内是否为素数。我已经编写了这个程序,但即使它运行,它可能也不起作用,我不能指出错误在哪里或为什么它会这样运行。任何建议都将不胜感激。谢谢。

.MODEL  SMALL

    .DATA
           VAl1     DB      ?
           NL1      DB      0AH,0DH,'ENTER NO:','$'
           NL2      DB      0AH,0DH,'IT IS NOT PRIME','$'
           NL3      DB      0AH,0DH,'IT IS PRIME','$'

           .CODE
       ORG 0100H
    MAIN:

            MOV AX,@DATA
            MOV DS,AX

            MOV DX,OFFSET NL1
            MOV AH,09H
            INT 21H
    
            MOV AH,01H
            INT 21H
            SUB AL,30H
            MOV VAL1,AL
            
            MOV AH,00

            MOV CL,2
            DIV CL
            MOV CL,AL

    LBL1:
            MOV AH,00
            MOV AL,VAL1
            DIV CL
            CMP AH,00
            JZ LBL2
            DEC CL
            CMP CL,1
            JNE LBL1
            JMP LBL3
    
    LBL2:
    
            MOV AH,09H
            MOV DX,OFFSET NL2
            INT 21H
            JMP EXIT    
    
    LBL3:
            MOV AH,09H
            MOV DX, OFFSETNL3
            INT 21H
    
    EXIT:
            MOV AH,4CH
            INT 21H
    
  END     MAIN

`
我试图检测一个数是否为素数,但它显示的答案不正确。

q5iwbnjs

q5iwbnjs1#

问题似乎是如果输入小于4,那么CL结束时不是1就是0,这在继续LBL1之前没有检查。而且0和1通常不被认为是素数。下面是一个最小的修复:

.MODEL  SMALL

.DATA
       VAl1     DB      ?
       NL1      DB      0AH,0DH,'ENTER NO:','$'
       NL2      DB      0AH,0DH,'IT IS NOT PRIME','$'
       NL3      DB      0AH,0DH,'IT IS PRIME','$'

       .CODE
   ORG 0100H
MAIN:

        MOV AX,@DATA
        MOV DS,AX

        MOV DX,OFFSET NL1
        MOV AH,09H
        INT 21H

        MOV AH,01H
        INT 21H
        SUB AL,30H
        MOV VAL1,AL
        CMP AL, 2
        JB LBL2 ; 0,1 are not prime
        JE LBL3 ; 2 is prime
        
        MOV AH,00

        MOV CL,2
        DIV CL
        MOV CL,AL
        JMP LBL4 ; check CL

LBL1:
        MOV AH,00
        MOV AL,VAL1
        DIV CL
        CMP AH,00
        JZ LBL2
        DEC CL
LBL4:
        CMP CL,1
        JNE LBL1
        JMP LBL3

LBL2:

        MOV AH,09H
        MOV DX,OFFSET NL2
        INT 21H
        JMP EXIT    

LBL3:
        MOV AH,09H
        MOV DX, OFFSETNL3
        INT 21H

EXIT:
        MOV AH,4CH
        INT 21H

END     MAIN

如果你可以利用你只需要处理0-9的事实,代码可以进一步简化为可能:

.MODEL  SMALL

.DATA
       NL1      DB      0AH,0DH,'ENTER NO:','$'
       NL2      DB      0AH,0DH,'IT IS NOT PRIME','$'
       NL3      DB      0AH,0DH,'IT IS PRIME','$'

       .CODE
   ORG 0100H
MAIN:

        MOV AX,@DATA
        MOV DS,AX

        MOV DX,OFFSET NL1
        MOV AH,09H
        INT 21H

        MOV AH,01H
        INT 21H
        CMP AL, 32H
        JB LBL2 ; 0,1 are not prime
        JE LBL3 ; 2 is prime
        TEST AL, 1
        JZ LBL2 ; even numbers 4,6,8 not prime
        CMP AL, 39H
        JNE LBL3 ; odd except 9 is prime 
LBL2:

        MOV AH,09H
        MOV DX,OFFSET NL2
        INT 21H
        JMP EXIT    

LBL3:
        MOV AH,09H
        MOV DX, OFFSETNL3
        INT 21H

EXIT:
        MOV AH,4CH
        INT 21H

END     MAIN

相关问题