The code below has been implemented using MIPS assembly in a MARS simulator, that when given a matrix NxN it calculates the determinant using a recursive function, the function is allocating any matrix in the heap using array structure where each row get assigned specific array and stores arrays through another array as below
## function computeMatrixDeterminant
## input: $a0 = the matrix pointer, $a1 = the number of rows and columns
## output: $v0 = the determinant
computeMatrixDeterminant:
#allocating space in the stack
add $sp, $sp, -20
sw $s0, 16($sp)
sw $s1, 12($sp)
sw $s2, 8($sp)
sw $s3, 4($sp)
sw $ra, 0($sp)
# our base intances
li $t0, 1
beq $a1, $t0, determinantBaseCase1
li $t0, 2
beq $a1, $t0, determinantBaseCase2
#This is our recursive case
determinantRecursiveCase:
#TODO: writing the function
#for every column j, we calculate (-1)^j * matrix[0,j] * determinant(matrix with no row 0 and column j) and summing it to the latest results
j determinantExit
#case 1x1 matrix
determinantBaseCase1:
li $a1, 0
li $a2, 0
jal getMatrixElement
j determinantExit
#case 2x2 matrix
determinantBaseCase2:
#s0 = is element 0, 0
li $a1, 0
li $a2, 0
jal getMatrixElement
move $s0, $v0
#s1 = is element 0, 1
li $a2, 1
jal getMatrixElement
move $s1, $v0
#s3 = is element 1, 1
li $a1, 1
jal getMatrixElement
move $s3, $v0
#s2 = is element 1, 0
li $a2, 0
jal getMatrixElement
move $s2, $v0
#computing Determinants: [0,0] * [1,1] - [0,1] * [1,0]
mul $v0, $s0, $s2
mul $s0, $s1, $s3
sub $v0, $v0, $s0
#going to exit
j determinantExit
determinantExit:
#restoring variables and returning to the caller
lw $s0, 16($sp)
lw $s1, 12($sp)
lw $s2, 8($sp)
lw $s3, 4($sp)
lw $ra, 0($sp)
add $sp, $sp, 20
j $ra
Works Cited
champion, MMO. Deallocating heap memory in MIPS assembly. 2007. Web. 21 May. 2016 . < http://www.mmo-champion.com>