Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

errorlevel handling in DOS batch

315 views
Skip to first unread message

bub...@gmail.com

unread,
Jun 10, 2008, 9:07:36 AM6/10/08
to
http:/hi
I have a question
in a toplevel batch I have some thing like:

call mybat
if NOT (%ERRORLEVEL%) == (0) echo error

in mybat I have some thing like
///////////////////////////////////////////////////////////////
pushd %~dp0
........
call mybat2
GOTO END
:END
echo,
popd
///////////////////////////////////////////////////////////////

mybat2 returns an errorlevel.
Is it guaranteed that I will see the errorlevel returned by mybat2 in
the top level batch file?

Or in other words will these DOS commands change the errorlevel (for
example on different OS???)?
GOTO END
:END
echo,
popd

bubu /groups.google.com/group/comp.os.msdos.programmer/topics?lnk=sg

Terence

unread,
Jun 11, 2008, 4:09:59 AM6/11/08
to
Put the error code byte in AL,
Put 4Ch in AX
Call DOS service 21h

Dirk Wolfgang Glomp

unread,
Jun 11, 2008, 5:44:16 AM6/11/08
to
Am Wed, 11 Jun 2008 01:09:59 -0700 (PDT) schrieb Terence:

> Put the error code byte in AL,
> Put 4Ch in AX

Results: AX=004Ch, AH=0, AL=4Ch
AH + AL
00000000 01001100 = AX

The exitcode(4Ch) must place only in the AH-register,
else the errorlevel in AL will be destroyd with 4Ch.
The zero function(terminate programm) in AH doesn´t
provide an errorlevel like terminate-function 4Ch.

----------------------------------------------
Ralf Browns Interrupt List(RBIL):
http://www.pobox.com/~ralf
http://www.pobox.com/~ralf/files.html
ftp://ftp.cs.cmu.edu/afs/cs.cmu.edu/user/ralf/pub/

RBIL->inter61b.zip->Interrup.f
--------D-2100-------------------------------
INT 21 - DOS 1+ - TERMINATE PROGRAM
AH = 00h
CS = PSP segment
Notes: Microsoft recommends using INT 21/AH=4Ch for DOS 2+
this function sets the program's return code (ERRORLEVEL) to 00h
execution continues at the address stored in INT 22 after DOS performs
whatever cleanup it needs to do (restoring the INT 22,INT 23,INT 24
vectors from the PSP assumed to be located at offset 0000h in the
segment indicated by the stack copy of CS, etc.)
if the PSP is its own parent, the process's memory is not freed; if
INT 22 additionally points into the terminating program, the
process is effectively NOT terminated
not supported by MS Windows 3.0 DOSX.EXE DOS extender
SeeAlso: AH=26h,AH=31h,AH=4Ch,INT 20,INT 22
----------------------------------------------

RBIL->inter61b.zip->Interrup.g
--------D-214C-------------------------------
INT 21 - DOS 2+ - "EXIT" - TERMINATE WITH RETURN CODE
AH = 4Ch
AL = return code
Return: never returns
Notes: unless the process is its own parent
(see #01378 [offset 16h] at AH=26h), all open files are closed and
all memory belonging to the process is freed
all network file locks should be removed before calling this function
SeeAlso: AH=00h,AH=26h,AH=4Bh,AH=4Dh,INT 15/AH=12h/BH=02h,INT 20,INT 22
SeeAlso: INT 60/DI=0601h
----------------------------------------------

Dirk

Terence

unread,
Jun 11, 2008, 8:31:15 PM6/11/08
to
On Jun 11, 7:44 pm, Dirk Wolfgang Glomp <d...@freecrac.dyndns.org>
wrote:

> Am Wed, 11 Jun 2008 01:09:59 -0700 (PDT) schrieb Terence:
>
> > Put the error code byte in AL,
> > Put 4Ch in AX
>
> Results: AX=004Ch, AH=0, AL=4Ch
>   AH    +   AL
> 00000000 01001100 = AX
>
> The exitcode(4Ch) must place only in the AH-register,
> else the errorlevel in AL will be destroyd with 4Ch.
> The zero function(terminate programm) in AH doesn´t
> provide an errorlevel like terminate-function 4Ch.

Correct! It was a tipo of mine.
Actually I load AX with the integer value passed to a routine, then
overwrite AH with 4Ch as follows.

name IRET
public IRET
siret segment
assume CS:siret
IRET proc far ;CALL IRET(iretc);
PUSH BP
MOV BP,SP
LES BX,dword ptr [bp+6]
MOV AX,ES:[BX]
MOV SP,BP
POP BP
MOV AH,4Ch
INT 21h
RET 4 ;dummy
IRET endp
siret ends
END

Dirk Wolfgang Glomp

unread,
Jun 12, 2008, 5:08:58 AM6/12/08
to
Am Tue, 10 Jun 2008 06:07:36 -0700 (PDT) schrieb bub...@gmail.com:

> http:/hi
> I have a question
> in a toplevel batch I have some thing like:
>
> call mybat
> if NOT (%ERRORLEVEL%) == (0) echo error

if ERRORLEVEL echo error

> in mybat I have some thing like
> ///////////////////////////////////////////////////////////////
> pushd %~dp0
> ........
> call mybat2

if ERRORLEVEL echo error

> GOTO END
> :END
> echo,
> popd
> ///////////////////////////////////////////////////////////////
>
> mybat2 returns an errorlevel.
> Is it guaranteed that I will see the errorlevel returned by mybat2 in
> the top level batch file?
>
> Or in other words will these DOS commands change the errorlevel (for
> example on different OS???)?

No i don´t believe that the errorlevel are changed on different OS.
But "pushd", "popd" and "cmd.exe" doesn´t exist on MSDOS 6.xx.

If we want more than check if the ERRORLEVEL = 0, than we must check
the ERRORLEVEL in a reverse order, so we start checking the highest
ERRORLEVEL that can be expected and than check the lower values.
The highest possible ERRORLEVEL is 255(one byte).

Dirk

Ted Davis

unread,
Jun 12, 2008, 9:04:45 AM6/12/08
to
On Tue, 10 Jun 2008 06:07:36 -0700, bubu.aa wrote:

> http:/hi
> I have a question
> in a toplevel batch I have some thing like:
>
> call mybat
> if NOT (%ERRORLEVEL%) == (0) echo error

That will always fail fail because you are testing (assuming ERRORLEVEL is
0)
if not "(0) "==" (0)"

Note the locations of the spaces prevents a valid test - the correct
syntax is

if not (%errorlevel%)==(0)
but I would advise against using () there since they have syntatical
functions - it is usual to use "" or to add a punctuation character (!,
for example) to avoid the possibility of testing a null string.

>
> in mybat I have some thing like
> ///////////////////////////////////////////////////////////////
> pushd %~dp0
> ........
> call mybat2
> GOTO END
> :END
> echo,
> popd
> ///////////////////////////////////////////////////////////////
>
> mybat2 returns an errorlevel.

We have ony your word for that - you omitted the code that would allow us
to decide whether or not you actually are diong that - that syntax
problems with the test indicates that suspicion on the exit code point is
warranted.

> Is it guaranteed that I will see the errorlevel returned by mybat2 in
> the top level batch file?
>
> Or in other words will these DOS commands change the errorlevel (for
> example on different OS???)?
> GOTO END
> :END
> echo,
> popd
>

Some commands that were internal commands in COMMAND.COM are externals
under CMD.EXE and may return their own errorlevels. ERRORLEVEL is
*always* tested immediately after the function that sets it returns just
as the return value of a function in high level languages is always tested
immediately after return (or is stored in a stable variable or use later).

Unless that rule is followed, there is the risk of inserting code later
between the function and the test - code that breaks the program. If it
is necessary to test a return code later rather than immediately, save it
in a dedicated variable.


--
T.E.D. (tda...@mst.edu)

0 new messages