Advertisement
Il_Voza

4.5 Check if elements of a vector are prime

May 12th, 2013
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;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.
  2. ;Given:
  3. ;-a vector of byte numbers containing DIM elements (DIM declared like constant)
  4. ;-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.
  5.  
  6. DIM EQU 4
  7. .MODEL small
  8. .STACK
  9. .DATA
  10. vett DB 6,3,9,11
  11. result DB DIM DUP (?)
  12. .CODE
  13. .STARTUP
  14. MOV SI,0
  15. MOV BX,0
  16. ciclo: MOV AL,vett[SI]
  17.        CMP SI,4
  18.        JE ex
  19.        MOV DL,AL ;I store the i-th value of vector into DL because, to see
  20.                  ;if my number is prime, I must divide my number 'n' from n/2
  21.                  ;to 1. If it isn't divisible by these numbers, then it is prime
  22.        SHR DL,1  ;Division by 2, so I get that n/2
  23.        CMP AL,3  ;I say that if the element of vector is 3, it jumps immediately to label 'prime'
  24.                  ;because when I do n/2, it returns me not 2 but 1, and so it gives the number as divisible
  25.                  ;by a number, so it results not prime, that isn't true
  26.        JE prime
  27.        
  28.        ciclo1:DIV DL  ;I divide my i-th element n (that is in AX) by n/2 that is in DL
  29.               CMP AH,0    ;If AH=0 i.e. if the division doesn't give me remainder, then the number is divisible
  30.                           ;by something, so surely it isn't prime
  31.               JE isnotprime
  32.               MOV AH,0
  33.               MOV AL,vett[SI] ;I reassing i-th value of vector to AL      
  34.               DEC DL
  35.               CMP DL,1
  36.               JNE ciclo1
  37.               JMP prime  ;If at the end of cycle I see that my number was not
  38.                          ;divisible by anything, it means that is prime
  39. isnotprime:
  40.           MOV result[SI],0
  41.           INC SI
  42.           JMP ciclo
  43. prime:
  44.       mov result[SI],1
  45.       INC SI
  46.       JMP ciclo  
  47.  
  48. ex:
  49. .exit
  50. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement