home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / ASM / ASMLIB40.ZIP / ASM4DEMO.ZIP / CURVEFIT.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-06-20  |  6.1 KB  |  381 lines

  1. ; CURVEFIT.ASM - Sample program for ASMLIB
  2. ; Programmer: Douglas Herr
  3. ; date: 5/24/1992
  4. ;
  5. ; CURVEFIT displays a series of points on the screen and calculates
  6. ; the equations for curves to fit the data.
  7. ; CURVEFIT has not been optimized particularly, so it's likely
  8. ; that much could be done to make it smaller and/or faster.
  9. ; CURVEFIT is intended as an illustration of ASMLIB use.
  10.  
  11. include    asm.inc
  12.  
  13. public  curvefit
  14.  
  15. ; non-library external subroutines
  16. extrn    graphmode:proc, textmode:proc, needs87:proc
  17.  
  18. ; standard library external subroutines
  19. extrn   ucinit:proc, uc2sys:proc, viewlimit:proc
  20. extrn   drawline:proc, drawbox:proc, fillbox:proc
  21. extrn    gcenter:proc, gprintup:proc, smalltext:proc
  22. extrn    gprint:proc, stdtext:proc, mathchip:proc
  23. extrn    getkey:proc, gcolor:proc, fillpattern:proc
  24. extrn    maxi2:proc, mini2:proc, strlen:proc
  25. extrn    linefiti2:proc, quadfiti2:proc, cubefiti2:proc
  26. extrn    psolvef4:proc
  27.  
  28.  
  29. .data
  30. ; bar graph data for y-axis
  31. ; any integers from -32768 to 32767 may be used
  32. bardata    dw 0,50,150,140,160,100,50,60,150,210,420,450,520
  33. npoints    equ ($-bardata) shr 1
  34.  
  35. extrn    x0:word        ; xmin
  36. extrn    y0:word        ; ymin
  37. extrn    x1:word        ; xmax
  38. extrn    y1:word        ; ymax
  39.  
  40. a    dd 0
  41. b    dd 0
  42. c    dd 0
  43. d    dd 0
  44.  
  45. fit_data    label word
  46.     dw (npoints shl 1) dup ('■■')
  47.  
  48. color    dw 1
  49. star    db '*',0
  50.  
  51.  
  52. ; titles
  53.  
  54. extrn    teenager:byte
  55. extrn    subtitle:byte
  56. extrn    xaxis:byte
  57. extrn    cost:byte
  58.  
  59. no_mathchip    db 'Math coprocessor required',0Dh,0Ah,7,'$'
  60. linefit        db ' linear fit',0
  61. quadfit        db ' quadratic fit',0
  62. cubefit        db ' cubic fit',0
  63. measured    db ' measured data',0
  64.  
  65. .code
  66. curvefit    proc
  67.     cld
  68.     call    needs87
  69.     call    graphmode
  70.     push    ds
  71.     pop    es
  72.     lea    di,bardata
  73.     mov    cx,npoints
  74.     call    maxi2
  75.     shl    ax,1
  76.     mov    bx,di
  77.     add    bx,ax
  78.     mov    ax,[bx]        ; get maximum value
  79.     mov    y1,ax
  80.  
  81.     call    mini2
  82.     shl    ax,1
  83.     mov    bx,di
  84.     add    bx,ax
  85.     mov    ax,[bx]        ; get minimum value
  86.     or    ax,ax        ; use 0 if min > 0
  87.     js    b1
  88.     xor    ax,ax
  89. b1:    mov    y0,ax
  90.  
  91.     mov    x0,0
  92.     mov    x1,npoints
  93.  
  94.     push    x0
  95.     push    y0
  96.     push    x1
  97.     push    y1
  98.  
  99. ; establish coordinates, leaving room at edges for titles
  100.     mov    cl,3
  101.     call    set_coordinates
  102.  
  103.     lea    bx,x0
  104.         call    ucinit
  105.  
  106. ; plot the data
  107.     mov    ax,15
  108.     call    gcolor
  109.     mov    x0,0
  110.     mov    x1,1
  111.     mov    y1,0
  112.     lea    si,bardata
  113.     mov    cx,npoints    ; number of data points
  114.     call    smalltext
  115. point:    push    cx
  116.     lodsw
  117.     push    si
  118.     mov    y0,ax
  119.     lea    bx,x0
  120.     call    uc2sys
  121.     mov    ax,4[bx]
  122.     sub    ax,[bx]
  123.     shr    ax,1
  124.     sub    ax,4        ; center the marker on the point
  125.     add    [bx],ax
  126.     sub    word ptr 2[bx],4
  127.  
  128.     mov    dx,bx        ; DS:[DX] points to position data
  129.     lea    si,star
  130.     call    gprint
  131.     inc    x0
  132.     inc    x1
  133.     pop    si
  134.     pop    cx
  135.     loop    point
  136.  
  137.     pop    y1
  138.     pop    x1
  139.     pop    y0
  140.     pop    x0
  141.  
  142. ; draw the graph axes
  143.     mov    ax,15
  144.     call    gcolor
  145.     push    y0
  146.     push    y1
  147.     mov    ax,0
  148.     mov    y0,ax
  149.     mov    y1,ax
  150.     lea    bx,x0
  151.     call    uc2sys
  152.     call    drawline
  153.     pop    y1
  154.     pop    y0
  155.     mov    ax,x0
  156.     mov    x1,ax
  157.     lea    bx,x0
  158.     call    uc2sys
  159.     call    drawline
  160.  
  161. ; a simple descriptive legend
  162.     mov    ax,9        ; linear fit color
  163.     call    gcolor
  164.     mov    ax,2[bx]    ; y0
  165.     cmp    ax,6[bx]
  166.     jb    c5
  167.     mov    ax,6[bx]
  168. c5:    add    ax,4
  169.     mov    2[bx],ax
  170.     mov    6[bx],ax
  171.     mov    ax,[bx]
  172.     cmp    ax,4[bx]
  173.     jb    c6
  174.     mov    ax,4[bx]
  175. c6:    add    ax,10
  176.     mov    [bx],ax        ; line length
  177.     add    ax,30
  178.     mov    4[bx],ax
  179.     call    drawline
  180.  
  181.     lea    si,linefit
  182.     call    print_label
  183.  
  184. ; quadratic label
  185.     mov    ax,12
  186.     call    gcolor
  187.     mov    ax,2[bx]
  188.     add    ax,12
  189.     mov    2[bx],ax
  190.     mov    6[bx],ax
  191.     call    drawline
  192.  
  193.     mov    ax,4[bx]
  194.     lea    si,quadfit
  195.     call    print_label
  196.  
  197. ; cubic label
  198.     mov    ax,14
  199.     call    gcolor
  200.     mov    ax,2[bx]
  201.     add    ax,12
  202.     mov    2[bx],ax
  203.     mov    6[bx],ax
  204.     call    drawline
  205.  
  206.     mov    ax,4[bx]
  207.     lea    si,cubefit
  208.     call    print_label
  209.  
  210. ; label for measured data
  211.     mov    ax,2[bx]
  212.     add    ax,12
  213.     mov    2[bx],ax
  214.     add    word ptr [bx],4
  215.     sub    word ptr 2[bx],4
  216.     mov    dx,bx
  217.     lea    si,star
  218.     call    gprint
  219.  
  220.     add    word ptr 2[bx],4
  221.     mov    ax,4[bx]
  222.     lea    si,measured
  223.     call    print_label
  224.  
  225. ; print main title
  226.     mov    ax,15        ; bright white
  227.     call    gcolor
  228.     call    stdtext        ; larger text if not CGA
  229.     mov    y0,0
  230.     lea    dx,x0
  231.     lea    si,teenager
  232.     call    gcenter
  233.  
  234. ; print subtitle
  235.     call    smalltext    ; 8x8 character size
  236.     lea    si,subtitle
  237.     mov    y0,16
  238.     call    gcenter
  239.  
  240. ; print x-axis label
  241.     call    viewlimit
  242.     mov    ax,2[bx]    ; maximum y
  243.     push    ax
  244.     sub    ax,10
  245.     mov    y0,ax
  246.     lea    si,xaxis
  247.     call    gcenter
  248.  
  249. ; print y-axis label
  250.     lea    bx,cost
  251.     call    strlen
  252.     mov    si,bx
  253.     mov    bx,cx
  254.     mov    cl,3
  255.     shl    bx,cl        ; length of string in pixels
  256.     pop    ax        ; maximum y
  257.     add    ax,bx        ; maximum y plus len(string)
  258.     shr    ax,1
  259.     mov    y0,ax
  260.     mov    x0,0
  261.     call    gprintup
  262.  
  263. ; convert data to linefit format
  264.     lea    di,fit_data
  265.     mov    bx,di        ; save for LineFit
  266.     push    ds
  267.     pop    es
  268.     mov    ax,1
  269.     mov    cx,npoints
  270.     lea    si,bardata
  271. c9:    stosw
  272.     movsw
  273.     inc    ax
  274.     loop    c9
  275.  
  276. ; calculate line equation
  277.     mov    cx,npoints
  278.     call    linefiti2
  279.  
  280.     fstp    a
  281.     fstp    b
  282.  
  283. ; solve for y given x
  284.     mov    ax,9
  285.     call    gcolor        ; bright blue
  286.     mov    dx,1        ; 1st order equation
  287.     call    solve0
  288.  
  289. ; calculate quadratic equation
  290.     lea    bx,fit_data
  291.     mov    cx,npoints
  292.     call    quadfiti2
  293.     fstp    a
  294.     fstp    b
  295.     fstp    c
  296.  
  297. ; solve for y given x
  298.     mov    ax,12        ; bright red
  299.     call    gcolor
  300.     mov    dx,2        ; 2nd order equation
  301.     call    solve0
  302.  
  303. ; calculate cubic equation
  304.     lea    bx,fit_data
  305.     mov    cx,npoints
  306.     call    cubefiti2
  307.     fstp    a        ; save equation coefficients
  308.     fstp    b
  309.     fstp    c
  310.     fstp    d
  311.  
  312. ; solve for y given x
  313.     mov    ax,14        ; yellow
  314.     call    gcolor
  315.     mov    dx,3        ; 3rd order equation
  316.     call    solve0
  317.  
  318. ; wait for any keypress, then return to text mode
  319.     call    getkey
  320.     call    textmode
  321. exit:    ret
  322.  
  323. curvefit    endp
  324.  
  325. ; SET_COORDINATES is outside MYMAIN because it'a always a near call
  326. ; this subroutine adjusts the user-defined coordinates to allow
  327. ; room on all sides for titles, etc.
  328. set_coordinates:
  329.     lea    si,x0        ; adjust x-coordinates
  330.     call    s0
  331.     add    si,2        ; now do it for y
  332. s0:    mov    ax,4[si]
  333.     sub    ax,[si]        ; delta x
  334.     shr    ax,cl
  335.     sub    [si],ax
  336.     add    4[si],ax
  337.     ret
  338.  
  339. solve0:    lea    si,a
  340.     mov    cx,npoints+1
  341.     mov    x0,0
  342.     mov    y0,0
  343.     mov    x1,0
  344. solve1:    push    cx
  345.     mov    cx,dx
  346.     finit
  347.     fild    x1
  348.     call    psolvef4
  349.     fistp    y1
  350.     cmp    x1,0
  351.     je    solve2
  352.     lea    bx,x0
  353.     call    uc2sys
  354.     call    drawline
  355. solve2:    push    x1
  356.     push    y1
  357.     pop    y0
  358.     pop    x0
  359.     inc    x1
  360.     pop    cx
  361.     loop    solve1
  362.     ret
  363.  
  364. print_label:
  365.     push    [bx]
  366.     push    2[bx]
  367.  
  368.     add    ax,3
  369.     mov    [bx],ax
  370.     mov    ax,15
  371.     call    gcolor
  372.     sub    word ptr 2[bx],3
  373.     mov    dx,bx
  374.     call    gprint
  375.  
  376.     pop    2[bx]
  377.     pop    [bx]
  378.     ret
  379.  
  380.     end
  381.