Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;Write a program that is able to determine if each natural number (>=2) contained into a vector is prime or not. Remember that a number is prime when it is divisible only by 1 and by itself.
- ;Given:
- ;-a vector of byte numbers containing DIM elements (DIM declared like constant)
- ;-a vector of byte 'result' that has same dimension, that must contain, for each analyzed number, a logic value 1 if the number in the same position are prime, and 0 if it isn't. This vector will be modified by program.
- DIM EQU 4
- .MODEL small
- .STACK
- .DATA
- vett DB 6,3,9,11
- result DB DIM DUP (?)
- .CODE
- .STARTUP
- MOV SI,0
- MOV BX,0
- ciclo: MOV AL,vett[SI]
- CMP SI,4
- JE ex
- MOV DL,AL ;I store the i-th value of vector into DL because, to see
- ;if my number is prime, I must divide my number 'n' from n/2
- ;to 1. If it isn't divisible by these numbers, then it is prime
- SHR DL,1 ;Division by 2, so I get that n/2
- CMP AL,3 ;I say that if the element of vector is 3, it jumps immediately to label 'prime'
- ;because when I do n/2, it returns me not 2 but 1, and so it gives the number as divisible
- ;by a number, so it results not prime, that isn't true
- JE prime
- ciclo1:DIV DL ;I divide my i-th element n (that is in AX) by n/2 that is in DL
- CMP AH,0 ;If AH=0 i.e. if the division doesn't give me remainder, then the number is divisible
- ;by something, so surely it isn't prime
- JE isnotprime
- MOV AH,0
- MOV AL,vett[SI] ;I reassing i-th value of vector to AL
- DEC DL
- CMP DL,1
- JNE ciclo1
- JMP prime ;If at the end of cycle I see that my number was not
- ;divisible by anything, it means that is prime
- isnotprime:
- MOV result[SI],0
- INC SI
- JMP ciclo
- prime:
- mov result[SI],1
- INC SI
- JMP ciclo
- ex:
- .exit
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement