home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / asmlib.zip / CURVEFIT.ASM < prev    next >
Assembly Source File  |  1992-06-04  |  7KB  |  391 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
  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    mathchip
  69.     or    ax,ax
  70.     jnz    c0
  71.     lea    dx,no_mathchip
  72.     jmp    short c1
  73. c0:    call    graphmode
  74.     jnc    b0        ; continue if no error
  75. c1:    mov    ah,9        ; else use DOS to write error message
  76.     int    21h
  77.     jmp    exit        ; & exit
  78.  
  79. b0:    push    ds
  80.     pop    es
  81.     lea    di,bardata
  82.     mov    cx,npoints
  83.     call    maxi2
  84.     shl    ax,1
  85.     mov    bx,di
  86.     add    bx,ax
  87.     mov    ax,[bx]        ; get maximum value
  88.     mov    y1,ax
  89.  
  90.     call    mini2
  91.     shl    ax,1
  92.     mov    bx,di
  93.     add    bx,ax
  94.     mov    ax,[bx]        ; get minimum value
  95.     or    ax,ax        ; use 0 if min > 0
  96.     js    b1
  97.     xor    ax,ax
  98. b1:    mov    y0,ax
  99.  
  100.     mov    x0,0
  101.     mov    x1,npoints
  102.  
  103.     push    x0
  104.     push    y0
  105.     push    x1
  106.     push    y1
  107.  
  108. ; establish coordinates, leaving room at edges for titles
  109.     mov    cl,3
  110.     call    set_coordinates
  111.  
  112.     lea    bx,x0
  113.         call    ucinit
  114.  
  115. ; plot the data
  116.     mov    ax,15
  117.     call    gcolor
  118.     mov    x0,0
  119.     mov    x1,1
  120.     mov    y1,0
  121.     lea    si,bardata
  122.     mov    cx,npoints    ; number of data points
  123.     call    smalltext
  124. point:    push    cx
  125.     lodsw
  126.     push    si
  127.     mov    y0,ax
  128.     lea    bx,x0
  129.     call    uc2sys
  130.     mov    ax,4[bx]
  131.     sub    ax,[bx]
  132.     shr    ax,1
  133.     sub    ax,4        ; center the marker on the point
  134.     add    [bx],ax
  135.     sub    word ptr 2[bx],4
  136.  
  137.     mov    dx,bx        ; DS:[DX] points to position data
  138.     lea    si,star
  139.     call    gprint
  140.     inc    x0
  141.     inc    x1
  142.     pop    si
  143.     pop    cx
  144.     loop    point
  145.  
  146.     pop    y1
  147.     pop    x1
  148.     pop    y0
  149.     pop    x0
  150.  
  151. ; draw the graph axes
  152.     mov    ax,15
  153.     call    gcolor
  154.     push    y0
  155.     push    y1
  156.     mov    ax,0
  157.     mov    y0,ax
  158.     mov    y1,ax
  159.     lea    bx,x0
  160.     call    uc2sys
  161.     call    drawline
  162.     pop    y1
  163.     pop    y0
  164.     mov    ax,x0
  165.     mov    x1,ax
  166.     lea    bx,x0
  167.     call    uc2sys
  168.     call    drawline
  169.  
  170. ; a simple descriptive legend
  171.     mov    ax,9        ; linear fit color
  172.     call    gcolor
  173.     mov    ax,2[bx]    ; y0
  174.     cmp    ax,6[bx]
  175.     jb    c5
  176.     mov    ax,6[bx]
  177. c5:    add    ax,4
  178.     mov    2[bx],ax
  179.     mov    6[bx],ax
  180.     mov    ax,[bx]
  181.     cmp    ax,4[bx]
  182.     jb    c6
  183.     mov    ax,4[bx]
  184. c6:    add    ax,10
  185.     mov    [bx],ax        ; line length
  186.     add    ax,30
  187.     mov    4[bx],ax
  188.     call    drawline
  189.  
  190.     lea    si,linefit
  191.     call    print_label
  192.  
  193. ; quadratic label
  194.     mov    ax,12
  195.     call    gcolor
  196.     mov    ax,2[bx]
  197.     add    ax,12
  198.     mov    2[bx],ax
  199.     mov    6[bx],ax
  200.     call    drawline
  201.  
  202.     mov    ax,4[bx]
  203.     lea    si,quadfit
  204.     call    print_label
  205.  
  206. ; cubic label
  207.     mov    ax,14
  208.     call    gcolor
  209.     mov    ax,2[bx]
  210.     add    ax,12
  211.     mov    2[bx],ax
  212.     mov    6[bx],ax
  213.     call    drawline
  214.  
  215.     mov    ax,4[bx]
  216.     lea    si,cubefit
  217.     call    print_label
  218.  
  219. ; label for measured data
  220.     mov    ax,2[bx]
  221.     add    ax,12
  222.     mov    2[bx],ax
  223.     add    word ptr [bx],4
  224.     sub    word ptr 2[bx],4
  225.     mov    dx,bx
  226.     lea    si,star
  227.     call    gprint
  228.  
  229.     add    word ptr 2[bx],4
  230.     mov    ax,4[bx]
  231.     lea    si,measured
  232.     call    print_label
  233.  
  234. ; print main title
  235.     mov    ax,15        ; bright white
  236.     call    gcolor
  237.     call    stdtext        ; larger text if not CGA
  238.     mov    y0,0
  239.     lea    dx,x0
  240.     lea    si,teenager
  241.     call    gcenter
  242.  
  243. ; print subtitle
  244.     call    smalltext    ; 8x8 character size
  245.     lea    si,subtitle
  246.     mov    y0,16
  247.     call    gcenter
  248.  
  249. ; print x-axis label
  250.     call    viewlimit
  251.     mov    ax,2[bx]    ; maximum y
  252.     push    ax
  253.     sub    ax,10
  254.     mov    y0,ax
  255.     lea    si,xaxis
  256.     call    gcenter
  257.  
  258. ; print y-axis label
  259.     lea    bx,cost
  260.     call    strlen
  261.     mov    si,bx
  262.     mov    bx,cx
  263.     mov    cl,3
  264.     shl    bx,cl        ; length of string in pixels
  265.     pop    ax        ; maximum y
  266.     add    ax,bx        ; maximum y plus len(string)
  267.     shr    ax,1
  268.     mov    y0,ax
  269.     mov    x0,0
  270.     call    gprintup
  271.  
  272. ; convert data to linefit format
  273.     lea    di,fit_data
  274.     mov    bx,di        ; save for LineFit
  275.     push    ds
  276.     pop    es
  277.     mov    ax,1
  278.     mov    cx,npoints
  279.     lea    si,bardata
  280. c9:    stosw
  281.     movsw
  282.     inc    ax
  283.     loop    c9
  284.  
  285. ; calculate line equation
  286.     mov    cx,npoints
  287.     call    linefiti2
  288.  
  289.     fstp    a
  290.     fstp    b
  291.  
  292. ; solve for y given x
  293.     mov    ax,9
  294.     call    gcolor        ; bright blue
  295.     mov    dx,1        ; 1st order equation
  296.     call    solve0
  297.  
  298. ; calculate quadratic equation
  299.     lea    bx,fit_data
  300.     mov    cx,npoints
  301.     call    quadfiti2
  302.     fstp    a
  303.     fstp    b
  304.     fstp    c
  305.  
  306. ; solve for y given x
  307.     mov    ax,12        ; bright red
  308.     call    gcolor
  309.     mov    dx,2        ; 2nd order equation
  310.     call    solve0
  311.  
  312. ; calculate cubic equation
  313.     lea    bx,fit_data
  314.     mov    cx,npoints
  315.     call    cubefiti2
  316.     fstp    a        ; save equation coefficients
  317.     fstp    b
  318.     fstp    c
  319.     fstp    d
  320.  
  321. ; solve for y given x
  322.     mov    ax,14        ; yellow
  323.     call    gcolor
  324.     mov    dx,3        ; 3rd order equation
  325.     call    solve0
  326.  
  327. ; wait for any keypress, then return to text mode
  328.     call    getkey
  329.     call    textmode
  330. exit:    ret
  331.  
  332. curvefit    endp
  333.  
  334. ; SET_COORDINATES is outside MYMAIN because it'a always a near call
  335. ; this subroutine adjusts the user-defined coordinates to allow
  336. ; room on all sides for titles, etc.
  337. set_coordinates:
  338.     lea    si,x0        ; adjust x-coordinates
  339.     call    s0
  340.     add    si,2        ; now do it for y
  341. s0:    mov    ax,4[si]
  342.     sub    ax,[si]        ; delta x
  343.     shr    ax,cl
  344.     sub    [si],ax
  345.     add    4[si],ax
  346.     ret
  347.  
  348. ; SOLVE0 is outside MYMAIN because it's always a near call
  349. solve0:    lea    si,a
  350.     mov    cx,npoints+1
  351.     mov    x0,0
  352.     mov    y0,0
  353.     mov    x1,0
  354. solve1:    push    cx
  355.     mov    cx,dx
  356.     finit
  357.     fild    x1
  358.     call    psolvef4
  359.     fistp    y1
  360.     cmp    x1,0
  361.     je    solve2
  362.     lea    bx,x0
  363.     call    uc2sys
  364.     call    drawline
  365. solve2:    push    x1
  366.     push    y1
  367.     pop    y0
  368.     pop    x0
  369.     inc    x1
  370.     pop    cx
  371.     loop    solve1
  372.     ret
  373.  
  374. print_label:
  375.     push    [bx]
  376.     push    2[bx]
  377.  
  378.     add    ax,3
  379.     mov    [bx],ax
  380.     mov    ax,15
  381.     call    gcolor
  382.     sub    word ptr 2[bx],3
  383.     mov    dx,bx
  384.     call    gprint
  385.  
  386.     pop    2[bx]
  387.     pop    [bx]
  388.     ret
  389.  
  390.     end
  391.