How to Read Input from 2D Array in MIPS

How to Read Input from 2D Array in MIPS thumbnail
Reading 2D arrays with MIPS assembly language can be challenging.

The MIPS assembly language uses lines of code to instruct computers. Similar to machine language, one line of MIPS code translates into a single, indivisible instruction for a central processing unit. However, machine code uses hexadecimal notation and other constructs, which can be difficult to understand at a glance. Assembly language is far more immediately accessible. Both machine language and assembly language have their peccadilloes, however. In MIPS, data arrays are contiguous lists of values. Adding further dimensions alters this in only a minor way. It creates a contiguous list of 1D arrays. Comprehending this idea is a vital step to reading values from 2D arrays.

Instructions

    • 1

      Identify the location of the desired element. Discern the row and column number. For example, consider the array:

      [3] [4] [3] [2] [1]
      [2] [5] [6] [5] [2]
      [9] [3] [1] [7] [9]
      [1] [2] [3] [4] [5]
      [5] [6] [5] [6] [5]

      This 2D array is a series of five rows and five columns. If the desired element is the center-most value in the array, it has coordinates row 3, column 3.

    • 2

      Rewrite the 2D array as a 1D array. Assembly code does not recognize carriage returns in 2D arrays. Instead, it assembles the data as a 1D array of 1D arrays, starting at a base address and ending at [base address + rows * columns]. To continue the example, the 2D array becomes:

      [3] [4] [3] [2] [1] [2] [5] [6] [5] [2] [9] [3] [1] [7] [9] [1] [2] [3] [4] [5] [5] [6] [5] [6] [5]

      The 25 values in this array correspond to the above five rows and five columns.

    • 3

      Subtract one from the row number of interest. Multiply this modified row number by the total number of columns. Add the value for the column of interest. Multiply it by the number of bytes allocated to each element. In the example, each row has five columns. The location of the integer (4 bytes) in row 3, column 3 is: [(2 * 5) + 3]*4 = 52. In assembly language, this is written: base address + (row * num_columns) + col.

    • 4

      Write a command using the load immediate value command, or li, to read this value into a variable. To conclude the example, set a variable (fini1) equal to the element in the calculated byte position. In assembly language, this appears as: li fini1 (52).

Related Searches:

References

  • Photo Credit Thinkstock/Comstock/Getty Images

Comments

Related Ads

Featured