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

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