AnonymousEng

sqrt

Apr 13th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .MODEL  SMALL
  2. .STACK  64
  3. .DATA
  4.     ORG 00
  5.     Y      DW  9    
  6.     X1     DW  1    ;Xn
  7.     X2     DW  ?    ;Xn+1
  8.    
  9.     ORG 10H
  10.     A      DW  0    ;(Y/X1)
  11.     B      DW  0    ;(X1+A)
  12.    
  13.     ORG 20H
  14.     Diff   DW  0    ;Holds the difference when comparing
  15.    
  16.     ORG 30H
  17.     RESULT DW  ?
  18.    
  19.    
  20. .CODE
  21.     MAIN    PROC    FAR
  22.     ;Setting the DS
  23.         MOV AX,@DATA
  24.         MOV DS,AX
  25.    
  26.     ;The Program is goint to do this process
  27.     ; X2 = (X1 + (NUMBER/X1))/2
  28.     ; and will repeat it unti |(X2-X1)|<2
  29.    
  30.     ;Shifting the variables to enlarge the precision
  31.         MOV CL,8
  32.         SHL WORD PTR Y,CL       ;Shift the Y to the left by 8bits
  33.         SHL WORD PTR X1,CL      ;Shift X1 as well
  34.    
  35.     Iteration:    
  36.     ;Getting A : Y(Word)/X1(Word)
  37.         MOV AX,Y
  38.         SUB DX,DX ;Clear DX
  39.         DIV X1
  40.         MOV A,AX  ;Move the quotient in A
  41.        
  42.     ;Getting B : X1+A
  43.         SUB AX,AX   ;clears the Ax
  44.         ADD AX,A
  45.         ADD AX,X1
  46.         MOV B,AX
  47.        
  48.     ;Getting X2 = (B)(WORD)/NUM2(WORD)
  49.         SUB DX,DX
  50.         MOV BX,02
  51.         DIV BX      ;B/2
  52.         MOV X2,AX   ;Stote in X2
  53.    
  54.     ;Find X2-X1 and strore it in Diff
  55.         SUB AX,X1       ;X2-X1
  56.         JNC NOFIX       ;IF Not Negative goto NOFIX
  57.             ;FIX If reuslt is negative
  58.             NOT AX
  59.             INC AX
  60.         NOFIX:
  61.             CLC
  62.             MOV Diff,AX  
  63.             CMP Diff,2
  64.             MOV BX,X2
  65.             MOV X1,BX       ;Set the Xn+1 = Xn for the next trial if there was
  66.             JNC Iteration   ;repeate the whole process
  67.             ;Else
  68.                 MOV CL,4
  69.                 SHR X1,CL
  70.                 SHR X2,CL
  71.                
  72.             ;Store the Result    
  73.             MOV AX,X1
  74.             MOV RESULT,AX
  75.        
  76.         ;DOS EXIT COMMAND
  77.         MOV AX,4CH
  78.         INT 21H
  79.    
  80.     MAIN    ENDP
  81. END MAIN
Advertisement
Add Comment
Please, Sign In to add comment