How to resolve the algorithm Sequence of primes 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 Sequence of primes by trial division step by step in the ARM Assembly programming language

Table of Contents

Problem Statement

Generate a sequence of primes by means of trial division.

Trial division is an algorithm where a candidate number is tested for being a prime by trying to divide it by other numbers. You may use primes, or any numbers of your choosing, as long as the result is indeed a sequence of primes. The sequence may be bounded (i.e. up to some limit), unbounded, starting from the start (i.e. 2) or above some given value. Organize your function as you wish, in particular, it might resemble a filtering operation, or a sieving operation. If you want to use a ready-made is_prime function, use one from the Primality by trial division page (i.e., add yours there if it isn't there already).

Let's start with the solution:

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

Source code in the arm programming language

/* ARM assembly Raspberry PI  */
/*  program trialprime.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
    ldr r4,iStart            @ start number 
    ldr r5,iLimit            @ end number 
    tst r4,#1
    addeq r4,#1
1:
    mov r0,r4
    ldr r1,iAdrsZoneConv
    bl conversion10          @ decimal conversion
    ldr r0,iAdrsZoneConv
    bl affichageMess
    mov r0,r4
    bl isPrime
    cmp r0,#0
    beq 2f
    ldr r0,iAdrszMessPrime
    bl affichageMess
    b 3f
2:  
    ldr r0,iAdrszMessNotPrime
    bl affichageMess  
3:
    add r4,r4,#2
    cmp r4,r5
    blt 1b
 
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 4000000000
iLimit:                   .int 4000000020
/******************************************************************/
/*     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 Loop over multiple arrays simultaneously step by step in the Jsish programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the Odin programming language
You may also check:How to resolve the algorithm Jacobi symbol step by step in the J programming language
You may also check:How to resolve the algorithm DNS query step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Knuth shuffle step by step in the PL/I programming language