Advertisement
yosikadoshi

Hw3Large

Jun 5th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Hw3Large.asm
  2.  
  3.     .MODEL LARGE
  4.    .STACK 100h
  5.    .DATA
  6. ;_find_closest_pair variables
  7.      ArrSize        DW ?
  8.      i              DW ?
  9.      j              DW ?
  10.      tmpSub         DD ?
  11.      minSub         DD ?
  12.      tmpDI          DW ?
  13.      
  14. ;_find_closest_matrix_pair variables
  15.      MatrixSize     DW ?
  16.      i1             DW ?
  17.      i2             DW ?         
  18.      j1             DW ?
  19.      j2             DW ?
  20.     minSubTotal     DD ?
  21.  
  22.      .CODE
  23.      .386
  24. PUBLIC _find_closest_pair
  25. ;extern long int find_closest_pair(long int arr1[], long int arr2[], int n, int *i_index, int *j_index);
  26. ;                                       [BP+6]         [BP+10]     [BP+14]    [BP+16]      [BP+20]
  27. _find_closest_pair  PROC FAR
  28.      PUSH BP
  29.      MOV BP, SP
  30.      PUSH DI
  31.      PUSH SI
  32.  
  33.      ;put the arrays size in a variable
  34.      MOV AX,[BP+14]
  35.      MOV ArrSize,AX  
  36.      
  37.      ;ES:[DI] --> Arr1[0]
  38.      MOV DI,[BP+6]
  39.      MOV ES,[BP+8]
  40.  
  41.      ;FS:[SI] --> Arr2[0]
  42.      MOV SI,[BP+10]
  43.      MOV FS,[BP+12]
  44.      
  45. ; intialize minSub to |(arr1[0]-arr2[0])|
  46.      MOV EAX, ES:[DI]
  47.      SUB EAX, FS:[SI]
  48.      JNS positive
  49. negative:
  50.      NEG EAX
  51. positive:
  52.      MOV minSub, EAX
  53.      
  54. ;==============CODE IN C:==============
  55. ;   for(i=0;;i<ArrSize;i++)
  56. ;       for(j=0;j<ArrSize;j++)
  57. ;       {
  58. ;           tmpSub=(arr1[i]-arr2[j])
  59. ;           if(tmpSub<0)
  60. ;               tmpSub*=-1;
  61. ;           if (tmpSub<minSub){
  62. ;               minSub=tmpSub
  63. ;               i_index=i;
  64. ;               j_index=j;
  65. ;               }
  66. ;       }
  67. ;=====================================
  68.  
  69. ;initialize i=0 for first loop  :
  70.      XOR AX, AX
  71.      MOV i, AX
  72. subLoopOut:
  73.  
  74.      MOV BX, i
  75.      SHL BX, 2 ;i*4
  76.      MOV ECX, ES:[DI+BX] ;ECX= arr1[i]
  77.      
  78. ;initialize j=0 for second loop:
  79.      XOR AX, AX
  80.      MOV j, AX
  81. subLoopin:
  82.      MOV BX, j
  83.      SHL BX, 2 ;j*4
  84.      MOV EAX, FS:[SI+BX] ;EAX= arr2[j]
  85.      SUB EAX, ECX       ;tmpsuB= arr2[J] - srr2[I]
  86.      JNS not_neg
  87.      NEG EAX
  88. not_neg:     
  89.      MOV tmpSub, EAX
  90.      CMP EAX, minSub
  91.      JA doNotUpdateMin
  92.      
  93.      MOV tmpDI, DI ;save DI value
  94.      
  95.      ;update minSub
  96.      MOV minSub, EAX
  97.      
  98.      ;update i_index
  99.      MOV DI, [BP+16]
  100.      MOV GS, [BP+18]
  101.      MOV AX, i
  102.      MOV GS:[DI], AX
  103.      
  104.      ;update j_index
  105.      MOV DI, [BP+20]
  106.      MOV GS, [BP+22]
  107.      MOV AX, j
  108.      MOV GS:[DI], AX
  109.      
  110.      MOV DI, tmpDI ;return DI his original value
  111.      
  112. doNotUpdateMin:  
  113.      ;subLoopin iteration check  
  114.      INC j
  115.      MOV AX, ArrSize
  116.      CMP j, AX
  117.      JB subLoopin
  118.      
  119.      ;subLoopOut iteration check
  120.      INC i
  121.      MOV AX, ArrSize
  122.      DEC AX
  123.      CMP i, AX
  124.      JBE subLoopOut
  125.      
  126.      ;return DX:AX
  127.      PUSH minSub
  128.      POP AX
  129.      POP DX
  130.      
  131.      POP SI
  132.      POP DI
  133.      POP BP
  134. ;Ending procedure
  135.      RET
  136.      _find_closest_pair  ENDP
  137.  
  138. PUBLIC _find_closest_matrix_pair
  139. ;extern long int find_closest_matrix_pair(long int *arr_ptr[], int n, int m, int *i1_index, int *j1_index,  int *i2_index, int *j2_index);
  140. ;                                                   [BP+6]     [BP+10] [BP+12]    [BP+14]        [BP+18]        [BP+22]         [BP+26]
  141. _find_closest_matrix_pair  PROC NEAR
  142.      PUSH BP
  143.      MOV BP, SP
  144.      PUSH DI
  145.      PUSH SI
  146.      
  147.      ;insert n to MatrixSize
  148.      MOV AX,[BP+10]
  149.      MOV MatrixSize,AX
  150.     ;insert m to ArrSize
  151.      MOV AX,[BP+12]
  152.      MOV ArrSize,AX
  153.      
  154.      ;[DI] --> arr_ptr[0]
  155.      MOV DI,[BP+6]
  156.      MOV ES,[BP+8]
  157.      ;[SI] --> arr_ptr[0]
  158.      MOV SI,[BP+6]
  159.      
  160.      ;initiate minSubTotal to the maximum value possible
  161.      MOV minSubTotal,0
  162.      NOT minSubTotal
  163.      
  164. ;initialize i1=0 for first loop :
  165.      XOR AX, AX
  166.      MOV i1, AX
  167. matLoopOut:  
  168.  
  169. ;initialize i2=i1+1 for second loop:
  170.      MOV AX, i1
  171.      INC AX
  172.      MOV i2, AX
  173. matLoopin:   
  174.  
  175.         ;push values to stack, before calling func find_closest_pair
  176.    
  177.         LEA DX, [j2]
  178.         LEA CX, [j1]
  179.        
  180.         ;address of j2
  181.         PUSH DS
  182.         PUSH DX
  183.      
  184.         ;address of j1
  185.         PUSH DS
  186.         PUSH CX
  187.        
  188.         PUSH ArrSize
  189.        
  190.         ;arr_ptr[i2]
  191.         MOV BX,i2
  192.         SHL BX,2 ;i2*4 
  193.         MOV ECX, ES:[DI+BX]
  194.         PUSH ECX
  195.        
  196.         ;arr_ptr[i1]       
  197.         MOV BX,i1
  198.         SHL BX,2 ;i1*4
  199.         MOV EDX, ES:[SI+BX]
  200.         PUSH EDX
  201.        
  202.         CALL _find_closest_pair
  203.        
  204.         ;ignore junk and clear stack
  205.         ADD SP, 8
  206.         POP ArrSize
  207.         ADD SP, 8
  208.  
  209.         ;mov result saved in DX:AX to tmpSub
  210.         PUSH DX
  211.         PUSH AX
  212.         POP tmpSub
  213.  
  214.         ;check if (tmpSub < minSubTotal)
  215.         MOV EAX,tmpSub
  216.         CMP EAX,minSubTotal
  217.         JA dontUpdateMin
  218.         ;save DI value
  219.         MOV tmpDI,DI
  220.        
  221.         ;Update minSubTotal = tmpSub
  222.         MOV minSubTotal,EAX
  223.        
  224.         ;Update indexes
  225.         MOV DI,[BP+14]
  226.         MOV FS,[BP+16]     
  227.         MOV BX,i1
  228.         MOV FS:[DI],BX ;*i1_index=i1
  229.  
  230.         MOV DI,[BP+18]
  231.         MOV FS,[BP+20]
  232.         MOV BX,j1
  233.         MOV FS:[DI],BX ;*j1_index=l1
  234.  
  235.         MOV DI,[BP+22]
  236.         MOV FS,[BP+24]
  237.         MOV BX,i2
  238.         MOV FS:[DI],BX ;*i2_index=i2
  239.  
  240.         MOV DI,[BP+26]
  241.         MOV FS,[BP+28]
  242.         MOV BX,j2
  243.         MOV FS:[DI],BX ;*j2_index=j2
  244.        
  245.         MOV DI, tmpDI ;return DI his original value
  246.        
  247.  dontUpdateMin:
  248.      ;sumLoopin iteration check  (i2<MatrixSize)
  249.      INC i2
  250.      MOV AX, MatrixSize
  251.      CMP i2, AX
  252.      JB matLoopin
  253.      
  254.      ;sumLoopOut iteration check (i1<MatrixSize-1)
  255.      INC i1
  256.      MOV AX, MatrixSize
  257.      DEC AX
  258.      CMP i1, AX
  259.      JB matLoopOut
  260.      
  261.      ;return DX:AX
  262.      PUSH minSubTotal
  263.      POP AX
  264.      POP DX
  265.  
  266.      POP SI
  267.      POP DI
  268.      POP BP
  269. ;Ending procedure find_closest_matrix_pair
  270.      RET
  271.      _find_closest_matrix_pair  ENDP
  272. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement