Task

Write a boolean function that tells whether a given integer is prime.



Remember that   1   and all non-positive numbers are not prime.

Use trial division.

Even numbers greater than   2   may be eliminated right away.

A loop from   3   to   √ n    will suffice,   but other loops are allowed.



Related tasks

   count in factors
   prime decomposition
   AKS test for primes
   factors of an integer
   Sieve of Eratosthenes
   factors of a Mersenne number
   trial factoring of a Mersenne number
   partition an integer X into N primes
   sequence of primes by Trial Division


 
/* ARM assembly AARCH64 Raspberry PI 3B */
/*  program testPrime64.s   */
 
/*******************************************/
/* Constantes file                         */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
 
/*******************************************/
/* Initialized data */
/*******************************************/
.data
szMessStartPgm:            .asciz "Program start \n"
szMessEndPgm:              .asciz "Program normal end.\n"
szMessNotPrime:            .asciz "Not prime.\n"
szMessPrime:               .asciz "Prime\n"
szCarriageReturn:          .asciz "\n"
 
/*******************************************/
/* UnInitialized data                      */
/*******************************************/
.bss 
.align 4
/*******************************************/
/*  code section                           */
/*******************************************/
.text
.global main 
main:                                           // program start
    ldr x0,qAdrszMessStartPgm                   // display start message
    bl affichageMess
    ldr x0,qVal
    bl isPrime                                  // test prime ?
    cmp x0,#0
    beq 1f
    ldr x0,qAdrszMessPrime                      // yes
    bl affichageMess
    b 2f
1:
    ldr x0,qAdrszMessNotPrime                   // no 
    bl affichageMess
2:
 
    ldr x0,qAdrszMessEndPgm                     // display end message
    bl affichageMess
 
100:                                            // standard end of the program
    mov x0,0                                    // return code
    mov x8,EXIT                                 // request to exit program
    svc 0                                       // perform system call
qAdrszMessStartPgm:        .quad szMessStartPgm
qAdrszMessEndPgm:          .quad szMessEndPgm
qAdrszCarriageReturn:      .quad szCarriageReturn
qAdrszMessNotPrime:        .quad szMessNotPrime
qAdrszMessPrime:           .quad szMessPrime
//qVal:                      .quad 1042441       // test not prime
//qVal:                      .quad 1046527       // test prime
//qVal:                       .quad 37811          // test prime 
//qVal:                      .quad 1429671721    // test not prime (37811 * 37811)
qVal:                      .quad 100000004437    // test prime
/******************************************************************/
/*     test if number is prime                                    */ 
/******************************************************************/
/* x0 contains the number  */
/* x0 return 1 if prime else return 0 */
isPrime:
    stp x1,lr,[sp,-16]!        // save  registers
    cmp x0,1                   // <= 1 ?
    ble 98f
    cmp x0,3                   // 2 and 3 prime
    ble 97f 
    tst x0,1                   //  even ?
    beq 98f 
    mov x11,3                 // first divisor
1:
    udiv x12,x0,x11
    msub x13,x12,x11,x0       // compute remainder
    cbz x13,98f               // end if zero
    add x11,x11,#2            // increment divisor
    cmp x11,x12               // divisors<=quotient ?
    ble 1b                    // loop
97:
    mov x0,1                  // return prime
    b 100f
98:
    mov x0,0                  // not prime
    b 100f
100:
    ldp x1,lr,[sp],16         // restaur  2 registers
    ret                       // return to address lr x30
 
/********************************************************/
/*        File Include fonctions                        */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
 