100 likes | 351 Views
Determinant Calculation of a NxN Matrix Using AT&T Assembly Syntax. Algorithm High-level language implementation AT&T assembly language implementation Image of the stack before and after a function is called. Want to compute. Algorithm. Suppose we have a 3x3 matrix:.
E N D
Determinant Calculation of a NxN Matrix Using AT&T Assembly Syntax Algorithm High-level language implementation AT&T assembly language implementation Image of the stack before and after a function is called
Want to compute Algorithm • Suppose we have a 3x3 matrix: • Use the recursive “expansion by minors” method: • Or in general, for a matrix M of NxN dimensions where Msub(N-1)x(N-1) is a sub-matrix of the matrix M whose dimension is (N-1)x(N-1) and does not contain the first row and i column
Algorithm (cont.) • Observations: • If N=2, compute determinant directly (base case) • If N>2, recursive calls must be done successively until N=2 • For each matrix element M(1,i), need to determine the sub-matrix that does not contain the first row and ith column of the “currently computing” matrix.
High-level Language Implementation: C++ int det(int *M, int N) { if (N==2) { return (M[0]*M[3] - M[1]*M[2]); } else { int new_dimension = (N-1); int tdet=0; int *temp = new int[(N-1)*(N-1)]; for (int ref_ele=0; ref_ele<N;ref_ele++) { int counter=0; for (int i=0;i<N;i++) { for (int j=0;j<N;j++) { if ( (j!= ref_ele) && (i!=0)) { temp[counter] = M[(N*i + j)]; counter++; } } } int t = det(temp, new_dimension); if ((ref_ele % 2) != 0) t = (-1)*t; tdet += M[ref_ele]*t; } return tdet; } }
AT&T Assembly Language Implementation • Specification of the determinant function “det” in the assembly language implementation. The following steps must be done successively: • All elements of the matrix M must be “PUSHED” onto the stack, in the major-row ordered, before the “det” function is called • The dimension N of the matrix M must also be “PUSHED” onto the stack • A variable that will hold the result of the determinant of the currently computing matrix must also be PUSHED onto the stack (make space for the return result) • A variable that will hold an error flag, if ever occurs, must also be “PUSHED” onto the stack (make space for the return error) • See attachment sheets for the program.
Error Flag: (0=no error, 1= error) Result: will contain the determinant N: dimension of Matrix M M[0] M[1] M[3] M[4] …… …… M[N2 – 1] The Image of the Stack “Just” Before the “det” Function Is Called Stack pointer: %esp
Address of the next instruction in the caller to be executed after returning from the function call Error Flag: (0=no error, 1= error) Result: will contain the determinant N: dimension of Matrix M M[0] M[1] M[3] M[4] …… …… M[N2 – 1] The Image of the Stack “Just” AFTER the “det” Function Is Called Stack pointer: %esp