home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 31 / CDASC_31_1996_juillet_aout.iso / vrac / mt100.zip / MTMEM.ASM < prev    next >
Assembly Source File  |  1996-06-04  |  11KB  |  285 lines

  1. ; MTMem - Memory to store mtdirs and mtprev[] for Move To (mt.com)
  2. ; Copyright Jason Hood 1996.
  3. ; Started:   5 May, 1996.
  4. ; Finished:  2 June.
  5. ;
  6. ; Usage: in the autoexec.bat file place these statements:
  7. ;        set mtmem=####
  8. ;        mtmem [d:/path] [;path] [;;path]
  9. ; Where: [d:/path] is the path to place the directory structure file.
  10. ;                  Default is the same path as mtmem.com.
  11. ;        [;path] is the initial previous directory.
  12. ;                Default is an empty string ("Directory does not exist.").
  13. ;        [;;path] is the initial before previous directory.
  14. ;                 Default as above.
  15. ;        The environment variable will be set to the segment address of the
  16. ;        allocated memory.
  17. ;
  18. ; Assumptions: all parameters are valid
  19. ;              environment variable MTMEM is set correctly, or not set at all
  20. ;              DOS 3.0+ (5.0+ for high memory)
  21. ;              Memory is available
  22. ;
  23. ; Acknowledgements: Ralf Brown's interrupt list for PSP/environment information
  24. ;                   and his AMIS.ASM for allocating memory.
  25. ;                   Howard Vigorita's ATIM.ASM for the paragraph length
  26. ;                   calculation.
  27. ;
  28. ; You are free to use this code, or a portion thereof, as long as an appropriate
  29. ; acknowledgement is made.
  30. ;
  31. ; Questions, suggestions and comments to hoodj@topaz.cqu.edu.au.
  32.  
  33.         .model  tiny
  34.         .286p
  35.         .code
  36.  
  37.  
  38. MaxPath equ     80                      ; These values from dir.h
  39. MaxDir  equ     66
  40. len     equ     MaxPath + (2 * MaxDir)  ; Byte length to keep resident
  41.  
  42. intdos  MACRO   function, service
  43.         IFB     <service>
  44.         mov     ah, function
  45.         ELSE
  46.         mov     ax, (function shl 8) + service
  47.         ENDIF
  48.         int     21h
  49.         ENDM
  50.  
  51. byt     equ     <byte ptr>
  52. wrd     equ     <word ptr>
  53. aamhex  equ     <db 0d4h, 10h>          ; Split AL into AH and AL (12h => 0102h)
  54. aadhex  equ     <db 0d5h, 10h>          ; Merge AH and AL into AL (0102h => 12h)
  55.  
  56.  
  57.         org     16h                     ; Segment address of parent
  58. parent  dw      ?
  59.  
  60.         org     2ch                     ; Segment address of environment
  61. env     dw      ?
  62.  
  63.         org     81h                     ; Where the commandline starts
  64. cmdline db      ?
  65.  
  66.         org     100h                    ; Finally, the start of the program
  67.  
  68. start:  mov     prgseg, ds              ; Store the program's data segment
  69.         cld                             ; String op's left-to-right
  70.  
  71.         mov     ax, parent              ; Obtain the parent PSP
  72.         mov     ds, ax
  73.         mov     ax, env                 ; Parent environment
  74.         mov     es, ax
  75.         mov     ds, ax
  76.         xor     di, di                  ; First environment variable
  77.         xor     al, al                  ; Null terminator
  78.         mov     cx, 0ffffh              ; Ensure the entire env. is scanned
  79.  
  80. find:   cmp     byt [di], 'M'           ; Find 'MTMEM='
  81.         je      rest                    ; Got the 'M', check the rest
  82. scan:   repne   scasb                   ; Find the next env. variable
  83.         cmp     byt [di], 0             ; There are no more
  84.         jne     find
  85.  
  86.         mov     ds, cs:prgseg
  87.         lea     dx, enverr              ; Tell the user to set
  88.         intdos  9                       ; the environment variable
  89.         ret                             ; and exit
  90.  
  91. rest:   IRPC    M, TMEM=
  92.         inc     di
  93.         cmp     byt [di], '&M&'
  94.         jne     scan
  95.         ENDM
  96.         inc     di
  97.         mov     cs:pathst, di           ; In case I have to search for path
  98.  
  99.         cmp     byt [di], '#'           ; Is it already installed?
  100.         jne     installed
  101.  
  102.         mov     es, cs:prgseg           ; Restore ES to the program segment
  103.         mov     bx, 1000h               ; Make the program the 64k it should be
  104.         intdos  4ah
  105.         intdos  58h, 2                  ; Get current UMB state
  106.         push    ax                      ; and save it
  107.         intdos  58h, 0                  ; Get current allocation strategy
  108.         push    ax                      ; and save it
  109.         mov     bx, 1                   ; Link in UMB
  110.         intdos  58h, 3
  111.         mov     bx, 81h                 ; Best fit high then low
  112.         intdos  58h, 1
  113.         mov     bx, (len + 15) shr 4    ; Number of paragraphs to keep
  114.         intdos  48h                     ; Allocate the memory
  115.         dec     ax
  116.         mov     es,ax                   ; The MCB of the newly allocated memory
  117.         inc     ax
  118.         mov     word ptr es:[1],ax      ; Make the memory block own itself
  119.         mov     dx, ds                  ; Save the environment pointer
  120.         mov     bx, di
  121.         mov     cx, cs
  122.         dec     cx
  123.         mov     ds, cx                  ; MCB of the program
  124.         mov     si, 8
  125.         mov     di, si
  126.         movsw                           ; Copy the DOS 4.0+ program name into
  127.         movsw                           ; the new memory block's MCB
  128.         movsw
  129.         movsw
  130.         mov     ds, dx                  ; Retrieve the environment pointer
  131.         mov     di, bx
  132.         mov     dx, ax
  133.         call    write                   ; Modify the environment variable
  134.         pop     bx                      ; Restore allocation strategy
  135.         intdos  58h, 1
  136.         pop     bx                      ; Restore UMB state
  137.         intdos  58h, 3
  138.         jmp     finish
  139.  
  140. installed:                              ; The memory has already been allocated
  141.         or      cs:flags, 110b          ; Don't create the previous directories
  142.         call    read                    ; Get the segment into DX
  143.  
  144. finish: mov     es, dx                  ; Segment to place parameters
  145.         mov     ds, cs:prgseg
  146.         lea     si, cmdline
  147.         cmp     byt [si], 0dh           ; No parameters
  148.         je      done
  149.  
  150. param:  lodsb
  151.         cmp     al, ' '                 ; Ignore spaces
  152.         je      param
  153.         cmp     al, 9                   ; and replace tabs
  154.         jne     tstarg
  155.         mov     byt [si-1], ' '         ; with a space
  156.         jmp     param
  157.  
  158. tstarg: cmp     al, ';'                 ; Is it a previous directory?
  159.         je      prev
  160.         mov     di, 0                   ; Offset for directory structure path
  161.         or      flags, 001b             ; Indicate a path was specified
  162.         jmp     copy
  163. prev:   lodsb
  164.         cmp     al, ';'                 ; Before previous directory
  165.         je      bprev
  166.         mov     di, MaxPath             ; Offset for prev[0]
  167.         or      flags, 010b
  168.         jmp     copy
  169. bprev:  lodsb
  170.         mov     di, MaxPath+MaxDir      ; Offset for prev[1]
  171.         or      flags, 100b
  172.  
  173. copy:   cmp     al, ' '                 ; End of this parameter
  174.         je      null
  175.         cmp     al, 0dh
  176.         je      null
  177.         stosb
  178.         lodsb
  179.         jmp     copy
  180. null:   mov     byt es:[di], 0          ; Place the terminating null
  181.         cmp     di, MaxPath             ; If copying the path
  182.         jae     testsp                  ; point BX to its end
  183.         mov     bx, di
  184. testsp: cmp     al, ' '                 ; More parameters?
  185.         je      param
  186.  
  187. done:   test    flags, 001b             ; Was a path entered?
  188.         jnz     append                  ; Yes, so don't search for one
  189.  
  190.         mov     bx, es                  ; BX has the memory segment
  191.         mov     ax, env                 ; Get the environment segment
  192.         mov     dx, ax                  ; DX has the environment segment
  193.         mov     es, ax
  194.         mov     di, pathst              ; At the #### of MTMEM=####
  195.         add     di, 4                   ; Point to its terminating null
  196.         xor     al, al
  197.         mov     cx, 0ffffh
  198. envend: repne   scasb                   ; End of environment variable
  199.         cmp     byt es:[di], 0          ; End of environment?
  200.         jne     envend
  201.         inc     di                      ; Point past the word stored after
  202.         inc     di                      ; the environment variables to get
  203.         inc     di                      ; the path of mtmem.com
  204.         mov     si, 0                   ; Swap source and destination
  205.         xchg    si, di                  ; for the copy
  206.         mov     ds, dx
  207.         mov     es, bx
  208. mvpath: lodsb
  209.         or      al, al                  ; Terminating null?
  210.         jz      initap
  211.         stosb
  212.         cmp     al, '\'
  213.         jne     mvpath
  214.         mov     bx, di                  ; BX will point to "MTMEM.COM"
  215.         jmp     mvpath
  216.  
  217. initap: mov     ds, cs:prgseg           ; Restore DS to the program segment
  218. append: mov     di, bx                  ; The end of the path
  219.         cmp     byt es:[di-1], '/'      ; Is there a trailing slash
  220.         je      app
  221.         cmp     byt es:[di-1], '\'      ; or backslash?
  222.         je      app
  223.         mov     byt es:[di], '/'        ; No, so add one
  224.         inc     di
  225. app:    lea     si, mtdirs              ; The filename to append
  226.         movsw                           ; It's ten characters
  227.         movsw                           ; which means five words
  228.         movsw
  229.         movsw
  230.         movsw
  231.         mov     byt es:[di], 0          ; Terminating null
  232.  
  233.         test    flags, 010b             ; Need to initialise the
  234.         jnz     nxtprv                  ; previous directory
  235.         mov     di, MaxPath
  236.         mov     byt es:[di], 0
  237. nxtprv: test    flags, 100b             ; Need to initialise the
  238.         jnz     fin                     ; before previous directory
  239.         mov     di, MaxPath+MaxDir
  240.         mov     byt es:[di], 0
  241.  
  242. fin:    intdos  4ch                     ; Terminate the program
  243.  
  244.  
  245. ; Store dx at ds:di as a four digit hex number, but instead of using A to F,
  246. ; use :;<=>? (ie. 3Ah to 3Fh) to simplify processing.
  247.  
  248. write:  mov     al, dh
  249.         aamhex
  250.         or      ax, 3030h               ; Convert to ASCII
  251.         xchg    al, ah                  ; Swap them around so they can
  252.         mov     wrd [di], ax            ; be stored in the right order
  253.         mov     al, dl
  254.         aamhex
  255.         or      ax, 3030h
  256.         xchg    al, ah
  257.         mov     wrd [di+2], ax
  258.         ret
  259.  
  260. ; Read the four digit hex number at ds:di and put it in dx. See above.
  261. read:   mov     ax, wrd [di]            ; High word
  262.         xchg    al, ah                  ; Restore the order
  263.         and     ax, 0f0fh               ; Strip the ASCII
  264.         aadhex
  265.         mov     dh, al
  266.         mov     ax, wrd [di+2]          ; Low word
  267.         xchg    al, ah
  268.         and     ax, 0f0fh
  269.         aadhex
  270.         mov     dl, al
  271.         ret
  272.  
  273.  
  274. prgseg  dw      ?                       ; The segment the program's in
  275. pathst  dw      ?                       ; Where to start searching for the path
  276. flags   db      0                       ; Bit 0 - 0 to find path
  277.                                         ;     1 - 0 to init. previous dir.
  278.                                         ;     2 - 0 to init. before prev. dir.
  279. mtdirs  db      'MTDIRS.DAT'            ; Filename to append to path
  280. enverr  db      13,10,'You must "SET MTMEM=####" before running this program.'
  281.         db      13,10,10,'$'
  282.  
  283.  
  284. end     start
  285.