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

  1. ;         R32COMP.ASM - 32 bit sqr , compares CPU, NDP
  2. ;         By Ray Mariella, 30 March 87 - increments the upper 16 bits
  3. ;                                         0000:7FFF to FFFF:7FFF
  4. ;         requires 8087 or 80287
  5.            page    ,96
  6. .8087
  7. ;
  8. crlf      macro
  9.           mov    dl,13
  10.           call   char_out
  11.           mov    dl,10
  12.           call   char_out
  13.           endm
  14. ;
  15. time_print macro byte_var, byte_string
  16.           local plenty
  17.           mov    dl,byte_string             ;output a colon or period
  18.           call   char_out
  19.           cmp    byte_var,9
  20.           ja     plenty
  21.           mov    dl,'0'                     ;space holder if var<10
  22.           call   char_out
  23. plenty:   mov    al,byte_var                ;minutes, secs, or hnds
  24.           xor    ah,ah
  25.           call   dec_out
  26.           endm
  27. ;
  28. ;
  29. data      segment  public 'DATA'
  30.           even                          
  31. base             dw  10                       ;base to print the numbers in
  32. BIGGUN           dq  ?
  33. rootp            dd  ?
  34. uper             db  ?
  35. secs             db  ?
  36. hnds             db  ?
  37. announc          db  ' 65535 increments of upper 16, CPU then 8087 ',13,10,'$'
  38. data      ends
  39. ;
  40. ;
  41. stack     segment   stack
  42.                  dw  64  dup(?)       
  43. stack     ends
  44. ;
  45. ;
  46. code      segment  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. crlf
  54.           mov    dx,offset announc
  55.           mov    ah,9                    ;print string function
  56.           int    21h                     ;DOS interrrupt
  57. ;
  58.           mov    si,32767
  59.           xor    di,di
  60. goodies:  call  update
  61. ;
  62. ;      square root procedure via 8086, DI:SI
  63. ;
  64. CPU:      mov    bx,1                   ;guess1
  65.           mov    dx,di                  ;guess2
  66.           mov    ax,si
  67. biggest:  or     dx,dx
  68.           jz     words
  69.           rcr    dx,1                   ;upper 16/2
  70.           rcr    ax,1                   ;lower 16/2 + carry from upper
  71.           shl    bx,1                   ;guess1*2
  72.           jmp    short biggest
  73.                                         ;next is for guess1 and guess2 16 bits
  74. words:    or     bx,bx
  75.           jnz    checkem                ;if all 32 were used, CF is set
  76.           mov    bx,65535               ;in case all 32 bits were used
  77. ;
  78. checkem:  mov    dx,ax                  ;guess2 ax,dx
  79.           mov    cx,bx                  ;guess1 bx,cx
  80. ;
  81. logit:    shl    cx,1                   ;guess1
  82.           jc     average                ;necessary for very large integers
  83.           cmp    cx,dx                  ;larger than guess2?
  84.           jae    average                ;if not, guess2/2
  85.           shr    dx,1
  86.           mov    ax,dx
  87.           mov    bx,cx
  88.           jmp    short logit
  89.                                         ;ready for averaging
  90. average:
  91.           add    bx,ax
  92.           rcr    bx,1                   ;average value
  93. Newton:   
  94.           REPT   2
  95.           mov    ax,si           ;lower 16
  96.           mov    dx,di           ;prepare for division, upper 16 in dx
  97.           cmp    bx,dx                  ;needed for really BIG ints
  98.           je     quit                   ; "
  99.           div    bx              ;ax still has target, bx first guess
  100.           add    bx,ax           ;newton
  101.           rcr    bx,1
  102.           endm
  103. ;
  104. quit:     inc    di
  105.           jz     done
  106.           jmp    CPU
  107. ;
  108. done:     call   update
  109. ;          
  110. ;         square root via 8087
  111. ;  if you need roots of 7FFF:FFFF and less, BIGGUN can be 32 bits, 
  112. ;  and ROOTP can be a 16 bits.  The extra length is needed here because
  113. ;  the 8087 does not expect unsigned integers.
  114. ;
  115.           xor    di,di                  ;8087 loads from memory, 
  116.           mov    ds:[bp],si             ;not regs directly
  117. NDP:      mov    ds:[bp+2],di
  118.          fild   biggun                 ;put integer into 8087 stack
  119.          fsqrt
  120.          fistp  rootp                  ;store to memory, too
  121.          fwait
  122. ;
  123. ; we now have an 8087 square root =rootp
  124. ;
  125.           inc    di
  126.           jnz    NDP 
  127.  
  128.           call   update
  129.           pop    bp
  130.           xor    al,al
  131.           mov    ah,4Ch
  132.           int    21h
  133. ;
  134. ;
  135. ;         output a hex word in decimal
  136. ;
  137. ;         CX,AX,DX destroyed
  138. ;
  139. dec_out   proc   near
  140.           xor    cx,cx
  141. another:  inc    cx
  142.           xor    dx,dx
  143.           div    base                   ;base is 10 decimal!
  144.           push   dx                     ;remainder is less sig digits
  145.           or     ax,ax                  ;is the quotient zero?
  146.           jnz    another                ;if not, more number to convert
  147. print_dig:
  148.           pop    dx                     ;retrive digit from stack
  149.           add    dl,'0'                 ;ascii offset
  150.           call   char_out
  151.           loop   print_dig              ;do all of the digits
  152.           ret
  153. dec_out   endp
  154. ;
  155. ;         output a single character
  156. ;
  157. char_out  proc   near
  158.           mov    ah,2                   ;output char function
  159.           int    21h                    ;do it
  160.           ret
  161. char_out  endp
  162. ;
  163. ;
  164. update    proc  near
  165.           mov    ah,2ch                 ;get dos time
  166.           int    21h                    ;hour in ch, mins in cl,secs in dh
  167.           mov    uper,cl
  168.           mov    secs,dh
  169.           mov    hnds,dl
  170.           mov    al,ch
  171.           xor    ah,ah
  172.           call   dec_out
  173. ;
  174.           time_print uper,':'
  175. ;
  176.           time_print secs,':'
  177. ;
  178.           time_print hnds,'.'
  179. ;
  180.           crlf
  181.           ret
  182. update    endp
  183. ;
  184. code      ends 
  185.           end    sqrt   
  186.  
  187.  
  188.