home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / UTILITY / VIRUS / DLCHK12.ZIP / SETERR.ASM < prev    next >
Encoding:
Assembly Source File  |  1991-03-18  |  7.8 KB  |  251 lines

  1.     TITLE    seterr
  2. ;-------------------------------------------------------------------------------
  3. ; SETERR.ASM  version 1.1
  4. ; by Duane Paulson 03/17/91.
  5. ;
  6. ; MASM 5.1
  7. ;
  8. ; PURPOSE: Batch file enhancer.
  9. ;
  10. ; OUTPUT: DOS errorlevel set to a value between 0 - 255 inclusive.
  11. ; INPUT: Command line parameter 0 - 255 inclusive.
  12. ;-------------------------------------------------------------------------------
  13.  
  14.     DOSSEG
  15.     .MODEL    SMALL
  16.  
  17. ;-------------------------------------------------------------------------------
  18. ; Macro Definitions:
  19. ;-------------------------------------------------------------------------------
  20.  
  21.     IF1                    ; Expand on pass 1 only.
  22. Mul_10    MACRO    factor            ; Multiplies byte value by 10.
  23.     MOV    AL,factor        ;; Load into AL
  24.     SHL    AL,1            ;; AL = Factor * 2 (binary shift left
  25.                     ;;          1 bit is same as mult * 2)
  26.     MOV    BL,AL            ;; Save copy in BL
  27.     SHL    AL,1            ;; AL = Factor * 4
  28.     SHL    AL,1            ;; AL = Factor * 8
  29.     ADD    AL,BL            ;; AL = Factor * 10
  30.     ENDM
  31.  
  32. OutStr    MACRO    str,lstr
  33.     MOV     AX,4000h        ;; Output string to device.
  34.     MOV     BX,1            ;; Specify STDOUT.
  35.     MOV     CL,lstr            ;; Length of string.
  36.     XOR    CH,CH            ;; Turn off high byte.
  37.     MOV     DX,OFFSET str        ;; Point to address of string.
  38.     INT     21h            ;; Call DOS
  39.     ENDM
  40.  
  41. OutChar    MACRO    char
  42.     MOV    AX,200h            ;; Display character to STDOUT.
  43.     MOV    DL,char            ;; Character to display.
  44.     INT    21h            ;; Call DOS.
  45.     ENDM
  46.  
  47.     ENDIF
  48.  
  49. ;-------------------------------------------------------------------------------
  50. ; Stack Declaration:
  51. ;-------------------------------------------------------------------------------
  52.  
  53.     .STACK    100h
  54.  
  55. ;-------------------------------------------------------------------------------
  56. ; Data Segment:
  57. ;-------------------------------------------------------------------------------
  58.  
  59.     .DATA
  60.  
  61. CMD_TAIL    DB 128 DUP(?)        ; Command line arguments go here.
  62. VAL_ARGS    DB 0            ; Will show length after non-numerals
  63.                     ;  are stripped.
  64.  
  65. CHAR1        DB '0'            ; three valid characters for processing.
  66. CHAR2        DB '0'
  67. CHAR3        DB '0'
  68.  
  69. ERR_LEV        DB ?            ; Errorlevel to be set.
  70.  
  71. ErrorMsg    DB 10,"Syntax: 'SETERR n' where range of 'n' is 0 to 255 inclusive.",13,10,10,"    DOS Errorlevel was not set. (Errorlevel = 0)",13,10
  72.  
  73. LErrorMsg    DB $ - ErrorMsg
  74.  
  75. SuccessMsg    DB 10,"The DOS Errorlevel has been set to "
  76. LSuccessMsg    DB $ - SuccessMsg
  77.  
  78. Windup        DB ".",13,10
  79. LWindup        DB $ - Windup
  80.  
  81. ;-------------------------------------------------------------------------------
  82. ; Code Segment:
  83. ;-------------------------------------------------------------------------------
  84.  
  85.     .CODE
  86.     EVEN
  87.  
  88. Start:
  89. ;-------------------------------------------------------------------------------
  90. ; Initialize the registers.
  91. ;-------------------------------------------------------------------------------
  92.  
  93.     MOV    AX,@DATA              ; Addr of work areas
  94.     MOV    DS,AX                  ; Set data segment reg
  95.  
  96. ;-------------------------------------------------------------------------------
  97. ; Set memory location of command tail and find out how many characters
  98. ;  it contains. Set loop counter.
  99. ;-------------------------------------------------------------------------------
  100.  
  101.     MOV    CL,BYTE PTR ES:[80H]      ; Length of command tail
  102.     XOR    CH,CH                  ; Clear hi-byte
  103.     DEC    CL            ; Decrement for Carriage Return
  104.     CMP    CL,0                  ; Any parmeter string?
  105.     JGE    @F            ; if found, jump forward.
  106.     JMP    Quit                  ;  else far jump to Quit.
  107. @@:                    ; Forward jump re-entry.
  108.  
  109. ;-------------------------------------------------------------------------------
  110. ; Point to buffer to receive command characters, and set up for
  111. ;  memory load and store routine.
  112. ;-------------------------------------------------------------------------------
  113.  
  114.     MOV    DI,OFFSET CMD_TAIL    ; Buffer to receive argument
  115.     MOV    SI,82H               ; Offset to command parm string
  116.     PUSH    DS                  ; Swap DS/ES
  117.     PUSH    ES
  118.     POP    DS
  119.     POP    ES
  120.     
  121. ;-------------------------------------------------------------------------------
  122. ; Read command tail a byte at a time.
  123. ; Determine whether or not character is a numeral.
  124. ; If it is, store it in a buffer area, and increment
  125. ;  the count of valid arguments.
  126. ;-------------------------------------------------------------------------------
  127.  
  128. Get:    LODSB                ; Byte from param string to AL
  129.     CMP    AL,'0'            ; Check if below 0 char.
  130.     JB    @F            ; If yes, jump forward.
  131.     CMP    AL,'9'            ; Check if above 9 char.
  132.     JA    @F            ; If yes, jump forward.
  133.  
  134.     STOSB                ; Store a character in the range
  135.                     ; 0-9 to CMD_TAIL.
  136.  
  137.     INC ES:VAL_ARGS            ; Increment to show that a valid
  138.                     ; argument was found.
  139.  
  140. @@:    LOOP    Get
  141.     
  142.             
  143.     PUSH    DS    ; Restore DS/ES for normal processing.
  144.     PUSH    ES
  145.     POP    DS
  146.     POP    ES
  147.  
  148. ;-------------------------------------------------------------------------------
  149. ; Now that we have only numerals in CMD_TAIL, we can start to process them.
  150. ;-------------------------------------------------------------------------------
  151.  
  152.  
  153.     CMP    VAL_ARGS,4        ; Less than 4 numerals on cmnd line?
  154.     JL    @F            ; If yes, jump forward.
  155.     JMP    Quit            ; Obviously more than 255 then. Long jmp
  156. @@:                    ; Forward re-entry.
  157.  
  158. ;-------------------------------------------------------------------------------
  159. ; Find out if there are 1, 2, or 3 valid characters, and assign individaul
  160. ; variables accordingly.
  161. ;-------------------------------------------------------------------------------
  162.  
  163.     CMP    VAL_ARGS,3        ; Compare VAL_ARGS to 3
  164.     JB    @F            ; If less than 3, jump forward.
  165.  
  166.     MOV    AL,BYTE PTR CMD_TAIL[0] ; Get byte from buffer location.
  167.     MOV    CHAR1,AL        ; Assign first character to CHAR1
  168.     MOV    AL,BYTE PTR CMD_TAIL[1]
  169.     MOV    CHAR2,AL        ; Second character goes to CHAR2
  170.     MOV     AL,BYTE PTR CMD_TAIL[2]
  171.     MOV    CHAR3,AL        ; Third character goes to CHAR3
  172.     JMP    SHORT    Convert        ; Jump to next routine.
  173. @@:
  174.     CMP    VAL_ARGS,2        ; Are there only two characters?
  175.     JB    @F            ; If no, jump forward.
  176.  
  177.     MOV    CHAR1,'0'        ; Assign place holder to CHAR1
  178.     MOV    AL,BYTE PTR CMD_TAIL[0]
  179.     MOV    CHAR2,AL        ; First character goes to CHAR2
  180.     MOV    AL,BYTE PTR CMD_TAIL[1]
  181.     MOV    CHAR3,AL        ; Second character goes to CHAR3
  182.     JMP    SHORT    Convert
  183. @@:
  184.                     ; Only one character found.
  185.     MOV    CHAR1,'0'        ; Assign place holders to CHAR1
  186.     MOV    CHAR2,'0'        ;  and CHAR2.
  187.     MOV    AL,BYTE PTR CMD_TAIL[0]    ; Character goes to CHAR3.
  188.     MOV    CHAR3,AL
  189.  
  190. ;-------------------------------------------------------------------------------
  191. ; Convert the three characters into a number.
  192. ;-------------------------------------------------------------------------------
  193.  
  194. Convert:
  195.     MOV    AL,CHAR1        ; First char is in the hundreds place.
  196.     SUB    AL,48            ; Convert from ASCII to number.
  197.     Mul_10    AL            ; Multiply by 10
  198.     Mul_10    AL            ; Multiply by 100 (10 * 10)
  199.     JC    Quit            ; Trap 300 and above.
  200.     MOV    ERR_LEV,AL        ; Else store the result.
  201.  
  202.     MOV    AL,CHAR2        ; Tens place.
  203.     SUB    AL,48            ; ASCII to number.
  204.     Mul_10    AL            ; Multiply by 10.
  205.     ADD    ERR_LEV,AL        ; Else add it.
  206.     JC    Quit            ; Trap 260 - 299.
  207.  
  208.     MOV    AL,CHAR3        ; Ones place.
  209.     SUB    AL,48            ; Make it a number.
  210.     ADD    ERR_LEV,AL        ; Add it.
  211.     JC    Quit            ; Trap 256-259.
  212.  
  213. ;-------------------------------------------------------------------------------
  214. ; Display the 'successful set' message.
  215. ;-------------------------------------------------------------------------------
  216.  
  217.     Outstr    SuccessMsg,LSuccessMsg
  218.  
  219.     CMP    VAL_ARGS,3
  220.     JB    @F
  221.     OutChar    CHAR1
  222.  
  223. @@:    CMP    VAL_ARGS,2
  224.     JB    @F
  225.     OutChar    CHAR2
  226.  
  227. @@:    OutChar    CHAR3
  228.  
  229.     OutStr    Windup,LWindup
  230.  
  231. ;-------------------------------------------------------------------------------
  232. ; Exit routine.
  233. ;-------------------------------------------------------------------------------
  234.  
  235. Whoa:
  236.  
  237.     MOV     AH,4Ch            ; Exit with return code (errorlevel)
  238.     MOV    AL,ERR_LEV
  239.     INT     21h            ; Call DOS and exit.
  240.  
  241. ;-------------------------------------------------------------------------------
  242. ; Error routine for bad parameters or no parameters.
  243. ; Displays proper syntax and exits.
  244. ;-------------------------------------------------------------------------------
  245.  
  246. Quit:
  247.     OutStr    ErrorMsg,LErrorMsg
  248.     JMP    Whoa            ; Bail out.
  249.  
  250.     END     Start
  251.