How to resolve the algorithm Additive primes step by step in the AArch64 Assembly programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Additive primes step by step in the AArch64 Assembly programming language

Table of Contents

Problem Statement

In mathematics, additive primes are prime numbers for which the sum of their decimal digits are also primes.

Write a program to determine (and show here) all additive primes less than 500. Optionally, show the number of additive primes.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Additive primes step by step in the AArch64 Assembly programming language

Source code in the aarch64 programming language

/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/*  program additivePrime64.s   */
 
/*******************************************/
/* Constantes file                         */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
 
.equ MAXI,      500
 
/*********************************/
/* Initialized data              */
/*********************************/
.data
szMessResult:       .asciz "Prime  : @ \n"
szMessCounter:      .asciz "Number found : @ \n" 
szCarriageReturn:   .asciz "\n"
 
/*********************************/
/* UnInitialized data            */
/*********************************/
.bss  
sZoneConv:                  .skip 24
TablePrime:                 .skip 8 * MAXI 
/*********************************/
/*  code section                 */
/*********************************/
.text
.global main 
main:                               // entry of program 

    bl createArrayPrime
    mov x5,x0                       // prime number

    ldr x4,qAdrTablePrime           // address prime table
    mov x10,#0                      // init counter
    mov x6,#0                       // indice
1:
    ldr x2,[x4,x6,lsl #3]           // load prime
    mov x9,x2                       // save prime
    mov x7,#0                       // init digit sum
    mov x1,#10                      // divisor
2:                                  // begin loop
    mov x0,x2                       // dividende
    udiv x2,x0,x1
    msub x3,x2,x1,x0                // compute remainder
    add x7,x7,x3                    // add digit to digit sum
    cmp x2,#0                       // quotient null ?
    bne 2b                          // no -> comppute other digit

    mov x8,#1                       // indice
4:                                  // prime search loop 
    cmp x8,x5                       // maxi ?
    bge 5f                          // yes
    ldr x0,[x4,x8,lsl #3]           // load prime
    cmp x0,x7                       // prime >= digit sum ?
    add x0,x8,1
    csel x8,x0,x8,lt                // no -> increment indice
    blt 4b                          // and loop
    bne 5f                          // > 
    mov x0,x9                       // equal
    bl displayPrime
    add x10,x10,#1                  // increment counter
5:
    add x6,x6,#1                    // increment first indice
    cmp x6,x5                       // maxi ?
    blt 1b                          // and loop
    
    mov x0,x10                      // number counter
    ldr x1,qAdrsZoneConv
    bl conversion10                 // call décimal conversion
    ldr x0,qAdrszMessCounter
    ldr x1,qAdrsZoneConv            // insert conversion in message
    bl strInsertAtCharInc
    bl affichageMess                // display message
 
100:                                // standard end of the program 
    mov x0, #0                      // return code
    mov x8, #EXIT                   // request to exit program
    svc #0                          // perform the system call
qAdrszCarriageReturn:     .quad szCarriageReturn
qAdrszMessResult:         .quad szMessResult
qAdrszMessCounter:        .quad szMessCounter
qAdrTablePrime:           .quad TablePrime
/******************************************************************/
/*      créate prime array                                       */ 
/******************************************************************/
createArrayPrime:
    stp x1,lr,[sp,-16]!       // save  registres
    ldr x4,qAdrTablePrime    // address prime table
    mov x0,#1                      
    str x0,[x4]              // store 1 in array
    mov x0,#2
    str x0,[x4,#8]           // store 2 in array
    mov x0,#3
    str x0,[x4,#16]          // store 3 in array
    mov x5,#3                // prine counter 
    mov x7,#5                // first number to test
1:
    mov x6,#1                // indice
2:
    mov x0,x7                // dividende
    ldr x1,[x4,x6,lsl #3]    // load divisor
    udiv x2,x0,x1
    msub x3,x2,x1,x0         // compute remainder
    cmp x3,#0                // null remainder ?
    beq 4f                   // yes -> end loop
    cmp x2,x1                // quotient < divisor
    bge 3f
    str x7,[x4,x5,lsl #3]    // dividende is prime store in array
    add x5,x5,#1             // increment counter
    b 4f                     // and end loop
3:
    add x6,x6,#1             // else increment indice
    cmp x6,x5                // maxi ?
    blt 2b                   // no -> loop
4:
    add x7,x7,#2             // other odd number
    cmp x7,#MAXI             // maxi ?
    blt 1b                   // no -> loop
    mov x0,x5                // return counter
100:
    ldp x1,lr,[sp],16         // restaur des  2 registres
    ret
/******************************************************************/
/*      Display prime table elements                                */ 
/******************************************************************/
/* x0 contains the prime */
displayPrime:
    stp x1,lr,[sp,-16]!       // save  registres
    ldr x1,qAdrsZoneConv
    bl conversion10           // call décimal conversion
    ldr x0,qAdrszMessResult
    ldr x1,qAdrsZoneConv      // insert conversion in message
    bl strInsertAtCharInc
    bl affichageMess          // display message
100:
    ldp x1,lr,[sp],16         // restaur des  2 registres
    ret

qAdrsZoneConv:                   .quad sZoneConv  

/********************************************************/
/*        File Include fonctions                        */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"

  

You may also check:How to resolve the algorithm Sum to 100 step by step in the Tcl programming language
You may also check:How to resolve the algorithm Chinese zodiac step by step in the C++ programming language
You may also check:How to resolve the algorithm Haversine formula step by step in the Euler Math Toolbox programming language
You may also check:How to resolve the algorithm Entropy step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Sorting algorithms/Quicksort step by step in the COBOL programming language