Questions on Arrays

  1. Give a declaration for an array of


  2. A. Give a declaration of an array of 26 characters (called letters), where each element is initialized to a lower case letter of the alphabet. letters[0] is 'a', letters[1] is 'b', and so on to letters[25] is 'z'.
          letters:  .byte   'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
          'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
          'u', 'v', 'w', 'x', 'y', 'z'
      
    B. Give code to initialize each element of an array of 26 characters declared with
         letters:  .byte  '\0':26
      
    to do the same thing as the first part of this question.
            la   $8, letters    # $8 is address of an array element
            li   $9, 'a'        # $9 is the ASCII character to place in the array
            li   $10, 'z'       # $10 is the sentinel ASCII character 'z'
    loop:   bgt  $9, $10, end_code
            sb   $9, ($8)       # store value in array element
            add  $9, $9, 1      # update character
            add  $8, $8, 1      # update address
            b    loop
    end_code:
      
    C. Identify each of parts A and B as either a static initialization or a dynamic initialization of the array.

    Part A is a static initialization. Part B is a dynamic initialization.

  3. Does MIPS assembly language (MAL) provide a way to declare an array without specifying an initial value for each element?

    The .space directive may be used to set aside (allocate) memory space without giving an initial value. However, this memory space is not guaranteed to start at a nicely word-aligned address.

    Note: Our simulator implementation includes a default directive (which you will not see) that causes most memory allocations to be placed at word-aligned addresses, including .space directives.

  4. Write a MAL program that dynamically stores n! in elements of an array of integers. The index of each array element is n. Do this for an array of 10 integers.
    # factorial program
    #   Computes n!, where n is an integer constant value.
    
    .data
    n:       .word   10
    fact:    .word   1:10     # array to store n!
                              # Cleverly initialized to 1, as 0!=1 and 1!=1
    
    .text
    __start:
             lw   $8, n       # $8 is the value n
             la   $9, fact    # $9 is an array element's address
    
             # if n>1, start computing at index=2
             li   $10, 1
             ble  $8, $10, done_computing 
             add  $9, $9, 8   # address now at array element with index=2
    
             li   $11, 2      # current index, and value n 
    for:
             beq  $11, $8, end_for
             lw   $12, -4($9)    # $12 is fact(n-1)
             mul  $13, $12, $11  # fact(n) = fact(n-1) * n
             sw   $13, ($9) 
             add  $11, $11, 1 # increment n
             add  $9, $9, 4   # update element address
             b    for
    end_for:
    
    done_computing:
             done
    


  5. A. Give a declaration for a 2-dimensional array of integers, with 6 rows and 10 columns, where each integer array element is initialized to the value 0.
    array:   .word  0:60
    
    B. What important piece of information has been omitted from this question specification?

    Not specified is whether the array is stored in row major order or column major order.

    C. Does this important piece of information matter? Why or why not?

    For this declaration, it does not matter whether the array is stored in row major or column major order. Either way, 60 contiguous integer-sized array elements must be allocated, and each one must be initialized to the value 0.

    Note that the declaration itself contains no indication of how the memory space will be used. It could be a 1 dimensional array of 60 integers, a 6 x 10 array of integers, a 10 x 6 array of integers, or a 2 x 30 array of integers (and many more combinations are possible).

  6. A 4 row by 7 column array of integers has been declared with
    int_array:  .word  0:28    # 4 x 7 array of integers
    
    This array is stored in row major order. Write a MAL code fragment that initializes each element of the array to be the sum of its row index and column index. A partial diagram of the array (after this code executes) might appear as:
       0  1  2  3  4  5  6   (column indices)
    
       0  1  2  3  4  5  6   (row with index 0)
       1  2  3  4  5  6  7   (row with index 1)
       2  3  4  5  6  7      (partial row with index 2)
                             (missing row with index 3)
    
              # $8 is the row index
              # $9 is the column index
              # $10 is the address of the desired array element
              # $11 is the constant 4 (number of rows)
              # $12 is the constant 7 (number of columns)
              la   $10, int_array
              li   $8, 0       # initialize row index to 0
              li   $11, 4
              li   $12, 7
    outer_for:                 # for each row
              beq  $8, $11, end_outer_for
              li   $9, 0       # start with column index 0
    
    inner_for:                 # for each column (within a row)
              beq  $9, $12, end_inner_for
              # sum row and column, placing sum into this array element
              add  $13, $8, $9
              sw   $13, ($10)
              add  $9, $9, 1   # update column index
              add  $10, $10, 4 # get next array element address
              b    inner_for
    end_inner_for:
    
              add  $8, $8, 1   # update row index
              b    outer_for
    end_outer_for:
    
Copyright © Karen Miller, 2007