Vulpes

pissed_off_by_div

Nov 18th, 2011
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;int mat_mult(int * mat1, int * mat2, int * mat3, int mat1row, int mat1col, int mat2row, int mat2col){
  2. ;;mat_mult_asm(     mata,   matb,       matc,matarow,   matacol,    matbrow,    matbcol);
  3.    
  4.  
  5. segment     .txt
  6.         global      mat_mult_asm
  7.  
  8.  
  9.  
  10.  
  11. mat_mult_asm:   enter       18h,0
  12.  
  13.         ;mov        ezx,        [ebp-04h]   ;; i1 storage
  14.         ;mov        ezx,        [ebp-08h]   ;; i2 storage
  15.         ;mov        ezx,        [ebp-0Ch]   ;; mat3 size
  16.         ;mov        ezx,        [ebp-10h]   ;; X       
  17.         ;mov        ezx,        [ebp-14h]   ;; Y       
  18.         ;mov        esp,        [ebp-18h]   ;; esp location for pushpops
  19.  
  20.         ;mov        ezx,        [ebp+08h]   ;; arg 1 mat1 pointer
  21.         ;mov        ezx,        [ebp+0Ch]   ;; arg 2 mat2 pointer
  22.         ;mov        ezx,        [ebp+10h]   ;; arg 3 mat3 pointer
  23.         ;mov        ezx,        [ebp+14h]   ;; arg 4 mat1 rows
  24.         ;mov        ezx,        [ebp+18h]   ;; arg 5 mat1 cols
  25.         ;mov        ezx,        [ebp+1Ch]   ;; arg 6 mat2 rows
  26.         ;mov        ezx,        [ebp+20h]   ;; arg 7 mat2 cols
  27.  
  28.         ;; 0 EAX [???] running product goes here
  29.         ;; 1 EBX [???] running sum of products goes here
  30.         ;; 2 ECX [???] i1 counter & i2 counter & X & Y, X & Y will also have memory
  31.         ;; 3 EDX [???] register to mul by ;; mul edx
  32.         ;; 4 ESI [???] mat1 pointer
  33.         ;; 5 EDI [???] mat3 pointer ;; will be permanently incremented
  34.         ;; 6 EBP [DNC] ;; dear Paloma:  DO NOT change
  35.         ;; 7 ESP [???] mat2 pointer
  36.  
  37.         mov     [ebp-18h],  esp     ;; storing esp value in case
  38.         mov     eax,        [ebp+14h]
  39.         mov     ebx,        [ebp+20h]
  40.         mul     ebx             ;; could possible be simplified, will leave as is for now
  41.         mov     [ebp-0Ch],  eax     ;; total size of mat3
  42.  
  43.         mov     eax,        00000000h   ;; initialize
  44.         mov     [ebp-04h],  eax     ;; i1 storage //i<mat1row*mat2col
  45.         ;mov        [ebp-08h],  eax     ;; i2 storage
  46.  
  47.         mov     esi,        [ebp+08h]   ;; mat1
  48.         mov     esp,        [ebp+0Ch]   ;; mat2
  49.         mov     edi,        [ebp+10h]   ;; mat3
  50.  
  51.                 ;; X=(i1/mat2col)*mat1col+i2;   Y=i2*mat2col+i1%mat2col
  52.         ;mov        eax,        [ebp-04h]   ;; load i1
  53. i1_loop:    mov     ecx,        eax     ;; copy i1 into ecx
  54.         mov     ebx,        [ebp+20h]   ;; load mat2col
  55.         div     ebx             ;; eax = i1/mat2col ;; don't exceed 255 then
  56.         ;; this part right here, gives me a wrong answer, it's this stupid DIV operation that I've spent all night on
  57.         ;; I'm about ready to give up on DIV, here's what I know:
  58.         ;; DIV [8bit] stores the quotient in al & the remainder in ah
  59.         ;; DIV [16bit or 32bit] stores the quotient in ax/eax & remainder in dx/edx. how annoying is that?
  60.         ;; on top of that, I am not even getting the right quotient & it is screwing everything up. I give up for the night
  61.         ;; I'm going to bed. Sorry Paloma. Feel free to turn this assgnt in, but just know that each product from the second
  62.         ;; row on down is incorrect because the mata & matb values being fetched (although within the respective matrix)
  63.         ;; are the wrong indexes. all I wanted was: floor(i1/mat2col)
  64.        
  65.         mov     edx,        [ebp-04h]   ;;BUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUG
  66.         cmp     edx,        2
  67.         jge     bug_done            ;;BUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUG
  68. bug_not_done:  
  69.  
  70.  
  71.         mov     edx,        [ebp+14h]   ;; load mat1col
  72.         mul     edx             ;; eax = (i1/mat2col)*mat1col       done with the i1 part
  73.         mov     [ebp-10h],  eax     ;; store i1 part of X
  74.  
  75.         ;;      i1      dividend
  76. int_modulus:    cmp     ecx,        ebx     ;; you CANNOT, under any condition, put 0 into the divisor
  77.         jl      modu_done
  78.         sub     ecx,        ebx
  79.         jmp     int_modulus
  80. modu_done:  mov     [ebp-14h],  ecx     ;; storing i1 part of Y
  81.  
  82.         mov     ebx,        00000000h   ;; initialize sum of producs
  83.         mov     [ebp-08h],  ebx     ;; set i2 to 0
  84. i2_loop:                       
  85.         mov     ecx,        [ebp-10h]   ;; load X
  86.         mov     eax,        [esi+04*ecx]    ;; load mat1 index X
  87.         inc     ecx             ;; increment X by 1
  88.  
  89.         mov     [ebp-10h],  ecx     ;; store next X
  90.         mov     ecx,        [ebp-14h]   ;; load Y
  91.         mov     edx,        [esp+04*ecx]    ;; load mat2 index Y
  92.         add     ecx,        [ebp+20h]   ;; increment Y by mat2col
  93.         mov     [ebp-14h],  ecx     ;; store Y
  94.         mul     edx             ;; eax = eax*edx
  95.         add     ebx,        eax     ;; add product to running sum of products
  96.  
  97.  
  98.  
  99.         mov     eax,        [ebp-08h]   ;; load i2
  100.         inc     eax             ;; increment i2
  101.         mov     [ebp-08h],  eax     ;; store i2
  102.         cmp     eax,        [ebp+18h]   ;; compare i2 to mat 1 columns
  103.         je      i2_done             ;; done with the running sum of products
  104.         jmp     i2_loop             ;; repeat the i2_loop
  105.  
  106. i2_done:
  107.  
  108.         mov     [edi],      ebx     ;; store ebx(sum of products) to mat3pointer(edi)
  109.         mov     eax,        [ebp-04h]   ;; load eax i1
  110.         inc     eax             ;; inc i1
  111.         mov     [ebp-04h],  eax     ;; store i1
  112.         add     edi,        00000004h   ;; increment mat3pointer by sizeof(int)
  113.  
  114.         cmp     eax,        [ebp-0Ch]   ;; compare i1 to mat3size ;; i1 already in eax here
  115.         jge     mat_done            ;; if we got size, we done
  116.         jmp     i1_loop             ;; jump i1 loop
  117.  
  118.  
  119. mat_done:  
  120.         mov     eax,        edx     ;;BUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUG
  121.         leave
  122.         ret
  123.  
  124.  
  125.  
  126.  
  127. bug_done:                           ;;BUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGBUGvBUGBUG
  128.         ;mov        edx,        [ebp-08h]
  129.         ;cmp        edx,        0
  130.         ;jl     bug_not_done
  131.         jmp mat_done
Advertisement
Add Comment
Please, Sign In to add comment