1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

help with assemble code 8051 - prime or not prime number

Discussion in '8051/8951' started by Greital, Mar 18, 2016.

  1. Greital

    Greital New Member

    Joined:
    Mar 8, 2016
    Messages:
    10
    Likes:
    0
    hi,
    I'm working with a multiple phases project in 8051 micro-controller
    and phase one involved with check if a given number is prime or not

    the way I've trying to do this code is :
    1. assuming 2 numbers one is prime and other is not and save them in R1, and R2
    2. create a counter from 99 and save it in R7 --->> 99 because in the following phases we have to check the prime number or not from 1 to 99 (a number that user enter using keypad)
    3. making a loop and divide the desire number with 99 first then check if prime or not, then decrements and repeat the loop again until we finish
    4. if its prime a led will flash using delay with counter R6=3
    5. if its not prime another led will flash using the same delay and same counter


    but I'm really stuck with the code, something wrong but I don't now what is ?
    any help is appreciable

    ----->>> I'm using Edsim51D1 to check my code

    ----------Begining---------
    START:
    MOV R1,#9
    MOV R2,#31
    MOV R7,#99
    MOV R6,#3
    LOOP:
    MOV A,R1
    MOV B,R7
    DIV AB
    MOV R3,#0
    MOV R3,B
    CJNE R3,#0,NOTPRIME
    DJNZ R7,LOOP

    PRIME:
    ACALL DELAY
    CPL P1.3
    DJNZ R6,PRIME
    AJMP DONE


    NOTPRIME:
    ACALL DELAY
    CPL P1.0
    DJNZ R6,NOTPRIME
    DONE:


    DELAY:
    MOV R5,#20
    X3: MOV R4,#200
    X2: MOV R3,#250
    X1: DJNZ R3,X1
    DJNZ R4,X2
    DJNZ R5,X3
    RET

    END
    -------------End------------
     
  2. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    6,060
    Likes:
    520
    Location:
    Cleveland, OH, USA
    Sorry, I am not familiar with that chip. I use PIC's exclusively. Your approach sounds like it will work for numbers ≤9801 (maybe higher), but dividing the long way can be slow. There are only 1229 primes below 10,000. What s the largest number you need to test? Have you considered a table lookup as a quicker alternative?

    John

    Edit: Also, it might be more efficient to count up with the divisor, rather than down.
     
    Last edited: Mar 18, 2016
  3. KeepItSimpleStupid

    KeepItSimpleStupid Well-Known Member Most Helpful Member

    Joined:
    Oct 30, 2010
    Messages:
    9,966
    Likes:
    1,099
    Be weird and use the Eratosthenes seive. The only penalty is memory. generate all the primes up to 100.
    It's like a 3 line program in some BASICS.
    1. Assume an array of numbers(100)
    2. You start at index 2 and put a true in every index but the first. Then 3, then 4 etc.
    3. To make it faster, you check whether or not the index is true already. So every 4th would have been covered by 2, so you don't do every 4.
    Illustrating
    0 1 2 3 4 5 6 7 8 9 10
    f f f f f f f f f f f f f f initial value
    f t f f f f f f f f f f f f Make 1 a prime number
    f t t f t f t f t f t f t f t do the 2 and every other one

    Then leave 3, then 6, then 9
    4 would have been done with 2
    Leave 5 and do 10
    6 would have been done by 2, so skip
    Leave 7; the next would be 14 which is bigger than 10, our limit)
    8 was done by 2
    9 was done by 3
    10 was done by 2 and 5

    So primes are 1,2,3,5,7,

    The algorithm is super-fast.

    So, tell teach the code is much faster.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Greital

    Greital New Member

    Joined:
    Mar 8, 2016
    Messages:
    10
    Likes:
    0

    I just thought about to divide the desire number by 2 then check if its prime or not , then increment the divisor and check again

    the largest number I have to test is 99
     
  6. KeepItSimpleStupid

    KeepItSimpleStupid Well-Known Member Most Helpful Member

    Joined:
    Oct 30, 2010
    Messages:
    9,966
    Likes:
    1,099
    You only have to check up to the sqrt of the number. So, you basically have to check 2 through 10, I think.

    You don't have a sqrt function, so use the max of sqrt(100) (Round numbers).
     
    Last edited: Mar 18, 2016
  7. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    6,060
    Likes:
    520
    Location:
    Cleveland, OH, USA
    If you only have to check to 99, a table would be the fastest and easiest way to go, at least for me. I wouldn't even waste time on an odd or even test. Just have a list o's and 1's for each number: 0= not prime; 1 = prime. Tests for zero are very quick. If not zero, then it is a prime. A table will handle 1 and 0 (not prime) easily.

    John
     
  8. KeepItSimpleStupid

    KeepItSimpleStupid Well-Known Member Most Helpful Member

    Joined:
    Oct 30, 2010
    Messages:
    9,966
    Likes:
    1,099
    But, it's probably an exercise. e.g. Divisible by itself and one. If the assignment is written differently, then anything goes.
     
  9. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    6,060
    Likes:
    520
    Location:
    Cleveland, OH, USA
    I don't think it is an assignment for homework. If it is, then we need to know what criteria are being used for grading. The table will be far faster than dividing by every integer up to 99 for the 25 (or so) primes that need to be considered.

    John

    Edit: If you take the successive divide route, the largest divisor you need to test is 47 for numbers less than 100. Also, you do not need to test all of the divisors less than or equal to 47, only the the 15 primes (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47).
     
    Last edited: Mar 18, 2016
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Greital Your code is almost there..

    Using a progressive loop the whole 25 prime No.'s are found in 26mS on a basic 8051..

    The code I used is here:-
    Code (asm):

      $MOD51
       ORG  0
         JMP  START

    START:
       MOV R0,#16     ; Start of table
       MOV R1,#3     ; Start at number 3

       MOV @R0,#2     ; store the first prime number
       INC R0       ; next
       
    LOOP:   MOV R6,#2     ; count from two
    LP:   MOV A,R1
       MOV B,R6
       DIV AB       ; modulus number / count
       MOV A,B
       JZ BLP1       ; zero either divides by count or itself
       DEC R1       ;\
       MOV A,R6     ; | Test number  = count
       SUBB A,R1     ; |
       INC R1       ;/
       INC R6
       JNZ LP       ; Loop done

    BLP1:   MOV A,R6     ; Test if a prime
       SUBB A,R1     ; by seeing if count run full course
       JNZ NPRIME

    PRIME:   MOV A,R1     ; Primes are lobbed into table
       MOV @R0,A
       INC R0
       
    NPRIME: MOV A, R1
       SUBB A,#99     ; All 99 numbers done??
       INC R1
       JNZ LOOP
         

    DONE:   JMP DONE
     
     
    • Like Like x 1
  11. Greital

    Greital New Member

    Joined:
    Mar 8, 2016
    Messages:
    10
    Likes:
    0
    thanks to all of you for your help,
    I've done by dividing my number by a prime numbers (2, 3, 5, 7) and check after each division if its prime or not
     

Share This Page