Kottos's Batch Programming Page


Ok here it goes...
After having a chat on #coders late one night a certain fellow suggested
that it is possible to use inline asm in batch files.
And then proceeded to leave without telling me how, so I figured it out on 
my lonesome.
But before we get into the cool stuff like this, I'll explain some basic stuff.
On my computer I use PCDOS not MSDOS which, in my humble opinion is the lesser 
of two evils, so to get some simple info you type :-

	help | grep "batch"

and you get a list of the batch file commands in the help file, then for each 
one you type

	help for > Batch.txt
	help if >> Batch.txt
	help goto >> Batch.txt 
	and so on.

But on MSDOS you get that shitty help system so you will have to get all the 
stuff by yourself.

Ok so you have read all the basic stuff now the cool stuff.

Inline ASM in batch files. (Kinda)

I suppose I should explain that you cannot realyy inline asm in batch files
rather you have to use dos's piping ability and debug.exe (one on the 2
Microsoft programs that actually work, the other being edit.com, actually FC
has been quite usefull too, enough of this micro$oft bashing), bugger where 
was I... Oh yes, so you have to use debug here is a simple example of a
debug session.

C:\DEBUG                        ;

-a 100                          ; Start code at offset 100 (.com files)
25CD:0100 MOV AX, 0             ; Keyboard get key no echo
25CD:0103 INT 16H               ; Ascii code in AL
25CD:0105 MOV AH, 4C            ; Exit do dos                              
25CD:0107 INT 21H               ; Return code still in AL              
25CD:0109                       ;
-N GETCH.COM                    ; Name of the file
-R BX                           ; Clear BX
BX 0000                         ;
:0                              ;
-R CX                           ; Put length of file in CX
CX 0000                         ;
:9                              ;
-W                              ; Write to disk
Writing 00009 bytes             ;
-Q                              ; Done, quit

Now you can use your new 9byte getch.com program in you batch files.
You should notic that this is programming at its best.  Make this exact
same program in Delphi :)

So how do we use this in our Batch file?
Well try this :-

edit 1.bat
@echo off                               ; Turn the echo off

ECHO Press [y/n]                        ; Prompt the user
getch.com                               ; Run our getch.com file
IF ERRORLEVEL 121 GOTO YAH              ; Chechk th return value
IF ERRORLEVEL 110 GOTO NAH              ; Blah blah

:YAH
ECHO You pressed Y
GOTO END

:NAH
ECHO You pressed N
GOTO END

:END

[SAVE]

So run this and if you r caps lock if off it will work.  
Remember that the IF ERRORLEVEL Num returns true if the ERRORLEVEL 
is eith equal _OR_ greater than Num. So do your large Number checks first.
The ERRORLEVEL is, for all you people to lazy to find out for yourself, the
Number in the AL register when the program exits.

HANG ON A MINUTE!!
Ok so thats not inline asm to do this in inline asm you must first 
understand piping in the DOS enviorment.

Piping and Redirection.

The DOS enviorment has both a Standard input (keyboard) and a standard 
output (the text screen).  So when you runa program you can redirect these
to and from files.
 
 program.com > file      ;Redirects the output of a program to a file

Eg. type autoexec.bat > autoexec.bak

 program.com < file     ; Redirect the input from a file

Eg. Actuall I cant think of another on off hand (doh!!) except the one Im 
gonna show you in a moment.

Piping is a little different but only slightly, it redirect the output of 
a program into the input of another program.  Phew!!

  program1.com | program2.com
Eg. type autoexec.bat | grep "BLASTER"

So for our Batch file when have to redirect some code  directly into the 
debug program to make it do something then exit cleanly and also redirect 
the its output.  But we dont want to keep its output so we send it into 
oblivion. By using the NUL file

	debug < debug.script > NUL

So make a text file...

A 100
MOV ax, 0
int 16h
MOV ah, 4c
int 21h

g 109
q

___EOF____

And try it out with or with out the NUL file.

So now come the clever bit, inlining this in a batch file.
Here's how its done 

edit 1.bat
-------8<-------------------------
@ECHO OFF
GOTO CODE

A 100
MOV ax, 0
int 16h
MOV ah, 4c
int 21h

G 109
q

:CODE
ECHO [Y/N]

debug < 1.bat > NUL                     ; The clever bit send the
                                        ; batch file itself into                                         ; debug
IF ERRORLEVEL 121 GOTO YAH              ; Chechk th return value
IF ERRORLEVEL 110 GOTO NAH              ; Blah blah

:YAH
ECHO You pressed Y
GOTO END

:NAH
ECHO You pressed N
GOTO END

:END
----8<----------------------------

And there you have it.

Mail me your groovey applications of this wonderful thingy...