#### Dynamic Matrices

A matrix (plural matrices) is a rectangular array of numbers arranged in rows and columns. For example,

``` 2 3 5
3 5 8```

is a 2×3 (2 rows, 3 columns) matrix.

Matrices are used extensively in scientific and mathematical software. For example, the above matrix could be used to represent the following system of equations:

```2x + 3y = 5
3x + 5y = 8```

Then matrix manipulations can be used to solve the equations.

#### Assignment

For this assignment you should write a MAL program with a main program and two subprograms:

• The `mwrite` subprogram writes out a matrix of integers.
• The `mcreate` subprogram dynamically creates an integer matrix whose entries are all 0.

Static matrices are used for testing `mwrite`. Then `mwrite` is used for testing `mcreate`. All of the entries in the matrices are 4 byte integers (words).

#### Internal Matrix Representation

To represent a matrix in the computer, you need a matrix descriptor struct with three entries: the number of rows, the number of columns, and the address of an array containing the entries. The entry arrays and descriptor structs are declared statically for testing `mcreate`.

When `mcreate` is written it will create matrices dynamically. The MAL `sbrk` system call is used to allocate the memory for both the descriptor struct and the entries array.

The entries in the array are stored in row-major order. The entries of the first row go into the array in order starting at the array base address. These entries are followed by the entries of the second row, then the third row, and so on.

#### Static Test Matrices

The static test matrices should be declared with the following declarations in a `.data` section of your program. These test matrices all use the same entries array but use different descriptors to yield different dimensions.

```# static entries declaration
entries:
.word	0
.word	1
.word	2
.word	3
.word	4
.word	5

# static descriptor declaration for a 6x1 matrix
matrix6x1:
.word	6	# 6 rows
.word	1	# 1 column
.word	entries

# static descriptor declaration for a 3x2 matrix
matrix3x2:
.word	3	# 3 rows
.word	2	# 2 columns
.word	entries

# static descriptor declaration for a 2x3 matrix
matrix2x3:
.word	2	# 2 rows
.word	3	# 3 columns
.word	entries

# static descriptor declaration for a 1x6 matrix
matrix1x6:
.word	1	# 1 row
.word	6	# 6 columns
.word	entries```

#### The `mwrite` Subprogram

The `mwrite` subprogram should have one parameter: the address of a matrix descriptor. It should not have a return value. It just prints the matrix specified by its parameter. This is just a simple sequential array processing loop.

You can test the `mwrite` subprogram by putting code in the main program to call `mwrite` once with each of the matrix descriptor addresses.

#### Write Format

The output from `mwrite` should display the matrix entries in rows. It would be nice if you could line up the columns, but that is more effort than it is worth when the entries do not have the same number of digits. Just write out the entries sequentially, each preceeded by a space character. You will need a doubly nested loop to add a newline character after each row.

For example, the `matrix2x3` static matrix should display as

``` 0 1 2
3 4 5```

The outermost of the nested loops iterates over rows and the innermost loop iterates over columns. The loop variables are not used for anything except counting iterations. For accessing the entries in the entries array you just start at its base address and advance the address after loading each entry.

#### The `mcreate` Subprogram

The `mcreate` subprogram has two parameters: the number of rows and the number of columns in the matrix to be created. It should use two `sbrk` system calls: one to create the entries array and one to create the matrix descriptor. It should return the address of the matrix descriptor that it creates.

The number of entries in the array of entries should be the product of the two subprogram parameters. You do not need to do anything to initialize the entries in the array. They are automatically initialized to 0.

The descriptor struct entries should be initialized with the number of rows, the number of columns, and the array address. The first two values are just the parameters. The third is the address returned by the sbrk system call that created the entries array.

#### The Main Program

The main program is the easy part. When you are working on `mwrite` the main program should invoke `mwrite` with each of the static descriptor addresses. Later, when you are working on `mcreate` you should add code to create a 6×6 matrix and invoke `mwrite` with its return value.

To make the output more readable you should print out a label such as "matrix3x2" for each matrix before calling `mwrite`.

#### What to Turn in

Turn in both a copy of your program and a Mars session record. In this session, you will just run your main program. You will not need to enter any input.