Skip to main content
Engineering LibreTexts

10.3: Processing an Array Using an Index

  • Page ID
    76144
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    This first program to use static memory to create of integers, initialize the values in an array, and then create a function, printArrayByIndex, which will walk through the array and print each value on a separate line.

    .global printArrayByIndex 
    .global main 
    
    .text 
    printArrayByIndex: 
        #push stack 
        SUB sp, sp, #16 
        STR lr, [sp, #0] 
        STR r4, [sp, #4]
        STR r5, [sp, #8] 
        STR r6, [sp, #12]
        
        # Save Base Array and Size to preserved registers 
        MOV r4, r0 
        MOV r5, r1
        
        # initialize loop for entering data 
        # r4 - array base 
        # r5 - end loop index 
        # r6 - loop index
        
        MOV r6, #0 
        startPrintLoop:
            CMP r6, r5 
            BGE endPrintLoop 
            
            LDR r0, =output
            MOV r1, r6 
            ADD r2, r4, r6, lsl #2 
            LDR r2, [r2, #0] 
            BL printf 
            
            ADD r6,r6, #1 
            B startPrintLoop
        endPrintLoop:
        
        #pop stack 
        LDR lr, [sp, #0] 
        LDR r4, [sp, #4] 
        LDR r5, [sp, #8] 
        LDR r6, [sp, #12] 
        ADD sp, sp, #16
        MOV pc, lr
        
    .data 
        output: .asciz "The value for element [%d] is %d\n"
    
    #end printArrayByIndex
    
    # Main procedure to test printArrayByIndex 
    .text 
    main: 
        #push stack 
        SUB sp, sp, #4 
        STR lr, [sp, #0]
        LDR r0, =myArray 
        LDR r1, =arrSize 
        LDR r1, [r1] 
        BL printArrayByIndex
        
        #pop stack 
        LDR lr, [sp, #0] 
        ADD sp, sp, #4 
        MOV pc, lr
        
    .data 
        myArray: .word 55 
                 .word 21 
                 .word 78 
                 .word 19 
        arrSize: .word 4
    

    10.3.1 Comments on Program

    The following list explains how this program works.

    1. The following lines create and initialize the array in static (or data) memory. Because the list is in static memory, the values in the list can be changed, but the size of the list cannot be changed. Hence the list is always 4 ints (or 16 bytes) in size.

    myArray: .word 55 
             .word 21 
             .word 78 
             .word 19 
    arrSize: .word 4
    

    2. The array base and array size are passed into the program using the registers r0 and r1. Because these registers are often changed, as when printf is called, the values need to saved either on the stack or in a preserved register. Here the values are copied to r4 and r5. Since r4 and r5 are now being used, these registers must be saved to the stack in the push and copied back from the stack on the pop.

    3. A loop counter variable is created in r6, which means that r6 must be saved on the stack.

    4. The loop index in r6 counts the number of iterations from 0...arrSize. Therefore it can be use to calculate the address of the each element in the array. This is accomplished by multiplying the index by 4 (each int is 4 bytes in size) and adding that amount to the base address. This is done in the following formula.

        ADD r2, r4, r6, lsl #2
    

    In this formula, the multiplication by 4 is performed by left logical shift by 2 bits.

    5. The value of the array index and element are then printed from r1 and r2.


    This page titled 10.3: Processing an Array Using an Index is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Charles W. Kann III via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?