Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .global matMult
  2. # program multiplies two matrices together and returns the new matrix
  3. .equ wordsize, 4
  4.  
  5. .text
  6.  
  7. matMult:
  8.     # esp + 24: cols_mat_b
  9.     # esp + 20: rows_mat_b
  10.     # esp + 16: mat_b
  11.     # esp + 12: cols_mat_a
  12.     # esp + 8: rows_mat_a
  13.     # esp + 4 mat_a
  14.     # esp: ret addr
  15.  
  16.     # prologue
  17.     push %ebp
  18.     movl %esp, %ebp
  19.   # make space for locals
  20.   subl $9 * wordsize, %esp
  21.  
  22.     # args
  23.     .equ mat_a, 2 * wordsize # (%ebp)
  24.     .equ rows_mat_a,3 * wordsize # (%ebp)
  25.     .equ cols_mat_a, 4 * wordsize # (%ebp)
  26.     .equ mat_b, 5 * wordsize # (%ebp)
  27.     .equ rows_mat_b, 6 * wordsize # (%ebp)
  28.     .equ cols_mat_b, 7 * wordsize # (%ebp)
  29.  
  30.  
  31.  
  32.     # local variables
  33.     .equ temp_eax, -1 * wordsize # %ebp
  34.     .equ temp_ecx, -2 * wordsize # %ebp
  35.     .equ temp_edx, -3 * wordsize # %ebp
  36.     .equ matrix, -4 * wordsize # %ebp
  37.     .equ result, -5 * wordsize # %ebp
  38.     .equ second_mult, -6 * wordsize # %ebp
  39.     .equ temp1, -7 * wordsize # %ebp
  40.     .equ temp2, -8 * wordsize # %ebp
  41.   .equ temp3, -9 * wordsize # %ebp
  42.  
  43.     movl rows_mat_a(%ebp), %eax
  44.     shll $2, %eax # multiplies number of rows by 4 to get num of bytes
  45.     push %eax # number of bytes to allocated
  46.     call malloc # eax now points to int** of new matrix
  47.     addl $wordsize, %esp # removes size for malloc from stack
  48.  
  49.     # ecx will be our i
  50.     # edx will be our j
  51.     # ebx will be our k
  52.     movl $0, %ecx # i
  53.     movl $0, %edx # j
  54.     movl $0, %ebx # k
  55.  
  56.     movl %eax, %esi # matrix is in esi
  57.     movl %eax, matrix(%ebp) # store matrix in variable
  58.  
  59.     i_loop:
  60.  
  61.     cmpl rows_mat_a(%ebp), %ecx # check if loop should end_outer_loop
  62.     jge end_i_loop
  63.  
  64.     movl cols_mat_b(%ebp), %eax # number of cols in mat b
  65.     shll $2, %eax # getting ready to malloc for number of cols
  66.     push %eax # prepping for malloc
  67.     movl %ecx, temp_ecx(%ebp) # store i
  68.     movl %edx, temp_edx(%ebp) # store j
  69.  
  70.     call malloc # allocate array of length cols_mat_b
  71.     addl $wordsize, %esp # remove argument
  72.  
  73.     movl temp_ecx(%ebp), %ecx # restore i
  74.  
  75.     movl %eax, (%esi, %ecx, wordsize) # place pointer to array in the
  76.                                                                         # ith position of first array
  77.     movl temp_edx(%ebp), %edx # restore edx
  78.  
  79.     movl $0, %edx # reset j
  80.  
  81.         j_loop: # beginning loop of multiplication
  82.  
  83.             cmpl cols_mat_b(%ebp), %edx
  84.             jge end_j_loop # end j loop
  85.  
  86.             movl $0, %edi # running sum
  87.  
  88.             movl $0, %ebx # reset k
  89.  
  90.  
  91.             k_loop:
  92.  
  93.                 cmpl rows_mat_b(%ebp), %ebx
  94.                 jge end_k_loop
  95.  
  96.                 movl %esi, temp1(%ebp) # store matrix in temp1
  97.  
  98.                 movl mat_a(%ebp), %esi # get pointer to mat_a
  99.  
  100.                 movl (%esi, %ecx, wordsize), %esi # get pointer to mat_a[i]
  101.  
  102.                 movl (%esi, %ebx, wordsize), %esi  # store mat_a[i][k] in esi
  103.  
  104.                 movl %esi, temp2(%ebp) # store value from mat_a[i][k]
  105.  
  106.                 movl mat_b(%ebp), %esi # get pointer to mat_b
  107.  
  108.                 movl (%esi, %ebx, wordsize), %esi # get pointer to mat_b[k]
  109.  
  110.                 movl (%esi, %edx, wordsize), %esi # store mat_b[k][j] in esi
  111.  
  112.                 movl %eax, temp_eax(%ebp)
  113.  
  114.                 movl %esi, %eax # moving mat_b[k][j] into %eax
  115.  
  116.                 movl %edx, temp_edx(%ebp)
  117.  
  118.                 mull temp2(%ebp) # and multiplying it by mat_a[i][k]
  119.  
  120.                 movl temp_edx(%ebp), %edx
  121.  
  122.                 addl %eax, %edi # add to running sum
  123.  
  124.                 movl temp1(%ebp), %esi
  125.                 movl temp_eax(%ebp), %eax
  126.  
  127.                 incl %ebx
  128.                 jmp k_loop
  129.  
  130.             end_k_loop:
  131.  
  132.         # movl %esi, temp1(%ebp) # save ptr to matrix
  133.         # movl (%esi, %ecx, wordsize), %esi # get ptr to matrix[i]
  134.         movl %edi, (%eax, %edx, wordsize) # store sum in matrix[i][j]
  135.         # movl temp1(%ebp), %esi
  136.  
  137.         incl %edx
  138.         jmp j_loop
  139.  
  140.         end_j_loop:
  141.  
  142.     incl %ecx
  143.     jmp i_loop
  144.  
  145.     end_i_loop:
  146.  
  147.     epilogue:
  148.         movl matrix(%ebp), %eax # return value
  149.         movl %ebp, %esp
  150.         pop %ebp
  151.         ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement