home *** CD-ROM | disk | FTP | other *** search
- TITLE seterr
- ;-------------------------------------------------------------------------------
- ; SETERR.ASM
- ; by Duane Paulson 03/12/91.
- ;
- ; MASM 5.1
- ;
- ; PURPOSE: Batch file enhancer.
- ;
- ; OUTPUT: DOS errorlevel set to a value between 0 - 255 inclusive.
- ; INPUT: Command line parameter 0 - 255 inclusive.
- ;-------------------------------------------------------------------------------
-
- DOSSEG
- .MODEL SMALL
- .STACK 100h
-
- .DATA
-
- CMD_TAIL DB 128 DUP(?) ; Command line arguments go here.
- ARG_LEN DB ? ; Original length of cmd line args.
- VAL_ARGS DB 0 ; Will show length after non-numerals
- ; are stripped.
-
- CHAR1 DB '0' ; three valid characters for processing.
- CHAR2 DB '0'
- CHAR3 DB '0'
-
- ERR_LEV DB ? ; Errorlevel to be set.
-
- WrongDOS DB "Sorry, requires DOS version 2.0 or greater.",13,10
- LWrongDOS DB $ - WrongDOS
- ErrorMsg DB "Syntax: 'SETERR n' where range of 'n' is 0 to 255 inclusive.",13,10,10," DOS Errorlevel was not set. (Errorlevel = 0)",13,10
-
- LErrorMsg DB $ - ErrorMsg
- SuccessMsg DB "DOS Errorlevel has been set to ???.",13,10 ; Locations 31,
- ; 32, and 33.
- LSuccessMsg DB $ - SuccessMsg
-
- .CODE
- EVEN
-
- Start:
- ;-------------------------------------------------------------------------------
- ; Initialize the registers.
- ;-------------------------------------------------------------------------------
-
- MOV AX,@DATA ; Addr of work areas
- MOV DS,AX ; Set data segment reg
-
- ;-------------------------------------------------------------------------------
- ; Output a linefeed so messages will stand out a little better.
- ;-------------------------------------------------------------------------------
-
- MOV AH,2 ; Output character to STDOUT
- MOV DL,10 ; Character to output
- INT 21h ; Call DOS
-
- ;-------------------------------------------------------------------------------
- ; Check for DOS version compatiblilty.
- ;-------------------------------------------------------------------------------
-
- MOV AX,3000h ; Get DOS version.
- INT 21h ; Call DOS.
- CMP AL,0 ; Less than 2.0 returns 0.
- JA @F ; Jump forward if greater than 0.
- JMP WrongOS ; Long jump to error message.
- @@: ; Re-entry point for forward jump.
-
- ;-------------------------------------------------------------------------------
- ; Set memory location of command tail and find out how many characters
- ; it contains. Set loop counter.
- ;-------------------------------------------------------------------------------
-
- MOV CL,BYTE PTR ES:[80H] ; Length of command tail
- XOR CH,CH ; Clear hi-byte
- MOV ARG_LEN,CL ; Set loop iterations.
- DEC CL ; Decrement for Carriage Return
- CMP CL,0 ; Any parmeter string?
- JGE @F ; if found, jump forward.
- JMP Quit ; else far jump to Quit.
- @@: ; Forward jump re-entry.
-
- ;-------------------------------------------------------------------------------
- ; Point to buffer to receive command characters, and set up for
- ; memory load and store routine.
- ;-------------------------------------------------------------------------------
-
- MOV DI,OFFSET CMD_TAIL ; Buffer to receive argument
- MOV SI,82H ; Offset to command parm string
- MOV DX,ES
- MOV AX,DS ; Swap DS/ES
- MOV ES,AX
- MOV DS,DX
-
- ;-------------------------------------------------------------------------------
- ; Read command tail a byte at a time.
- ; Determine whether or not character is a numeral.
- ; If it is, store it in a buffer area, and increment
- ; the count of valid arguments.
- ;-------------------------------------------------------------------------------
-
- Get: LODSB ; Byte from param string to AL
- CMP AL,'0' ; Check if below 0 char.
- JB @F ; If yes, jump forward.
- CMP AL,'9' ; Check if above 9 char.
- JA @F ; If yes, jump forward.
-
- STOSB ; Store a character in the range
- ; 0-9 to CMD_TAIL.
-
- MOV DX,ES ; Restore DS/ES so we can
- MOV AX,DS ; increment a register properly.
- MOV ES,AX
- MOV DS,DX
-
- INC VAL_ARGS ; Increment to show that a valid
- ; argument was found.
-
- MOV DX,ES ; Swap DS/ES back so the memory
- MOV AX,DS ; manipulation routine can continue.
- MOV ES,AX
- MOV DS,DX
-
- @@: LOOP Get
-
- MOV DX,ES ; Restore DS/ES for normal processing.
- MOV AX,DS
- MOV ES,AX
- MOV DS,DX
-
- ;-------------------------------------------------------------------------------
- ; Now that we have only numerals in CMD_TAIL, we can start to process them.
- ;-------------------------------------------------------------------------------
-
-
- CMP VAL_ARGS,4 ; Less than 4 numerals on cmnd line?
- JL @F ; If yes, jump forward.
- JMP Quit ; Obviously more than 255 then. Long jmp
- @@: ; Forward re-entry.
-
- ;-------------------------------------------------------------------------------
- ; Find out if there are 1, 2, or 3 valid characters, and assign individaul
- ; variables accordingly.
- ;-------------------------------------------------------------------------------
-
- CMP VAL_ARGS,3 ; Compare VAL_ARGS to 3
- JB @F ; If less than 3, jump forward.
-
- MOV AL,BYTE PTR CMD_TAIL[0] ; Get byte from buffer location.
- MOV CHAR1,AL ; Assign first character to CHAR1
- MOV AL,BYTE PTR CMD_TAIL[1]
- MOV CHAR2,AL ; Second character goes to CHAR2
- MOV AL,BYTE PTR CMD_TAIL[2]
- MOV CHAR3,AL ; Third character goes to CHAR3
- JMP Convert ; Jump to next routine.
- @@:
- CMP VAL_ARGS,2 ; Are there only two characters?
- JB @F ; If no, jump forward.
-
- MOV CHAR1,'0' ; Assign place holder to CHAR1
- MOV AL,BYTE PTR CMD_TAIL[0]
- MOV CHAR2,AL ; First character goes to CHAR2
- MOV AL,BYTE PTR CMD_TAIL[1]
- MOV CHAR3,AL ; Second character goes to CHAR3
- JMP Convert
- @@:
- ; Only one character found.
- MOV CHAR1,'0' ; Assign place holders to CHAR1
- MOV CHAR2,'0' ; and CHAR2.
- MOV AL,BYTE PTR CMD_TAIL[0] ; Character goes to CHAR3.
- MOV CHAR3,AL
-
- ;-------------------------------------------------------------------------------
- ; Convert the three characters into a number.
- ;-------------------------------------------------------------------------------
-
- Convert:
- MOV AL,CHAR1 ; First char is in the hundreds place.
- SUB AL,48 ; Convert from ASCII to number.
- MOV BL,100 ; Multiply by 100
- MUL BL
- JC Quit ; Quit if carry to AH. (Product > 255.)
- MOV ERR_LEV,AL ; Else store the result.
- ; Note to self: rtfm.
-
- MOV AL,CHAR2 ; Tens place.
- SUB AL,48 ; ASCII to number.
- MOV BL,10 ; Multiply by 10
- MUL BL
- JC Quit ; Overflow is still possible.
- ADD ERR_LEV,AL ; Else add it.
-
- MOV AL,CHAR3 ; Ones place.
- SUB AL,48 ; Make it a number.
- ADD ERR_LEV,AL ; Add it.
- JC Quit ; 256 is still greater than 255.
-
- ;-------------------------------------------------------------------------------
- ; Drop the characters into SuccessMsg locations 31, 32, and 33.
- ;-------------------------------------------------------------------------------
-
- MOV AL,CHAR1
- MOV BYTE PTR SuccessMsg[31],AL ; Point at buffer location.
- MOV AL,CHAR2
- MOV BYTE PTR SuccessMsg[32],AL
- MOV AL,CHAR3
- MOV BYTE PTR SuccessMsg[33],AL
-
- MOV AX,4000h ; Output string to device.
- MOV BX,1 ; Specify STDOUT.
- MOV CL,LSuccessMsg ; Length of string.
- XOR CH,CH ; Turn off high byte.
- MOV DX,OFFSET SuccessMsg ; Point to address of string.
- INT 21h ; Call DOS
-
- ;-------------------------------------------------------------------------------
- ; Exit routine.
- ;-------------------------------------------------------------------------------
-
- Whoa:
- MOV AH,4Ch ; Exit with return code (errorlevel)
- MOV AL,ERR_LEV
- INT 21h ; Call DOS and exit.
-
- ;-------------------------------------------------------------------------------
- ; Error routine for bad parameters or no parameters.
- ; Displays proper syntax and exits.
- ;-------------------------------------------------------------------------------
-
- Quit:
- MOV AX,4000h ; Output string to device.
- MOV BX,1 ; Specify STDOUT.
- MOV CL,LErrorMsg ; Length of string.
- XOR CH,CH ; Turn off high byte.
- MOV DX,OFFSET ErrorMsg ; Pointer to address of string
- INT 21h ; Call DOS.
- JMP Whoa ; Bail out.
-
- ;-------------------------------------------------------------------------------
- ; Error routine for DOS version 1.
- ;-------------------------------------------------------------------------------
-
- WrongOS:
- MOV AX,4000h ; Output string to device.
- MOV BX,1 ; Specify STDOUT.
- MOV CL,LWrongDOS ; Length of string.
- XOR CH,CH ; Turn off high byte.
- MOV DX,OFFSET WrongDOS ; Pointer to address of string
- INT 21h ; Call DOS.
- JMP Whoa ; Bail out.
-
- END Start