好吧,我会说,我有一个相当愚蠢的问题。我必须写一个程序,从键盘读取一个数字,并决定它是否是素数。它似乎工作得很好(这里是),但仅限于一位数字。如果我试着阅读,那么请说,10,它只会读1.我怎么可能这样做才能读出多于一位的数字?提前致谢并抱歉可能
assume ds: data_, cs:text_
data_ SEGMENT
Message db 'Enter the number: $'
Message2 db 'Prime $'
Message3 db 'Not prime $'
number dw ?
aux db ?
nrd db 1
var db 2
data_ ENDS
text_ SEGMENT
start:
mov ax, data_
mov ds, ax
mov es, ax
mov cl, 2 ;cl will be used as an index for the divisors of the number which are different from 1 and the number itself
add cl, 48 ;conversion to decimal
mov ah, 09h
mov dx, offset Message ;we print the Message before reading the number
int 21h
mov ah, 08h
int 21h ;using the 08h function, the number is read
sub al, 30h ;conversion to decimal
mov ah, 0 ;conversion from byte to word
mov number, ax ;the read number is copied to the variable number
div var ;since the number is required to be prime, it is enough to check the divisors up until its half, so we divide it by var (which has the value 2)
add al, 48 ;conversion to decimal
mov aux, al ;the half is moved to aux so that al can be used without losing the value in it
mov ax, number ;we move the initial number back to ax
et1: ;in et1, we check whether a number divides itself by the index or not ;if it does, a divisor is found
cmp cl, aux
je et2 ;the set index is compare to the half of the number; if it's equal, we go to et2
div cl
cmp ah, 0
je increment
inc cl
jmp et1
increment:
inc nrd ;nrd is used to count the divisors of the number
et2: ;if there are more than 1 divisor (which is 1), the number isn't prime and we jump to et 3 ;otherwise, we print Message2
cmp nrd, 1
jg et3
mov ah, 09h
mov dx, offset Message2
int 21h
jmp exit
et3: ;if the number isn't prime, Message3 is printed
mov ah, 09h
mov dx, offset Message3
int 21h
exit:
mov ax, 4C00H
int 21H
text_ ENDS
end start