home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1987 / 12 / mariella / rall16.asm < prev   
Assembly Source File  |  1987-12-21  |  4KB  |  156 lines

  1. ;         RALL16 - square roots of 1 to 65535 
  2. ;         By Ray Mariella, Nov 1986   
  3.            page    ,96
  4. ;
  5. crlf      macro
  6.           mov    dl,13
  7.           call   char_out
  8.           mov    dl,10
  9.           call   char_out
  10.           endm
  11. ;
  12. time_print macro byte_var, byte_string
  13.           local plenty
  14.           mov    dl,byte_string             ;output a colon or period
  15.           call   char_out
  16.           cmp    byte_var,9
  17.           ja     plenty
  18.           mov    dl,'0'                     ;space holder if var<10
  19.           call   char_out
  20. plenty:   mov    al,byte_var                ;minutes, secs, or hnds
  21.           xor    ah,ah
  22.           call   dec_out
  23.           endm
  24. ;
  25. ;
  26. data      segment  word public 'DATA'
  27.           even                          
  28. base             dw  10                       ;base to print the numbers in
  29. uper             db  ?
  30. secs             db  ?
  31. hnds             db  ?
  32. announc          db  'square roots of 1 - 65535 ',13,10,'$'
  33. data      ends
  34. ;
  35. ;
  36. stack     segment   stack
  37.                  dw  64  dup(?)       
  38. stack     ends
  39. ;
  40. ;
  41. code      segment  word public 'CODE'
  42.           assume cs:code, ds:data, ss:stack
  43.           even                     
  44. ;
  45. sqrt:     mov    ax,data
  46.           mov    ds,ax
  47. crlf
  48.           mov    dx,offset announc
  49.           mov    ah,9                    ;print string function
  50.           int    21h                     ;DOS interrrupt
  51.           mov    dl,13
  52.           call   char_out
  53. ;
  54.           call  update
  55.           mov    cx,65535
  56. ;
  57. ;      square root procedure via 8086, integer in CX
  58. ;
  59. boundit:  mov    ax,1                   ;infimum - will be lower bound
  60.           mov    dx,cx                   ;supremum - will be upper bound
  61. ;
  62. ; the next section gets the max lower bound and the min upper bound
  63.           REPT   8
  64.           shl    ax,1                   ;mpy infimum by 2
  65.           cmp    ax,dx                  ;above supremum ?
  66.           ja     root                   ; then done
  67.           shr    dx,1                   ;if not, div supremum by 2
  68.           mov    bx,dx                  ;store supr.
  69.           mov    si,ax                  ;store infim.
  70.           ENDM
  71. ;
  72. root:     add    bx,si                  ;get avg. of bounds for
  73.           shr    bx,1                   ;first guess of root
  74.           mov    ax,cx
  75. ;
  76. ;      Newton's Method =>  X = (Xo + N/Xo)/2
  77. ;
  78. Newton:   
  79.           xor    dx,dx           ;prepare for division
  80.           div    bx              ;ax still has target
  81.           add    bx,ax           ;newton
  82.           shr    bx,1
  83. ;
  84. ; we now have a square root in bx
  85. ;
  86. ;to print, remove the next 8 semicolons
  87. ;         mov   ax,cx
  88. ;         call  dec_out
  89. ;         mov   dl,' '
  90. ;         call  char_out
  91. ;         mov   ax,bx
  92. ;         call  dec_out
  93. ;         crlf
  94. didit:   loop boundit
  95. ;
  96. done:     call update
  97.           crlf
  98.           xor    al,al
  99.           mov    ah,4Ch
  100.           int    21h
  101. ;
  102. ;
  103. ;         output a hex word in decimal
  104. ;
  105. ;         CX,AX,DX destroyed
  106. ;
  107. dec_out   proc   near
  108.           xor    cx,cx
  109. another:  inc    cx
  110.           xor    dx,dx
  111.           div    base                   ;base is 10 decimal!
  112.           push   dx                     ;remainder is less sig digits
  113.           or     ax,ax                  ;is the quotient zero?
  114.           jnz    another                ;if not, more number to convert
  115. print_dig:
  116.           pop    dx                     ;retrive digit from stack
  117.           add    dl,'0'                 ;ascii offset
  118.           call   char_out
  119.           loop   print_dig              ;do all of the digits
  120.           ret
  121. dec_out   endp
  122. ;
  123. ;         output a single character
  124. ;
  125. char_out  proc   near
  126.           mov    ah,2                   ;output char function
  127.           int    21h                    ;do it
  128.           ret
  129. char_out  endp
  130. ;
  131. ;
  132. update    proc  near
  133.           mov    ah,2ch                 ;get dos time
  134.           int    21h                    ;hour in ch, mins in cl,secs in dh
  135.           mov    uper,cl
  136.           mov    secs,dh
  137.           mov    hnds,dl
  138.           mov    al,ch
  139.           xor    ah,ah
  140.           call   dec_out
  141. ;
  142.           time_print uper,':'
  143. ;
  144.           time_print secs,':'
  145. ;
  146.           time_print hnds,'.'
  147. ;
  148.           crlf
  149.           ret
  150. update    endp
  151. ;
  152. code      ends
  153.           end    sqrt   
  154.  
  155.  
  156.