home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / asmlib.zip / BARGRAPH.ASM < prev    next >
Assembly Source File  |  1992-06-04  |  4KB  |  229 lines

  1. ; BARGRAPH.ASM - Sample program for ASMLIB
  2. ; Programmer: Douglas Herr
  3. ; date: 5/24/1992
  4. ;
  5. ; BARGRAPH displays a simple bar graph on the screen, then waits
  6. ; for any keypress to return to DOS.
  7. ; BARGRAPH has not been optimized particularly, so it's likely
  8. ; that much could be done to make it smaller and/or faster.
  9. ; BARGRAPH is intended as an illustration of ASMLIB use.
  10.  
  11. include    asm.inc
  12.  
  13. public  bargraph
  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    getkey:proc, gcolor:proc, fillpattern:proc
  23. extrn    maxi2:proc, mini2:proc, strlen:proc
  24.  
  25. .data
  26. extrn    x0:word        ; xmin
  27. extrn    y0:word        ; ymin
  28. extrn    x1:word        ; xmax
  29. extrn    y1:word        ; ymax
  30.  
  31. color    dw 1
  32.  
  33.  
  34. ; bar graph data for y-axis
  35. ; any integers from -32768 to 32767 may be used
  36. bardata    dw 7,3,5,2,6,10,17,18,19,21
  37. npoints    equ ($-bardata) shr 1
  38.  
  39. ; ten fill patterns defined, all 8 bytes long plus NUL
  40. pattern    db 8 dup (10101010b),0
  41.  
  42.     db 10000001b,01000010b,00100100b,00011000b
  43.     db 00011000b,00100100b,01000010b,10000001b,0
  44.  
  45.     db 8 dup (10001000b),0
  46.  
  47.     db 01111110b,10111101b,11011011b,11100111b
  48.     db 11100111b,11011011b,10111101b,01111110b,0
  49.  
  50.     db 4 dup (0FFh,1),0
  51.  
  52.     db 0FFh,1,1,1,1,1,0,0,0
  53.  
  54.     db 8 dup (11001100b),0
  55.     db 8 dup (11110000b),0
  56.  
  57.     db 11000000b,01100000b,00110000b,00011000b
  58.     db 00001100b,00000110b,00000011b,10000001b,0
  59.  
  60.     db 10001000b,10001000b,01000100b,01000100b
  61.     db 00100010b,00100010b,00010001b,00010001b,0
  62.  
  63. ; titles
  64.  
  65. extrn    teenager:byte
  66. extrn    subtitle:byte
  67. extrn    xaxis:byte
  68. extrn    cost:byte
  69.  
  70. .code
  71. bargraph    proc
  72.     call    graphmode
  73.     jnc    b0        ; continue if no error
  74.     mov    ah,9        ; else use DOS to write error message
  75.     int    21h
  76.     jmp    exit        ; & exit
  77.  
  78. b0:    push    ds
  79.     pop    es
  80.     lea    di,bardata
  81.     mov    cx,npoints
  82.     call    maxi2
  83.     shl    ax,1
  84.     mov    bx,di
  85.     add    bx,ax
  86.     mov    ax,[bx]        ; get maximum value
  87.     mov    y1,ax
  88.  
  89.     call    mini2
  90.     shl    ax,1
  91.     mov    bx,di
  92.     add    bx,ax
  93.     mov    ax,[bx]        ; get minimum value
  94.     or    ax,ax        ; use 0 if min > 0
  95.     js    b1
  96.     xor    ax,ax
  97. b1:    mov    y0,ax
  98.  
  99.     mov    x0,0
  100.     mov    x1,npoints
  101.  
  102.     push    x0
  103.     push    y0
  104.     push    x1
  105.     push    y1
  106.  
  107. ; establish coordinates, leaving room at edges for titles
  108.     mov    cl,3
  109.     call    set_coordinates
  110.  
  111.         lea     bx,x0
  112.         call    ucinit
  113.  
  114. ; draw the bars
  115.     mov    color,1
  116.     mov    x0,0
  117.     mov    x1,1
  118.     mov    y0,0
  119.     lea    si,bardata
  120.     lea    dx,pattern
  121.     cld
  122.     mov    cx,npoints    ; number of data points
  123. bar:    mov    ax,color
  124.     call    gcolor
  125.     mov    bx,dx        ; point to next pattern
  126.     add    dx,9
  127.     call    fillpattern
  128.     lodsw
  129.     mov    y1,ax
  130.     lea    bx,x0
  131.     call    uc2sys
  132.  
  133. ; provide clearance between bars
  134.     inc    word ptr [bx]
  135.     call    fillbox
  136.     mov    ax,color
  137.     or    ax,8        ; bright color for bar outline
  138.     call    gcolor
  139.     call    drawbox
  140.     inc    x0
  141.     inc    x1
  142.  
  143. ; update color attribute; limit fill color to 1-7
  144.     inc    color
  145.     and    color,7
  146.     jnz    bar9
  147.     mov    color,1
  148. bar9:    loop    bar
  149.  
  150.     pop    y1
  151.     pop    x1
  152.     pop    y0
  153.     pop    x0
  154.  
  155. ; draw the graph axes
  156.     mov    ax,15
  157.     call    gcolor
  158.     push    y0
  159.     push    y1
  160.     mov    ax,0
  161.     mov    y0,ax
  162.     mov    y1,ax
  163.     lea    bx,x0
  164.     call    uc2sys
  165.     call    drawline
  166.     pop    y1
  167.     pop    y0
  168.     mov    ax,x0
  169.     mov    x1,ax
  170.     lea    bx,x0
  171.     call    uc2sys
  172.     call    drawline
  173.  
  174. ; print main title
  175.     mov    y0,0
  176.     lea    dx,x0
  177.     lea    si,teenager
  178.     call    gcenter
  179.  
  180. ; print subtitle
  181.     call    smalltext
  182.     lea    si,subtitle
  183.     mov    y0,16
  184.     call    gcenter
  185.  
  186. ; print x-axis label
  187.     call    viewlimit
  188.     mov    ax,2[bx]    ; maximum y
  189.     push    ax
  190.     sub    ax,10
  191.     mov    y0,ax
  192.     lea    si,xaxis
  193.     call    gcenter
  194.  
  195. ; print y-axis label
  196.     lea    bx,cost
  197.     call    strlen
  198.     mov    si,bx
  199.     mov    bx,cx
  200.     mov    cl,3
  201.     shl    bx,cl        ; length of string in pixels
  202.     pop    ax        ; maximum y
  203.     add    ax,bx        ; maximum y plus len(string)
  204.     shr    ax,1
  205.     mov    y0,ax
  206.     mov    x0,0
  207.     call    gprintup
  208.  
  209. ; wait for any keypress, then return to text mode
  210.     call    getkey
  211.     call    textmode
  212. exit:    ret
  213.  
  214. bargraph    endp
  215.  
  216. ; SET_COORDINATES is outside MYMAIN because it'a always a near call
  217. set_coordinates:
  218.     lea    si,x0        ; adjust x-coordinates
  219.     call    s0
  220.     add    si,2        ; now do it for y
  221. s0:    mov    ax,4[si]
  222.     sub    ax,[si]        ; delta x
  223.     shr    ax,cl
  224.     sub    [si],ax
  225.     add    4[si],ax
  226.     ret
  227.  
  228.     end
  229.