Skip to main content
Engineering LibreTexts

9.3: Printing an Array

  • Page ID
    27150
  • \( \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 presented here shows how to access arrays by creating a PrintIntArray subprogram that prints the elements in an integer array. Two variables are passed into the subprogram, $a0 which is the base address of the array, and $a1, which is the number of elements to print. The subprogram processes the array in a counter loop, and prints out each element followed by a ",". The pseudo code for this subprogram follows.

    Subprogram PrintIntArray(array, size)
    {
        print("[")
        for (int i = 0; i < size; i++)
        {
            print("," + array[i])
        }
        print("]")
    }    
    

    The following is the subprogram in assembly, along with a test main program to show how to use it.

    Program 9-3: Printing an array of integers
    
    .text
    .globl main
    main:
        la $a0, array_base
        lw $a1, array_size
        jal PrintIntArray
        jal Exit
    .data
        array_size: .word 5
        array_base:
            .word 12
            .word 7
            .word 3
            .word 5
            .word 11
    
    .text
    # Subprogram: PrintIntArray
    # Purpose: print an array of ints
    # inputs: $a0 - the base address of the array
    #         $a1 - the size of the array
    #
    PrintIntArray:
        addi $sp, $sp, -16    # Stack record
        sw $ra, 0($sp)
        sw $s0, 4($sp)
        sw $s1, 8($sp)
        sw $s2, 12($sp)
        
        move $s0, $a0         # save the base of the array to $s0
        
        # initialization for counter loop
        # $s1 is the ending index of the loop
        # $s2 is the loop counter
        move $s1, $a1
        move $s2, $zero
        
        la $a0 open_bracket    # print open bracket
        jal PrintString
    
    loop:
        # check ending condition
        sge $t0, $s2, $s1
        bnez $t0, end_loop
        
            sll $t0, $s2, 2    # Multiply the loop counter by 4 to get offset (each element is 4 big).
            add $t0, $t0, $s0  # address of next array element
            lw $a1, 0($t0)     # Next array element
            la $a0, comma
            jal PrintInt       # print the integer from array
            
            addi $s2, $s2, 1   #increment $s0
            b loop
    end_loop:
    
        li $v0, 4              # print close bracket
        la $a0, close_bracket
        syscall
        
        lw $ra, 0($sp)
        lw $s0, 4($sp)
        lw $s1, 8($sp)
        lw $s2, 12($sp)        # restore stack and return
        addi $sp, $sp, 16
        jr $ra
    
    .data
        open_bracket:    .asciiz "["
        close_bracket:   .asciiz "]"
        comma:           .asciiz ","
    .include "utils.asm"       
    

    This page titled 9.3: Printing an Array is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Charles W. Kann III.

    • Was this article helpful?