How to resolve the algorithm Primality by trial division step by step in the ARM Assembly programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Primality by trial division step by step in the ARM Assembly programming language

Table of Contents

Problem Statement

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.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Primality by trial division step by step in the ARM Assembly programming language

Source code in the arm programming language

/* ARM assembly Raspberry PI  */
/*  program testtrialprime.s   */

/************************************/
/* Constantes                       */
/************************************/
/* for this file see task include a file in language ARM assembly*/
.include "../constantes.inc"

//.include "../../ficmacros32.inc"       @ for debugging developper
/************************************/
/* Initialized data                 */
/************************************/
.data
szMessPrime:          .asciz " is prime.\n"
szMessNotPrime:       .asciz " is not prime.\n"
szCarriageReturn:     .asciz "\n"
szMessStart:          .asciz "Program 32 bits start.\n"
/************************************/
/* UnInitialized data               */
/************************************/
.bss 
sZoneConv:            .skip 24
/************************************/
/*  code section                    */
/************************************/
.text
.global main 
main:                        @ entry of program
    ldr r0,iAdrszMessStart
    bl affichageMess
    mov r0,#19
    bl testPrime
    
    ldr r0,iStart            @ number 
    bl testPrime
    ldr r0,iStart1           @ number 
    bl testPrime
 
100:                         @ standard end of the program
    mov r0, #0               @ return code
    mov r7, #EXIT            @ request to exit program
    swi 0                    @ perform the system call
iAdrsZoneConv:             .int sZoneConv

iAdrszMessPrime:         .int szMessPrime
iAdrszMessNotPrime:      .int szMessNotPrime
iAdrszCarriageReturn:     .int szCarriageReturn
iAdrszMessStart:          .int szMessStart
iStart:                   .int 2600002183
iStart1:                  .int 4124163031
/******************************************************************/
/*     test if number is prime                                    */ 
/******************************************************************/
/* r0 contains the number  */
testPrime:
    push {r1,r2,lr}                @ save  registers 
    mov r2,r0
    ldr r1,iAdrsZoneConv
    bl conversion10          @ decimal conversion
    ldr r0,iAdrsZoneConv
    bl affichageMess
    mov r0,r2
    bl isPrime
    cmp r0,#0
    beq 1f
    ldr r0,iAdrszMessPrime
    bl affichageMess
    b 100f
1:  
    ldr r0,iAdrszMessNotPrime
    bl affichageMess 
100:
    pop {r1,r2,pc}                 @ restaur registers
/******************************************************************/
/*     test if number is prime                                    */ 
/******************************************************************/
/* r0 contains the number  */
/* r0 return 1 if prime else return 0 */
isPrime:
    push {r4,lr}                @ save  registers 
    cmp r0,#1                   @ <= 1 ?
    movls r0,#0                 @ not prime
    bls 100f
    cmp r0,#3                   @ 2 and 3 prime
    movls r0,#1
    bls 100f
    tst r0,#1                   @  even ?
    moveq r0,#0                 @ not prime
    beq 100f
    mov r4,r0                   @ save number
    mov r1,#3                   @ first divisor
1:
    mov r0,r4                   @ number
    bl division
    add r1,r1,#2                @ increment divisor
    cmp r3,#0                   @ remainder = zero ?
    moveq r0,#0                 @ not prime
    beq 100f   
    cmp r1,r2                   @ divisors<=quotient ?
    ble 1b                      @ loop
    mov r0,#1                   @ return prime

100:
    pop {r4,pc}                 @ restaur registers
/***************************************************/
/*      ROUTINES INCLUDE                           */
/***************************************************/
/* for this file see task include a file in language ARM assembly*/
.include "../affichage.inc"

  

You may also check:How to resolve the algorithm Subtractive generator step by step in the Ruby programming language
You may also check:How to resolve the algorithm Factorial step by step in the OCaml programming language
You may also check:How to resolve the algorithm Compiler/lexical analyzer step by step in the Julia programming language
You may also check:How to resolve the algorithm Multiplication tables step by step in the D programming language
You may also check:How to resolve the algorithm Find Chess960 starting position identifier step by step in the Ruby programming language