home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1987 / 12 / mariella / r32fail.asm < prev    next >
Assembly Source File  |  1987-12-21  |  7KB  |  230 lines

  1. ;         R32fail.ASM - 32 bit integer test program REPT macros
  2. ;         looks for the first time that Newton fails 0,-1
  3. ;         By Ray Mariella, April 87
  4.            page    ,96
  5. .8087
  6. ;
  7. crlf      macro
  8.           mov    dl,13
  9.           call   char_out
  10.           mov    dl,10
  11.           call   char_out
  12.           endm
  13. ;
  14. time_print macro byte_var, byte_string
  15.           local plenty
  16.           mov    dl,byte_string             ;output a colon or period
  17.           call   char_out
  18.           cmp    byte_var,9
  19.           ja     plenty
  20.           mov    dl,'0'                     ;space holder if var<10
  21.           call   char_out
  22. plenty:   mov    al,byte_var                ;minutes, secs, or hnds
  23.           xor    ah,ah
  24.           call   dec_out
  25.           endm
  26. ;
  27. ;
  28. data      segment  word public 'DATA'
  29. base             dw  10                     ;base to print the numbers in
  30. BIGGUN           dq  ?
  31. rootp            dd  ?
  32. contwd           dw  ?
  33. uper             db  ?
  34. secs             db  ?
  35. hnds             db  ?
  36. announc          db  'incr. lower 16 from 1 ',13,10,'$'
  37. intermed         db  ' passed 65535   ',13,10,'$'
  38. data      ends
  39. ;
  40. ;
  41. stack     segment   stack
  42.                  dw  64  dup(?)       
  43. stack     ends
  44. ;
  45. ;
  46. code      segment  word public 'code'
  47.           assume cs:code, ds:data, ss:stack
  48. ;
  49. sqrt:     push   bp
  50.           mov    ax,data
  51.           mov    ds,ax
  52.           mov    bp,offset biggun
  53.           fclex                         ;clear 8087 exceptions, if any
  54.           fstcw  contwd                 ;get control word
  55.           and    contwd,1111001111111111B ;round to nearest
  56.           fldcw  contwd                 ;load changed control word
  57.           crlf
  58.           mov    dx,offset announc
  59.           mov    ah,9                    ;print string function
  60.           int    21h                     ;DOS interrrupt
  61.           mov    dl,13
  62.           call   char_out
  63. ;
  64. herald:   crlf
  65. ;
  66. rolling:  xor    bx,bx
  67.           xor    ax,ax
  68.           mov    si,1                   ;lower 16, will vary  
  69.           mov    ds:[bp],si             ;BIGGUN lower 16
  70.           mov    di,0                   ;upper 16
  71.           mov    ds:[bp+2],di           
  72. ;        
  73. ;
  74. goodies:  call  update
  75.           crlf
  76. start:    FILD BIGGUN
  77. ;
  78. ;      square root procedure via 8086/V30
  79. ;
  80. ;
  81.           mov    bx,1                   ;guess1
  82.           mov    dx,di                  ;guess2
  83.           mov    ax,si
  84. ;
  85.           REPT 8
  86.           or     dx,dx
  87.           jz     halfway
  88.           rcr    dx,1                   ;upper 16/2
  89.           rcr    ax,1                   ;lower 16/2 + carry from upper
  90.           shl    bx,1                   ;guess1*2
  91.           endm
  92.           jmp    short rest
  93. halfway:  jmp    short words
  94. ;
  95. rest:
  96.           REPT 8
  97.           or     dx,dx
  98.           jz     words
  99.           rcr    dx,1                   ;upper 16/2
  100.           rcr    ax,1                   ;lower 16/2 + carry from upper
  101.           shl    bx,1                   ;guess1*2
  102.           endm
  103.                                         ;next is for guess1 and guess2 16 bits
  104. words:    FSQRT
  105.           or     bx,bx
  106.           jnz    checkem                ;if all 32 were used, CF is set
  107.           mov    bx,65535               ;in case all 32 bits were used
  108. ;
  109. checkem:  mov    dx,ax                  ;guess2 ax,dx
  110.           mov    cx,bx                  ;guess1 bx,cx
  111. ;
  112. logit:
  113.           REPT 8
  114.           shl    cx,1                   ;guess1
  115.           jc     average                ;necessary for 2000:4000 and up
  116.           cmp    cx,dx                  ;larger than guess2?
  117.           jae    average                
  118.           shr    dx,1                   ;if not, guess2/2
  119.           mov    ax,dx
  120.           mov    bx,cx
  121.           endm
  122.                                         ;ready for averaging
  123. average:  FISTP  rootp
  124.           add    bx,ax
  125.           rcr    bx,1                   ;average value
  126. Newton:   
  127.           REPT   2
  128.           mov    ax,si           ;lower 16
  129.           mov    dx,di           ;prepare for division, upper 16 in dx
  130.           cmp    bx,dx           ;for FFFE:0000 and up
  131.           je     quit
  132.           div    bx              ;ax still has target, bx first guess
  133.           add    bx,ax           ;newton
  134.           rcr    bx,1
  135.           endm
  136.           FWAIT
  137. done:     mov    ax,bx                          ;ax, and bx have approx. root
  138.           mov    dx,word ptr ds:[bp+8]          ;bp+8 is rootp, 8087 root
  139.           xor    ax,dx                          ;see if rootp agrees
  140.           jz     cont
  141.           cmp    bx,dx
  142.           ja     quit
  143. ;
  144. belo:     inc    bx
  145.           cmp    bx,dx
  146.           jnz    quit
  147. ;
  148. ;
  149. cont:     inc    si                     ;lower 16
  150.           jnz    notyet
  151.           inc    di
  152.           mov    ax,di
  153.           call   dec_out
  154.           mov    dx,offset intermed
  155.           mov   ah,9
  156.           int    21h
  157.           crlf
  158. notyet:   mov    ds:[bp+2],di           ;biggun upper 16
  159.           mov    ds:[bp],si             ;biggun lower 16
  160.           jmp   start
  161. ;
  162. quit:     call   update
  163.           mov    ax,di
  164.           call   dec_out
  165.           mov    dl,','
  166.           call   char_out
  167.           mov    ax,si
  168.           call   dec_out
  169.           crlf
  170.           pop    bp
  171.           xor    al,al
  172.           mov    ah,4Ch
  173.           int    21h
  174.           ret
  175. ;
  176. ;         output a hex word in decimal
  177. ;
  178. ;         CX,AX,DX destroyed
  179. ;
  180. dec_out   proc   near
  181.           xor    cx,cx
  182. another:  inc    cx
  183.           xor    dx,dx
  184.           div    base                   ;base is 10 decimal!
  185.           push   dx                     ;remainder is less sig digits
  186.           or     ax,ax                  ;is the quotient zero?
  187.           jnz    another                ;if not, more number to convert
  188. print_dig:
  189.           pop    dx                     ;retrive digit from stack
  190.           add    dl,'0'                 ;ascii offset
  191.           call   char_out
  192.           loop   print_dig              ;do all of the digits
  193.           ret
  194. dec_out   endp
  195. ;
  196. ;         output a single character from dl
  197. ;
  198. char_out  proc   near
  199.           mov    ah,2                   ;output char function
  200.           int    21h                    ;do it
  201.           ret
  202. char_out  endp
  203. ;
  204. ;
  205. update    proc  near
  206.           mov    ah,2ch                 ;get dos time
  207.           int    21h                    ;hour in ch, mins in cl,secs in dh
  208.           mov    uper,cl
  209.           mov    secs,dh
  210.           mov    hnds,dl
  211.           mov    al,ch
  212.           xor    ah,ah
  213.           call   dec_out
  214. ;
  215.           time_print uper,':'
  216. ;
  217.           time_print secs,':'
  218. ;
  219.           time_print hnds,'.'
  220. ;
  221.           crlf
  222.           ret
  223. update    endp
  224. ;
  225. code      ends
  226.           end    sqrt
  227.  
  228.  
  229.  
  230.