How to resolve the algorithm Sum and product of an array step by step in the AArch64 Assembly programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sum and product of an array step by step in the AArch64 Assembly programming language

Table of Contents

Problem Statement

Compute the sum and product of an array of integers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sum and product of an array step by step in the AArch64 Assembly programming language

Source code in the aarch64 programming language

/* ARM assembly AARCH64 Raspberry PI 3B */
/*  program sumandproduct64.s   */

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

/*********************************/
/* Initialized data              */
/*********************************/
.data
szMessSum:            .asciz "Sum = "
szMessProd:           .asciz "Product = "
szMessStart:          .asciz "Program 64 bits start.\n"
szCarriageReturn:     .asciz "\n"
szMessErreur:         .asciz "Overflow ! \n"

tabArray:       .quad  2, 11, 19, 90, 55,1000000
.equ TABARRAYSIZE,    (. - tabArray) / 8
/*********************************/
/* UnInitialized data            */
/*********************************/
.bss
sZoneConv:             .skip 24
/*********************************/
/*  code section                 */
/*********************************/
.text
.global main 
main:                            // entry of program 
    ldr x0,qAdrszMessStart
    bl affichageMess
    ldr x2,qAdrtabArray
    mov x1,#0                    // indice
    mov x0,#0                    // sum init    
1:
    ldr x3,[x2,x1,lsl #3]
    adds x0,x0,x3
    bcs 99f
    add x1,x1,#1
    cmp x1,#TABARRAYSIZE
    blt 1b
    
    ldr x1,qAdrsZoneConv
    bl conversion10              // decimal conversion
    mov x0,#3                    // number string to display
    ldr x1,qAdrszMessSum
    ldr x2,qAdrsZoneConv         // insert conversion in message
    ldr x3,qAdrszCarriageReturn
    bl displayStrings            // display message
    
    ldr x2,qAdrtabArray
    mov x1,#0                    // indice
    mov x0,#1                    // product init    
2:
    ldr x3,[x2,x1,lsl #3]
    mul x0,x3,x0
    umulh x4,x3,x0
    cmp x4,#0
    bne 99f
    add x1,x1,#1
    cmp x1,#TABARRAYSIZE
    blt 2b
    
    ldr x1,qAdrsZoneConv
    bl conversion10              // decimal conversion
    mov x0,#3                    // number string to display
    ldr x1,qAdrszMessProd
    ldr x2,qAdrsZoneConv         // insert conversion in message
    ldr x3,qAdrszCarriageReturn
    bl displayStrings            // display message
    b 100f
99:
    ldr x0,qAdrszMessErreur
    bl affichageMess
100:                              // standard end of the program 
    mov x0, #0                    // return code
    mov x8,EXIT 
    svc #0                        // perform the system call
qAdrszCarriageReturn:        .quad szCarriageReturn
qAdrsZoneConv:               .quad sZoneConv
qAdrszMessSum:               .quad szMessSum
qAdrszMessProd:              .quad szMessProd
qAdrszMessErreur:            .quad szMessErreur
qAdrszMessStart:             .quad szMessStart
qAdrtabArray:                .quad tabArray

/***************************************************/
/*   display multi strings                         */
/*   new version 24/05/2023                        */
/***************************************************/
/* x0  contains number strings address */
/* x1 address string1 */
/* x2 address string2 */
/* x3 address string3 */
/* x4 address string4 */
/* x5 address string5 */
/* x6 address string5 */
displayStrings:            // INFO:  displayStrings
    stp x7,lr,[sp,-16]!    // save  registers 
    stp x2,fp,[sp,-16]!    // save  registers 
    add fp,sp,#32          // save paraméters address (4 registers saved * 8 bytes)
    mov x7,x0              // save strings number
    cmp x7,#0              // 0 string -> end
    ble 100f
    mov x0,x1              // string 1
    bl affichageMess
    cmp x7,#1              // number > 1
    ble 100f
    mov x0,x2
    bl affichageMess
    cmp x7,#2
    ble 100f
    mov x0,x3
    bl affichageMess
    cmp x7,#3
    ble 100f
    mov x0,x4
    bl affichageMess
    cmp x7,#4
    ble 100f
    mov x0,x5
    bl affichageMess
    cmp x7,#5
    ble 100f
    mov x0,x6
    bl affichageMess
100:
    ldp x2,fp,[sp],16        // restaur  registers 
    ldp x7,lr,[sp],16        // restaur  registers
    ret 

/***************************************************/
/*      ROUTINES INCLUDE                           */
/***************************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeARM64.inc"

  

You may also check:How to resolve the algorithm Array length step by step in the Quackery programming language
You may also check:How to resolve the algorithm Pascal's triangle step by step in the 360 Assembly programming language
You may also check:How to resolve the algorithm User input/Text step by step in the Factor programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the Eiffel programming language