How to resolve the algorithm Jewels and stones step by step in the AArch64 Assembly programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Jewels and stones step by step in the AArch64 Assembly programming language

Table of Contents

Problem Statement

Create a function which takes two string parameters: 'stones' and 'jewels' and returns an integer. Both strings can contain any number of upper or lower case letters. However, in the case of 'jewels', all letters must be distinct. The function should count (and return) how many 'stones' are 'jewels' or, in other words, how many letters in 'stones' are also letters in 'jewels'.

Note that: So, for example, if passed "aAAbbbb" for 'stones' and "aA" for 'jewels', the function should return 3. This task was inspired by this problem.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Jewels and stones step by step in the AArch64 Assembly programming language

Source code in the aarch64 programming language

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

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

/************************************/
/* Initialized data                 */
/************************************/
.data
szMessResult:         .asciz "Result: "
szStone1:             .asciz "aAAbbbb" 
szJewels1:            .asciz "aA"
szStone2:             .asciz "ZZ" 
szJewels2:            .asciz "z"
szCarriageReturn:     .asciz "\n"
szMessStart:          .asciz "Program 64 bits start.\n"
/************************************/
/* UnInitialized data               */
/************************************/
.bss 
sZoneConv:            .skip 24
/************************************/
/*  code section                    */
/************************************/
.text
.global main   
main:                      // entry of program
    ldr x0,qAdrszMessStart
    bl affichageMess

    ldr x0,qAdrszStone1
    ldr x1,qAdrszJewels1
    bl countJewels
    ldr x0,qAdrszStone2
    ldr x1,qAdrszJewels2
    bl countJewels

100:                       // standard end of the program
    mov x0, #0             // return code
    mov x8, #EXIT          // request to exit program
    svc 0                  // perform the system call
qAdrszStone1:             .quad  szStone1
qAdrszJewels1:            .quad  szJewels1
qAdrszStone2:             .quad  szStone2
qAdrszJewels2:            .quad  szJewels2
qAdrsZoneConv:            .quad  sZoneConv
qAdrszMessResult:         .quad  szMessResult
qAdrszCarriageReturn:     .quad  szCarriageReturn
qAdrszMessStart:          .quad  szMessStart
/***************************************************/
/*   count jewels in stone                   */
/***************************************************/
/* r0 contains stone address */
/* r1 contains jewels address */
/* r0 return jewels count */
countJewels:
    stp x1,lr,[sp,-16]!
    stp x2,x3,[sp,-16]! 
    stp x4,x5,[sp,-16]! 
    stp x6,x7,[sp,-16]! 
    mov x4,#0            // counter
    mov x3,#0            // index stone
1:
    ldrb w6,[x0,x3]      // load byte of stone
    cmp x6,#0            // end stone ?
    beq 3f
    mov x5,#0            // index jewels
2:
    ldrb w2,[x1,x5]      // load byte of jewels
    cmp x2,#0            // end jewels ?
    cinc x3,x3,eq        // yes -> increment index stone
    beq 1b               // and loop
    cmp x6,x2            // compare byte
    cinc x5,x5,ne        // not equal -> increment jewels index
    bne 2b               // and loop
    add x4,x4,#1         // else increment counter
    add x3,x3,#1         // incremente index stone
    b 1b                 // and loop

3:                       // result display
    mov x0,x4
    ldr x1,qAdrsZoneConv
    bl conversion10
    ldr x0,qAdrszMessResult
    bl affichageMess
    ldr x0,qAdrsZoneConv
    bl affichageMess
    ldr x0,qAdrszCarriageReturn
    bl affichageMess
    
    mov x0,x4
 100:
    ldp x6,x7,[sp],16
    ldp x4,x5,[sp],16
    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 Colorful numbers step by step in the 11l programming language
You may also check:How to resolve the algorithm Read a configuration file step by step in the RapidQ programming language
You may also check:How to resolve the algorithm Continued fraction/Arithmetic/Construct from rational number step by step in the Go programming language
You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the Swift programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the Elixir programming language