How to resolve the algorithm Sort disjoint sublist step by step in the AArch64 Assembly programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sort disjoint sublist step by step in the AArch64 Assembly programming language

Table of Contents

Problem Statement

Given a list of values and a set of integer indices into that value list, the task is to sort the values at the given indices, while preserving the values at indices outside the set of those to be sorted. Make your example work with the following list of values and set of indices: Where the correct result would be: In case of one-based indexing, rather than the zero-based indexing above, you would use the indices {7, 2, 8} instead. The indices are described as a set rather than a list but any collection-type of those indices without duplication may be used as long as the example is insensitive to the order of indices given.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sort disjoint sublist step by step in the AArch64 Assembly programming language

Source code in the aarch64 programming language

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

/************************************/
/* Constantes                       */
/************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc" 
 
/*********************************/
/* Initialized data              */
/*********************************/
.data
szMessStart:        .asciz "Program 64 bits start.\n"
sMessResult:        .ascii "Value  : "
sMessValeur:        .fill 11, 1, ' '            // size => 11
szCarriageReturn:   .asciz "\n"
 
.align 4
ArrayNumber:      .quad    7, 6, 5, 4, 3, 2, 1, 0
.equ NBELEMENTS,     (. - ArrayNumber) / 8
ArrayIndex:       .quad  6,1,7
.equ NBELEMINDEX,     (. - ArrayIndex) / 8
/*********************************/
/* UnInitialized data            */
/*********************************/
.bss
ArrayExtract:       .skip 8 * NBELEMINDEX
/*********************************/
/*  code section                 */
/*********************************/
.text
.global main 
main:
    ldr x0,qAdrszMessStart
    bl affichageMess
    ldr x4,qAdrArrayNumber         // number array address
    ldr x5,qAdrArrayIndex          // index array address
    ldr x6,qAdrArrayExtract        // extract array address
    mov x3,#0                      // index
1:
    ldr x0,[x5,x3,lsl #3]          // load index
    ldr x1,[x4,x0,lsl #3]          // load value of index
    str x1,[x6,x3,lsl #3]          // store value in array extract
    add x3,x3,#1                   // increment index
    cmp x3,#NBELEMINDEX            // end array index ?
    blt 1b                         // no -> loop

    mov x0,x5                      // index array address
    mov x1,#0                      // first element 
    mov x2,#NBELEMINDEX            // array size
    bl insertionSort
    mov x0,x6                      // extract array address
    mov x1,#0                      // first element 
    mov x2,#NBELEMINDEX            // array size
    bl insertionSort

    mov x3,#0                      // init index
2:
    ldr x0,[x6,x3,lsl #3]          // load value of array extract
    ldr x1,[x5,x3,lsl #3]          // load index
    str x0,[x4,x1,lsl #3]          // store value in number array in index place
    add x3,x3,#1                   // increment indice
    cmp x3,#NBELEMINDEX
    blt 2b

    mov x0,x4                      // number array address
    bl displayArray  
 
100:                               // standard end of the program 
    mov x0, #0                     // return code
    mov x8, #EXIT                  // request to exit program
    svc #0                         // perform the system call
 
qAdrsMessValeur:          .quad  sMessValeur
qAdrszMessStart:          .quad  szMessStart
qAdrszCarriageReturn:     .quad  szCarriageReturn
qAdrsMessResult:          .quad  sMessResult
qAdrArrayNumber:          .quad  ArrayNumber
qAdrArrayIndex:           .quad  ArrayIndex
qAdrArrayExtract:         .quad  ArrayExtract
/******************************************************************/
/*         insertion sort                                              */ 
/******************************************************************/
/* x0 contains the address of table */
/* x1 contains the first element    */
/* x2 contains the number of element */
insertionSort:
    stp x1,lr,[sp,-16]!             // save registers
    stp x2,x3,[sp,-16]! 
    stp x4,x5,[sp,-16]! 
    stp x6,x7,[sp,-16]! 
    add x3,x1,#1                    // start index i
1:                                  // start loop
    ldr x4,[x0,x3,lsl #3]           // load value A[i]
    sub x5,x3,#1                    // index j
2:
    ldr x6,[x0,x5,lsl #3]           // load value A[j]
    cmp x6,x4                       // compare value
    ble 3f
    add x5,x5,#1                    // increment index j
    str x6,[x0,x5,lsl #3]           // store value A[j+1]
    subs x5,x5,#2                   // j = j - 1
    bge 2b                          // loop if j >= 0
3:
    add x5,x5,#1                    // increment index j
    str x4,[x0,x5,lsl #3]           // store value A[i] in A[j+1]
    add x3,x3,#1                    // increment index i
    cmp x3,x2                       // end ?
    blt 1b                          // no -> loop

100:
    ldp x6,x7,[sp],16
    ldp x4,x5,[sp],16
    ldp x2,x3,[sp],16
    ldp x1,lr,[sp],16
    ret 

/******************************************************************/
/*      Display table elements                                */ 
/******************************************************************/
/* x0 contains the address of array */
displayArray:
    stp x1,lr,[sp,-16]!
    stp x2,x3,[sp,-16]! 
    mov x2,x0                         // table address
    mov x3,#0
1:                                    // loop display table
    ldr x0,[x2,x3,lsl #3]
    ldr x1,qAdrsMessValeur            // display value
    bl conversion10                   // call function
    ldr x0,qAdrsMessResult
    bl affichageMess                  // display message
    ldr x0,qAdrszCarriageReturn
    bl affichageMess
    add x3,x3,#1
    cmp x3,#NBELEMENTS
    blt 1b
    ldr x0,qAdrszCarriageReturn
    bl affichageMess
100:
    ldp x2,x3,[sp],16
    ldp x1,lr,[sp],16
    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 Twin primes step by step in the Perl programming language
You may also check:How to resolve the algorithm Call a function step by step in the Raku programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Lang programming language
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the Sidef programming language
You may also check:How to resolve the algorithm Jaro similarity step by step in the Elixir programming language