home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d01xx / d0155.lha / AsmExamples / MemTool.a < prev    next >
Text File  |  1988-10-02  |  6KB  |  230 lines

  1. * Exercise in Assembly Language: A memory meter.
  2. * Same trouble as Screen program: eats a little ram at every
  3. * WB startup, probably the WBenchMsg not being deallocated
  4. * or something similarily intricate
  5.  
  6.    option nl
  7.    INCLUDE "intuition/intuition.i"
  8.    INCLUDE "graphics/text.i"
  9.    INCLUDE "graphics/rastport.i"
  10.  
  11.    option l
  12.    XREF _AbsExecBase
  13.  
  14.    XREF _LVOOpenLibrary
  15.    XREF _LVOCloseLibrary
  16.    XREF _LVODelay
  17.    XREF _LVOAvailMem
  18.    XREF _LVOText
  19.    XREF _LVOMove
  20.    XREF _LVOSetBPen
  21.    XREF _LVOWaitPort
  22.    XREF _LVOGetMsg
  23.    XREF _LVOSetWindowTitle
  24.    XREF _LVODisplayBeep
  25.    XREF _LVOOpenWindow
  26.    XREF _LVOCloseWindow
  27.    XREF _LVOSetWindowTitles
  28.    XREF _LVOExit
  29.  
  30. CHIPMEM EQU 2
  31. FASTMEM EQU 4
  32.  
  33.    lea      DosName,a1
  34.    clr.l    d0
  35.    movea.l  _AbsExecBase,a6
  36.    jsr      _LVOOpenLibrary(a6)
  37.    move.l   d0,DosBase
  38.    tst.l    d0                          ; Ought to make things secure.
  39.    bne      1$            ; Got DosBase
  40.    rts      ; Can't even get Dos library, let's get out of here!!!
  41.  
  42. 1$ lea      IntuitionName,a1
  43.    clr.l    d0
  44.    movea.l  _AbsExecBase,a6
  45.    jsr      _LVOOpenLibrary(a6)
  46.    move.l   d0,IntuiBase
  47.    tst.l    d0                          ; Ought to make things secure.
  48.    beq      error
  49.  
  50.    lea      GraphicsName,a1
  51.    clr.l    d0
  52.    movea.l  _AbsExecBase,a6
  53.    jsr      _LVOOpenLibrary(a6)
  54.    move.l   d0,GraphicsBase
  55.    tst.l    d0                          ; Ought to make things secure.
  56.    beq      error
  57.  
  58.    movea.l  IntuiBase,a6
  59.    lea      MyNewWindow,a0
  60.    jsr      _LVOOpenWindow(a6)          ; Error checking
  61.    tst.l    d0
  62.    beq      error
  63.    move.l   d0,MyWindow
  64.  
  65. * To Set Screen Title:
  66.    move.l   d0,a0
  67.    lea      WindowTitle,a1
  68.    lea      ScreenTitle,a2
  69.    movea.l  IntuiBase,a6
  70.    jsr      _LVOSetWindowTitles(a6)
  71.  
  72. DisplayMem:
  73. * First, make sure there are spaces in the string, not old digits:
  74.    move.l   #$20202020,d3       ; Four Spaces
  75.    move.l   d3,ChipCount+6
  76.    move.l   d3,ChipCount+10
  77.    move.l   d3,FastCount+6
  78.    move.l   d3,FastCount+10
  79.    
  80. * Now, get and display ram figures
  81.    movea.l  _AbsExecBase,a6
  82.    moveq    #FASTMEM,d1
  83.    jsr      _LVOAvailMem(a6)    ; Gets Fast ram count
  84.    move.l   d0,d4        ; Save result to test for ram low
  85.    lea      FastCount+15,a0
  86.    jsr      Convert
  87.  
  88.    movea.l  _AbsExecBase,a6
  89.    moveq    #CHIPMEM,d1
  90.    jsr      _LVOAvailMem(a6)
  91.    add.l    d0,d4
  92.    cmpi.l   #$C000,d4    ; If ram is < 48k, we commit suicide
  93.    ble        Suicide
  94.    lea      ChipCount+15,a0
  95.    jsr      Convert
  96.  
  97. * Here, we've got the text strings. 
  98.  
  99. * Get a proper position to put text
  100.    movea.l  GraphicsBase,a6
  101.    move.l   MyWindow,a2
  102.    move.l   wd_RPort(a2),a1
  103.    moveq    #17,d1              ; Desired position
  104.    moveq    #6,d0
  105.    jsr      _LVOMove(a6)
  106.  
  107. * Put it:
  108.    movea.l  GraphicsBase,a6
  109.    move.l   MyWindow,a2
  110.    move.l   wd_RPort(a2),a1
  111.    lea      FastCount,a0
  112.    moveq    #15,d0              ; String Length
  113.    jsr      _LVOText(a6)
  114.  
  115. * Do the same with chip text
  116.    movea.l  GraphicsBase,a6
  117.    move.l   MyWindow,a2
  118.    move.l   wd_RPort(a2),a1
  119.    moveq    #27,d1              : Where we want to write
  120.    moveq    #6,d0
  121.    jsr      _LVOMove(a6)
  122.  
  123.    movea.l  GraphicsBase,a6
  124.    move.l   MyWindow,a2
  125.    move.l   wd_RPort(a2),a1
  126.    lea      ChipCount,a0
  127.    moveq    #15,d0              ; String Length
  128.    jsr      _LVOText(a6)
  129.  
  130.    movea.l  DosBase,a6
  131.    moveq    #100,d1                     ; Wait 2 seconds
  132.    jsr      _LVODelay(a6)
  133.  
  134.    move.l   MyWindow,a0
  135.    move.l   wd_UserPort(a0),a0          ;Get the IDCMP Port of the window
  136.    movea.l  _AbsExecBase,a6
  137.    jsr      _LVOGetMsg(a6)            ; Test for close gadget
  138.    tst.l    d0                  ; Only Message we can get is CLOSEWINDOW
  139.    beq      DisplayMem          ; If NULL ptr, loop back
  140.  
  141. * Getting Near the end.
  142. exit:
  143.    movea.l  IntuiBase,a6
  144.    movea.l  MyWindow,a0
  145.    jsr      _LVOCloseWindow(a6)
  146.  
  147. error:        ; OpenWindow or some OpenLibrary failed
  148.    movea.l  _AbsExecBase,a6
  149.    move.l   IntuiBase,a1
  150.    jsr      _LVOCloseLibrary(a6) ; This is not really needed
  151.    clr.l    d1            ; Return Code always zero
  152.    jsr        _LVOExit(a6)    ; Done
  153.    clr.l    d0
  154.    rts                          ; Well, almost done. It seems Exit sends us
  155.                                 ; back here. We'll return completely now.
  156.  
  157. Suicide:        ; If ram is running low, we'll give ours up.
  158.     lea    0,a0    ; To beep every screen. This is not a very
  159.     movea.l    IntuiBase,a6    ; polite thing to do, but low ram is a disaster
  160.     jsr    _LVODisplayBeep(a6) 
  161.     bra    exit    ; Now leave
  162.     
  163. Convert:
  164. * Inputs: adress in a0, data i d0.
  165. * Output: Numeric string in ASCII before the address, data spent.
  166.    move.l   d0,d1       ; Divide long word by 10 may cause overflow.
  167.    andi.l   #$FFFF0000,d1
  168.    swap     d1
  169.  
  170. nextdigit:
  171.    divu     #10,d1
  172.    move.w   d1,d2       ; The very high part of the number
  173.    ext.l    d2
  174.    andi.l   #$FFFF0000,d1
  175.    andi.l   #$0000FFFF,d0
  176.    add.l    d1,d0
  177.    divu     #10,d0
  178.    swap     d0
  179.    addi.b   #48,d0      ; low byte now contains ASCII digit
  180.    move.b   d0,-(a0)    ; Move to string
  181.    swap d0              ; Move remaining number back
  182.    andi.l   #$0000FFFF,d0 ; Wipe out the digit.
  183.    move.l   d2,d1       ; Put the high part where we need it
  184.    add.l    d0,d2       ; This to test for the end: d2 is scratch, containing
  185.    bne      nextdigit   ; data that have been copied to d1. We can then use it
  186.                         ; to determine if we're done.
  187.  
  188.    rts                          ; All digits gone to the string.
  189.  
  190. * Data section
  191.  
  192. **************** Window data
  193.  
  194. MyWindow:
  195.    dc.l 0
  196.  
  197. MyFlags       EQU SMART_REFRESH!WINDOWDEPTH!WINDOWDRAG!WINDOWCLOSE
  198. MyNewWindow:  dc.w 0,30,132,30
  199.               dc.b 1,2          ; Colours
  200.               dc.l CLOSEWINDOW  ; IDCMP Flags
  201.               dc.l MyFlags
  202.               dc.l 0,0
  203.               dc.l WindowTitle
  204.               dc.l 0            ; Screen to be filled in later
  205.               dc.l 0            ; Pointer to bitmap
  206.               dc.w 0,0,0,0
  207.               dc.w WBENCHSCREEN
  208.  
  209. WindowTitle:
  210.    dc.b 'Memory:',0
  211. ScreenTitle
  212.    dc.b 'Assembly Language is fun!',0
  213. IntuitionName:
  214.    dc.b 'intuition.library',0
  215. IntuiBase:
  216.    dc.l 0
  217. GraphicsName:
  218.    dc.b 'graphics.library',0,0
  219. GraphicsBase:
  220.    dc.l 0
  221. DosName:
  222.    dc.b 'dos.library',0
  223. DosBase:
  224.    dc.l 0
  225. ChipCount:
  226.    dc.b 'Chip:          ',0
  227. FastCount:
  228.    dc.b 'Fast:          ',0
  229.  
  230.