home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / TRYHEAP.ZIP / TRYHEAPD.ASM < prev    next >
Assembly Source File  |  1989-03-16  |  9KB  |  264 lines

  1.         title   TRYHEAP Demo of HEAP.ASM routines
  2.         page    55,132
  3.  
  4. ; TRYHEAP.ASM   Demo of routines in HEAP.ASM (MS-DOS version)
  5. ; Copyright (C) 1989 Ziff Davis Communications
  6. ; PC Magazine * Ray Dunan
  7.  
  8. cr      equ     0dh                     ; ASCII carriage return
  9. lf      equ     0ah                     ; ASCII line feed
  10. hsize   equ     4096                    ; heap size     
  11.  
  12. stdin   equ     0                       ; standard input handle
  13. stdout  equ     1                       ; standard output handle
  14.  
  15. DGROUP  group   _DATA
  16.  
  17.  
  18. _DATA   segment word public 'DATA'
  19.  
  20. msg1    db      cr,lf
  21.         db      'Heap Manager Demonstration Program'
  22.         db      cr,lf
  23. msg1_len equ $-msg1
  24.  
  25. msg2    db      cr,lf
  26.         db      'Commands available:'
  27.         db      cr,lf,lf
  28.         db      'A nnnn       = Allocate block of nnnn bytes'
  29.         db      cr,lf
  30.         db      'R xxxx nnnn  = Reallocate block xxxx to nnnn bytes'
  31.         db      cr,lf
  32.         db      'F xxxx       = Free block xxxx'
  33.         db      cr,lf,lf
  34.         db      'All values are entered and displayed in hex.'
  35.         db      cr,lf
  36. msg2_len equ $-msg2
  37.  
  38. msg3    db      cr,lf,lf,'Enter command:  '
  39. msg3_len equ $-msg3
  40.  
  41. msg4    db      cr,lf,'Function failed!',cr,lf
  42. msg4_len equ $-msg4
  43.  
  44. msg5    db      cr,lf,'Initialization failed!',cr,lf
  45. msg5_len equ $-msg5
  46.         
  47. msg6    db      cr,lf,'Returned pointer: '
  48. msg6a   db      'XXXX',cr,lf
  49. msg6_len equ $-msg6
  50.  
  51. msg7    db      cr,lf,'Current heap contents:'
  52.         db      cr,lf,lf,'Base  Length  Owned/Free'
  53. msg7_len equ $-msg7
  54.  
  55. msg8    db      cr,lf
  56. msg8a   db      'XXXX   '               ; base address (hex)    
  57. msg8b   db      'XXXX   '               ; block length (hex)
  58. msg8c   db      'X'                     ; O = owned, F = free
  59. msg8_len equ $-msg8
  60.  
  61. ibuff   db      80 dup (?)              ; keyboard input buffer
  62.  
  63. heap    db      hsize dup (?)           ; heap storage area
  64.  
  65. _DATA   ends
  66.  
  67.  
  68. _TEXT   segment word public 'CODE'
  69.  
  70.         assume  cs:_TEXT,ds:_DATA
  71.  
  72.         extrn   hinit:near              ; initialize heap
  73.         extrn   halloc:near             ; allocate heap block
  74.         extrn   hrealloc:near           ; resize heap block
  75.         extrn   hfree:near              ; free heap block
  76.         extrn   htol:near               ; hex ASCII to binary
  77.         extrn   itoh:near               ; binary to hex ASCII
  78.  
  79. main    proc    far
  80.  
  81.         mov     ax,DGROUP               ; make our data segment
  82.         mov     ds,ax                   ; addressable 
  83.         mov     es,ax
  84.  
  85.         mov     bx,offset heap          ; heap base address
  86.         mov     ax,hsize                ; heap size
  87.         call    hinit                   ; initialize heap
  88.  
  89.         jnc     main1                   ; successful init, continue
  90.  
  91.         mov     dx,offset msg5          ; initialization failed,
  92.         mov     cx,msg5_len             ; display error message
  93.         call    pmsg                    ; and exit
  94.         jmp     main9
  95.  
  96. main1:  mov     dx,offset msg1          ; display sign-on message
  97.         mov     cx,msg1_len
  98.         call    pmsg
  99.  
  100.         mov     dx,offset msg2          ; display help message
  101.         mov     cx,msg2_len             ; listing available commands
  102.         call    pmsg
  103.  
  104.         call    hwalk                   ; walk & display heap
  105.  
  106. main2:  mov     dx,offset msg3          ; display prompt
  107.         mov     cx,msg3_len
  108.         call    pmsg       
  109.  
  110.                                         ; read keyboard...
  111.         mov     dx,offset ibuff         ; buffer address
  112.         mov     cx,80                   ; buffer length
  113.         mov     bx,stdin                ; standard input handle
  114.         mov     ah,3fh                  ; function 3fh = read
  115.         int     21h                     ; transfer to MS-DOS
  116.  
  117.         cmp     ax,2                    ; anything entered?
  118.         je      main9                   ; no, terminate program
  119.  
  120.         mov     ax,word ptr ibuff       ; get first 2 chars
  121.         or      ax,0020h                ; lower-case the command
  122.  
  123.         cmp     ax,' a'                 ; allocate block command?
  124.         je      main3                   ; yes, jump
  125.  
  126.         cmp     ax,' r'                 ; reallocate block command?
  127.         je      main4                   ; yes, jump
  128.  
  129.         cmp     ax,' f'                 ; free block command?
  130.         je      main6                   ; yes, jump
  131.  
  132.         mov     dx,offset msg2          ; couldn't match command,
  133.         mov     cx,msg2_len             ; display list of available
  134.         call    pmsg                    ; commands
  135.  
  136.         jmp     main2                   ; get another command   
  137.  
  138. main3:                                  ; allocate block command
  139.  
  140.         mov     si,offset ibuff+2       ; convert block size    
  141.         call    htol
  142.  
  143.         call    halloc                  ; now request allocation
  144.         jc      main7                   ; jump if function failed
  145.         jmp     main5                   ; go display returned pointer
  146.  
  147. main4:                                  ; reallocate block command
  148.  
  149.         mov     si,offset ibuff+2       ; convert block pointer
  150.         call    htol
  151.  
  152.         mov     bx,ax                   ; convert new block size        
  153.         call    htol
  154.  
  155.         call    hrealloc                ; now request reallocation
  156.         jc      main7                   ; jump if function failed
  157.  
  158. main5:  mov     ax,bx                   ; function succeeded,
  159.         mov     bx,offset msg6a         ; convert returned pointer
  160.         call    itoh
  161.  
  162.         mov     dx,offset msg6          ; display returned pointer
  163.         mov     cx,msg6_len
  164.         call    pmsg
  165.  
  166.         jmp     main8                   ; go display heap
  167.  
  168. main6:                                  ; free block command
  169.  
  170.         mov     si,offset ibuff+2       ; convert block pointer
  171.         call    htol
  172.  
  173.         mov     bx,ax                   ; request release of block
  174.         call    hfree
  175.         jnc     main8                   ; jump if function successful
  176.  
  177. main7:  mov     dx,offset msg4          ; display 'Function failed!'
  178.         mov     cx,msg4_len
  179.         call    pmsg
  180.  
  181. main8:  call    hwalk                   ; walk & display heap
  182.         jmp     main2                   ; get another entry
  183.  
  184. main9:  mov     ax,4c00h                ; function 4CH = exit   
  185.         int     21h                     ; transfer to MS-DOS
  186.  
  187. main    endp
  188.  
  189. ;
  190. ; HWALK: displays address, length, and status of each heap block
  191. ;
  192. ; Call with:    nothing
  193. ;
  194. ; Returns:      nothing
  195. ;
  196. hwalk   proc    near
  197.  
  198.         mov     dx,offset msg7          ; display heading
  199.         mov     cx,msg7_len
  200.         call    pmsg
  201.  
  202.         mov     si,offset heap          ; address of start of heap
  203.         mov     di,si           
  204.         add     di,hsize                ; address of end of heap
  205.  
  206. hwalk1: cmp     si,di                   ; end of heap yet?
  207.         je      hwalk3                  ; yes, exit
  208.  
  209.         mov     ax,si                   ; convert block address
  210.         add     ax,2                    ; to ASCII
  211.         mov     bx,offset msg8a
  212.         call    itoh
  213.  
  214.         lodsw                           ; get length of block
  215.         mov     msg8c,'F'               ; assume block free
  216.         or      ax,ax                   ; test allocated bit
  217.         jns     hwalk2                  ; jump, block really free
  218.         mov     msg8c,'O'               ; indicate block owned
  219.  
  220. hwalk2: and     ax,7fffh                ; isolate length and
  221.         add     si,ax                   ; update block pointer
  222.  
  223.         mov     bx,offset msg8b         ; convert block length
  224.         call    itoh                    ; to ASCII
  225.  
  226.         mov     dx,offset msg8          ; display block information
  227.         mov     cx,msg8_len
  228.         call    pmsg
  229.  
  230.         jmp     hwalk1                  ; do next block
  231.  
  232. hwalk3: ret
  233.  
  234. hwalk   endp
  235.  
  236. ;
  237. ; PMSG: display message on standard output
  238. ;
  239. ; Call with:    DS:DX = message address
  240. ;               CX    = message length
  241. ;
  242. ; Returns:      nothing
  243. ;
  244. pmsg    proc    near   
  245.  
  246.         mov     bx,stdout               ; standard output handle
  247.         mov     ah,40h                  ; function 40h = write
  248.         int     21h                     ; transfer to MS-DOS
  249.         ret                             ; return to caller
  250.  
  251. pmsg    endp
  252.  
  253. _TEXT   ends
  254.  
  255.  
  256. STACK   segment para stack 'STACK'
  257.  
  258.         dw      64 dup (0)
  259.  
  260. STACK   ends
  261.  
  262.         end     main
  263.  
  264.