Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .global matMult
- # program multiplies two matrices together and returns the new matrix
- .equ wordsize, 4
- .text
- matMult:
- # esp + 24: cols_mat_b
- # esp + 20: rows_mat_b
- # esp + 16: mat_b
- # esp + 12: cols_mat_a
- # esp + 8: rows_mat_a
- # esp + 4 mat_a
- # esp: ret addr
- # prologue
- push %ebp
- movl %esp, %ebp
- # make space for locals
- subl $9 * wordsize, %esp
- # args
- .equ mat_a, 2 * wordsize # (%ebp)
- .equ rows_mat_a,3 * wordsize # (%ebp)
- .equ cols_mat_a, 4 * wordsize # (%ebp)
- .equ mat_b, 5 * wordsize # (%ebp)
- .equ rows_mat_b, 6 * wordsize # (%ebp)
- .equ cols_mat_b, 7 * wordsize # (%ebp)
- # local variables
- .equ temp_eax, -1 * wordsize # %ebp
- .equ temp_ecx, -2 * wordsize # %ebp
- .equ temp_edx, -3 * wordsize # %ebp
- .equ matrix, -4 * wordsize # %ebp
- .equ result, -5 * wordsize # %ebp
- .equ second_mult, -6 * wordsize # %ebp
- .equ temp1, -7 * wordsize # %ebp
- .equ temp2, -8 * wordsize # %ebp
- .equ temp3, -9 * wordsize # %ebp
- movl rows_mat_a(%ebp), %eax
- shll $2, %eax # multiplies number of rows by 4 to get num of bytes
- push %eax # number of bytes to allocated
- call malloc # eax now points to int** of new matrix
- addl $wordsize, %esp # removes size for malloc from stack
- # ecx will be our i
- # edx will be our j
- # ebx will be our k
- movl $0, %ecx # i
- movl $0, %edx # j
- movl $0, %ebx # k
- movl %eax, %esi # matrix is in esi
- movl %eax, matrix(%ebp) # store matrix in variable
- i_loop:
- cmpl rows_mat_a(%ebp), %ecx # check if loop should end_outer_loop
- jge end_i_loop
- movl cols_mat_b(%ebp), %eax # number of cols in mat b
- shll $2, %eax # getting ready to malloc for number of cols
- push %eax # prepping for malloc
- movl %ecx, temp_ecx(%ebp) # store i
- movl %edx, temp_edx(%ebp) # store j
- call malloc # allocate array of length cols_mat_b
- addl $wordsize, %esp # remove argument
- movl temp_ecx(%ebp), %ecx # restore i
- movl %eax, (%esi, %ecx, wordsize) # place pointer to array in the
- # ith position of first array
- movl temp_edx(%ebp), %edx # restore edx
- movl $0, %edx # reset j
- j_loop: # beginning loop of multiplication
- cmpl cols_mat_b(%ebp), %edx
- jge end_j_loop # end j loop
- movl $0, %edi # running sum
- movl $0, %ebx # reset k
- k_loop:
- cmpl rows_mat_b(%ebp), %ebx
- jge end_k_loop
- movl %esi, temp1(%ebp) # store matrix in temp1
- movl mat_a(%ebp), %esi # get pointer to mat_a
- movl (%esi, %ecx, wordsize), %esi # get pointer to mat_a[i]
- movl (%esi, %ebx, wordsize), %esi # store mat_a[i][k] in esi
- movl %esi, temp2(%ebp) # store value from mat_a[i][k]
- movl mat_b(%ebp), %esi # get pointer to mat_b
- movl (%esi, %ebx, wordsize), %esi # get pointer to mat_b[k]
- movl (%esi, %edx, wordsize), %esi # store mat_b[k][j] in esi
- movl %eax, temp_eax(%ebp)
- movl %esi, %eax # moving mat_b[k][j] into %eax
- movl %edx, temp_edx(%ebp)
- mull temp2(%ebp) # and multiplying it by mat_a[i][k]
- movl temp_edx(%ebp), %edx
- addl %eax, %edi # add to running sum
- movl temp1(%ebp), %esi
- movl temp_eax(%ebp), %eax
- incl %ebx
- jmp k_loop
- end_k_loop:
- # movl %esi, temp1(%ebp) # save ptr to matrix
- # movl (%esi, %ecx, wordsize), %esi # get ptr to matrix[i]
- movl %edi, (%eax, %edx, wordsize) # store sum in matrix[i][j]
- # movl temp1(%ebp), %esi
- incl %edx
- jmp j_loop
- end_j_loop:
- incl %ecx
- jmp i_loop
- end_i_loop:
- epilogue:
- movl matrix(%ebp), %eax # return value
- movl %ebp, %esp
- pop %ebp
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement