home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / assemblr / library / psp_io / psp.asm < prev    next >
Assembly Source File  |  1994-05-28  |  11KB  |  246 lines

  1. ;****************************************************************************
  2. ;                               P S P . A S M
  3. ;============================================================================
  4. ; Functions to process data in the Program Segment Prefix (PSP). 
  5. ;---------------------------------------------------------------------------
  6. ; Copyright (c) Simon Groes, 1994
  7. ;---------------------------------------------------------------------------
  8. ; Assemble with Borland Turbo Assembler v3.0
  9. ;****************************************************************************
  10.  
  11.         IDEAL
  12.         DOSSEG
  13.         MODEL   small
  14.  
  15.         LOCALS
  16.  
  17. ;-----  Insert INCLUDE "filename" directives here
  18.         INCLUDE "macro.inc"
  19.         INCLUDE "common.inc"
  20.  
  21. ;-----  Insert EQU and = equates here
  22.         TAIL_LENGTH     =       0080h           ; Offset of PSP length byte.
  23.         COMMAND_TAIL    =       0081h           ; Offset of command tail.
  24.         BUFLEN          =       189             ; (see below).
  25.  
  26.  
  27.         DATASEG
  28.  
  29. ;-----  Declare all PUBLIC variables here:
  30.  
  31. ;-----  Declare other variables with DB, DW, etc., here
  32.  
  33.         cmdBuf  db      BUFLEN DUP (0)     ; Buffer to hold edited command tail.
  34.       ;---------------------------------------------------------
  35.       ; Note that the command tail in the PSP has a maximum of 127 chars,
  36.       ; including <CR>, while this buffer can accommodate 189 chars. 
  37.       ; This is to allow for the unlikely situation when the command tail
  38.       ; consists of 63 1-char switches (e.g. /x), a terminating <CR> & no 
  39.       ; spaces in between. However, each switch will be copied with a 
  40.       ; terminating NULL char (total 62) to this buffer.
  41.       ; i.e. 126 + 62 + 1 = 189
  42.       ;---------------------------------------------------------
  43.         cBufLen db      BUFLEN          ; Used in GetNextParam.
  44.  
  45.         nParams         db      0       ; # of parameters (incl switches).
  46.         nSwitches       db      0       ; # of switches.
  47.  
  48.  
  49. ;-----  Specify any EXTRN variables here
  50.  
  51.         CODESEG
  52.  
  53. ;-----  Declare PUBLIC procedures here
  54.         PUBLIC  PSP_Parameters, GetParamNumber, GetSwNumber, GetNextParam
  55.  
  56. ;===============================================================
  57. ; Procedure:    =*=PSP_Parameters=*=
  58. ;---------------------------------------------------------------
  59. ; Access:       Public - Available to other files.
  60. ; Task:         To copy the parameters in the command tail of the
  61. ;               PSP to a single buffer (supplied by the calling procedure)
  62. ;               as a series of NULL-terminated strings.
  63. ; Input:        none
  64. ; Output:       Parameters copied to buffer as a series of 
  65. ;               NULL-terminated strings.
  66. ;               di -> the buffer.
  67. ;               si -> the buffer.
  68. ; Registers:    ds -> Program's data segment.
  69. ;               di -> cmdBuf.
  70. ;               si -> cmdBuf.
  71. ;               All other registers unchanged.
  72. ; Note:         DS & ES registers always address the Program Segment
  73. ;               Prefix (PSP) when all .EXE programs begin.
  74. ;===============================================================
  75. PROC    PSP_Parameters
  76.  
  77.         SaveRegs <ax,bx,cx>             ; Save modified register(s).
  78.  
  79.         mov     ax, @data               ; ES = Data Segment.
  80.         mov     es, ax
  81.         ASSUME  es:@data
  82.         mov     di, OFFSET cmdBuf       ; DI -> cmdBuf
  83.         mov     bx, di                  ; BX -> cmdBuf for calculations.
  84.         push    di                      ; Save di.
  85.  
  86.         mov     cl, [ds:TAIL_LENGTH]    ; cl = Length of PSP command tail.
  87.         xor     ch, ch                  ; Clear rest of cx.
  88.         mov     si, COMMAND_TAIL        ; si -> command tail.
  89.         xor     al, al                  ; al = NULL
  90.  
  91. ;-----  JUMP LEADING SPACES & TABS:
  92.         cmp     [BYTE ds:si], SPACE
  93.         je      @@1stParam
  94.         cmp     [BYTE ds:si], TAB
  95.         je      @@1stParam
  96. @@1stParam:
  97.         call    JumpSPABS
  98.  
  99. ;-----  MAIN LOOP BEGINS HERE..
  100. @@LOOP_BEGIN:
  101.         cmp     [BYTE ds:si], CR        ; Is the char a carriage return ?
  102.         je      @@NoError               ; Yes: No parameters. End.
  103.         cmp     [BYTE ds:si], NULL      ; No : Is the char a NULL ?
  104.         je      @@Error                 ; Yes: End with error.
  105.         cmp     [BYTE ds:si], SPACE     ; No : Is the char a space ?
  106.         je      @@NextParam             ; Yes: Jump to next parameter.
  107.         cmp     [BYTE ds:si], TAB       ; No : Is the char a tab ?
  108.         je      @@NextParam             ; Yes: Jump to next parameter.
  109.         cmp     [BYTE ds:si], SW1       ; No : Is the char a '/' switch ?
  110.         je      @@Switch                ; Yes: Follow routine for switches.
  111.         cmp     [BYTE ds:si], SW2       ; No : Is the char a '-' switch ?
  112.         jne     @@CopyChar              ; No : Char is a param char.
  113. @@Switch:
  114.         inc     [es:nSwitches]          ; Yes: Inc # of switches.
  115.         cmp     di, bx                  ;  Is di at start of buffer ?
  116.         je      @@CopyChar              ;  Yes: Copy to buffer.
  117.         or      [BYTE es:di-1], 0       ;  No : Is prev char 0 ?
  118.         jz      @@CopyChar              ;  Yes: Copy to buffer.
  119.         stosb                           ;  No : Add 0 to prev param.
  120.         inc     [es:nParams]            ;   Inc # of parameters.
  121. @@CopyChar:
  122.         movsb                           ; [ds:si] -> [es:di]; inc si, inc di.
  123.         jmp     NEAR    @@LOOP_END
  124. @@NextParam:
  125.         call    JumpSPABS               ; si -> next param.
  126.         inc     cx                      ; 1st space/tab should not dec cx.
  127.         stosb                           ; [es:di]=NULL, inc di.
  128.         inc     [es:nParams]
  129. @@LOOP_END:
  130.         loop    @@LOOP_BEGIN            ; dec cx, if cx=0 break.
  131. ;-----  MAIN LOOP ENDS HERE.
  132.  
  133. @@TailEnd:
  134. ;-----  SI -> last char (should be <CR>). DI -> end of param string list.
  135.         or      [BYTE es:di-1], 0       ; Is last param char 0 ?
  136.         je      @@20                    ; Yes: Skip next instructions.
  137.         inc     [es:nParams]            ; No : Inc # of parameters.
  138. @@20:   cmp     [BYTE ds:si], CR        ; Is the last char a carriage return?
  139.         jne     @@Error                 ; No : End with error.
  140.         stosb                           ; Change <CR> to NULL.
  141. @@NoError:
  142.         clc                             ; No error.
  143. @@Return:
  144.         smove   ds, es                  ; ds -> Data Segment
  145.         pop     di                      ; di -> cmdBuf
  146.         mov     si, di                  ; si -> cmdBuf
  147.         RestoreRegs <cx,bx,ax>       ; Restore registers.
  148.         ret                             ; Return to caller.
  149.  
  150. @@Error:
  151.         stc                             ; Set carry flag.
  152.         jmp     NEAR    @@Return        ; End routine.
  153. ENDP    PSP_Parameters
  154.  
  155. ;===============================================================
  156. ; Procedure:    =*=JumpSPABS=*=
  157. ;---------------------------------------------------------------
  158. ; Access:       Private - Not available to other files.
  159. ; Task:         Jumps string of 1 or more spaces & tabs (SPABS). 
  160. ; Input:        si = Offset address of string of SPABS.
  161. ;               cx = # of chars from si reg to end of command tail
  162. ;                    (not including CR at end).
  163. ; Output:       si -> 1st char after SPABS (SPaces & tABS).
  164. ; Registers:
  165. ;       si, di, cx changed.
  166. ;===============================================================
  167. PROC    JumpSPABS
  168.  
  169.         push    ax              ; Save modified register(s).
  170. @@Repeat:
  171.         mov     al, [si]        ; Get character at ds:si.
  172.         cmp     al, SPACE       ; Is it a space?
  173.         je      @@Next          ; Yes: Move to next char.
  174.         cmp     al, TAB         ; No : Is it a tab?
  175.         je      @@Next          ; Yes: Move to next char.
  176.         jmp     NEAR    @@Return; No : si -> 1st char after SPABS; end.
  177. @@Next: inc     si              ; Move to next char.
  178.         loop    @@Repeat        ; dec cx; if cx > 0, jmp @@Repeat.
  179. @@Return:
  180.         pop     ax
  181.         ret                     ; Return to caller.
  182. ENDP    JumpSPABS
  183.  
  184. ;===============================================================
  185. ; Procedure:    =*=GetParamNumber=*=
  186. ;---------------------------------------------------------------
  187. ; Access:       Public - Available to other files.
  188. ; Task:         Return # of parameters (including switches) in command tail.
  189. ; Input:        none
  190. ; Output:       ax = # of parameters in command tail.
  191. ; Registers:    ax changed.
  192. ;===============================================================
  193. PROC    GetParamNumber
  194.  
  195.         xor     ah, ah          ; Clear upper half of ax.
  196.         mov     al, [nParams]   ; # of params in al.
  197.         ret                     ; Return to caller.
  198. ENDP    GetParamNumber
  199.  
  200. ;===============================================================
  201. ; Procedure:    =*=GetSwNumber=*=
  202. ;---------------------------------------------------------------
  203. ; Access:       Public - Available to other files.
  204. ; Task:         Return # of switches (e.g. /x) in command tail.
  205. ; Input:        none
  206. ; Output:       ax = # of switches in command tail.
  207. ; Registers:    ax changed.
  208. ;===============================================================
  209. PROC    GetSwNumber
  210.  
  211.         xor     ah, ah          ; Clear upper half of ax.
  212.         mov     al, [nSwitches] ; # of switches in al.
  213.         ret                     ; Return to caller.
  214. ENDP    GetSwNumber
  215.  
  216. ;===============================================================
  217. ; Procedure:    =*=GetNextParam=*=
  218. ;---------------------------------------------------------------
  219. ; Access:       Public - Available to other .ASM files.
  220. ; Task:         Point di to next param in command buffer.
  221. ; Input:        di -> cmdBuf
  222. ;               cmdBuf -> Parameters (PSP_Parameters should have
  223. ;               already processed PSP.
  224. ; Output:       di -> next parameter in cmdBuf.
  225. ; Registers:    di changed.
  226. ;===============================================================
  227. PROC    GetNextParam
  228.  
  229.         SaveRegs<ax,cx>
  230.         mov     al, NULL        ; Char to find in al.
  231.         mov     cl, [cBufLen]   ; Remaining buf length in cx.
  232.         xor     ch, ch
  233.         jcxz    @@Err           ; If di -> end of buf, end with error.
  234.         cld                     ; Auto increment.
  235.         repnz   scasb           ; Search for NULL. di -> Next parameter.
  236.         jcxz    @@Err           ; If di -> end of buf, end with error.
  237.         clc                     ; Else, no error.
  238. @@Rtn:  mov     [cBufLen], cl   ; Save remaining buf length in variable.
  239.         RestoreRegs<cx,ax>
  240.         ret                     ; Return to caller.
  241. @@Err:  stc                     ; Error.
  242.         jmp     @@Rtn           ; End program.
  243. ENDP    GetNextParam
  244.  
  245.         END                     ; End of module.
  246.