News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

How to create a universal bat-file

Started by Mikl__, November 25, 2015, 07:52:14 PM

Previous topic - Next topic

Mikl__

How to create a universal bat-file
We write for DOS, for Windows, etc. Wi can use TASM, MASM, WASM (Watcom Assembler), FASM, NASM, etc - but our source file has a standard extension "asm", and as a result require com, exe, dll, lib, etc ... If we write the exe-file of for Windows - if in different ways to collect exe for console or gui ... Is it possible to simplify the assembly of the ASM-file and do not write every time a new bat-file?
For starters we write a bat-file that at the click of the mouse or pressing Enter creates gui-file under Windows, and if the same folder exist rc-file with the same name, then our obj-file will be added to res-file.
set filename=
set masm_path=\masm32
if exist %filename%.rc (
%masm_path%\bin\rc /v %filename%.rc
%masm_path%\bin\cvtres /machine:ix86 %filename%.res
%masm_path%\bin\ml /c /Cp /Gz /I%masm_path%\include /coff /nologo %filename%.asm  || exit
%masm_path%\bin\Link /SUBSYSTEM:WINDOWS /ALIGN:16 ^
/LIBPATH:%masm_path%\lib /NOLOGO %filename%.obj %filename%.res || exit
del %filename%.res
) else (
%masm_path%\bin\ml /c /Cp /Gz /I%masm_path%\include /coff /nologo %filename%.asm  || exit
%masm_path%\bin\Link /SUBSYSTEM:WINDOWS /ALIGN:16 ^
/LIBPATH:%masm_path%\lib /NOLOGO %filename%.obj
)
The resulting bat-file runs in semi-automatic mode. The first string is «set filename =» we enter the name of our asm-file and run asm.bat, and to make our bat-file do not breed like cockroaches, you need to drag this asm.bat from project to project. I agree it's not exactly what we wanted - or rather not at all!
Program Far Menager (Far.exe), as well as before Norton commander, you can "train" to certain actions with files having the same extension. Far Menager supports file associations, that allow you to set different actions to run, edit, and view your files, set the mask (in this case the mask «* .asm»). You can add new associations with the command "File Association" in the "Menu commands". You can specify multiple associations for a file type and select the desired association from the menu. The list of associations, use the following steps:

  • Ins - add a new association;
  • F4 - to change the current configuration of the association;
  • Del - delete the current association.
When you run from the command line, we write the following text:> asm.bat msgbox.asmasm.bat should analyze the command line, to take the first argument after his own name, select it from the file name without the extension «.asm» and substitute that name in a variable filename. The first argument after the program name are referred to as %1. You're learning how to complete the file name of the allocated file name without the extension. The associated commands are special meta-characters, such as "!" Means the length of the name without the extension. Well - we have enough knowledge to solve this problem!

Now we need to fix our asm.bat - on the first line writeset filename=%1Run Far Manager — click on F9 -> «Commands» —> «File Associations» —> F4—> «A file mask or several file masks» —> write «*.asm» —> «Execute command (used for Enter)» —> write «asm.bat !» and copy asm.bat in folder Windows\system32\. Now it is necessary for any asm-file click or hover and click on Enter, as the process will be started compiling and linking. And asm.bat is one only! But we wanted it to be more universatile ...
We divide our asm-bat into several parts, for example, in the first part of asm.bat strings that create COM-files, in the second part - strings, which is created from the asm-file EXE-files for DOS, in the third part - EXE-files for Windows (and, when we need a GUI option is inserted /SUBSYSTEM:WINDOWS and when needed console - setting /SUBSYSTEM: CONSOLE), in the fourth - DLL-files in the fifth - SYS-files, etc., and some strings bat-file is common to remove the "garbage" as obj-files, etc.
Suppose that the first string of our asm-file will tell our universal bat-file what type of file we should get a result of compilation and linking, for any operating system (Windows, DOS), this file will be used and how it should be the assembler to compile and link.
If the first line in the asm-file comment out, it will be ignored by the compiler, but it will be perceived by the shell, which, in turn, ignores the "semicolon".
So the first line of asm-file - we write on masm-dialect and want to get a file com-dos:; masm com dos #
.model tiny
.code
org 100h
start:
. . .
Inside our asm.bat paste procedure that for the first three words separated by spaces or tabs will be a label which will give control inside asm.bat and already there from our asm-file, we will file the necessary result :read_settings
for /f "eol=# tokens=2-5" %%A in (%filename%.asm) do (
set compiler=%%A
set kind_of_file=%%B
set os=%%C
if %%D == # exit /b)
exit /b
Erased from the screen (cls —"clear screen"), to get the name asm file without an extension (set filename =%1) and transmits it to the procedure, depending on the desired assembly, operating system and the file type go to the appropriate branch (goto ...). Further, the values of variables in the compiler, kind_of_file, os handle asm-file.cls
set filename=%1
call :read_settings %filename%
goto %compiler%%os%%kind_of_file%
The part of asm.bat collect on masm com-file under dos looks like this. . .
:masmdoscom
if exist %filename%.%kind_of_file% del %filename%.%kind_of_file%
ml /AT %filename%.asm
del %filename%.obj
exit
. . .
Since asm.bat appeared inside our procedure will have to make a slight change in the launch of the associated file.
Run FAR.exe - click on F9 —> «Commands» —> «File Associations» —> F4 —> «A file mask or several file masks» —>write «*.asm» —> «Execute command (used for Enter)» —> write «call asm.bat  !»
Below the full content of asm.bat, which I use on my computercls
set filename=%1
call :read_settings %filename%
@echo %compiler% %os% %kind_of_file%
if exist %filename%.exe del %filename%.exe
if exist %filename%.com del %filename%.com
if exist %filename%.dll del %filename%.dll
set %compiler%_path= <-- there must be a path to your directory with compilers and linkers
goto :%compiler%%os%%kind_of_file%
:wasmwindowsgui
%wasm_path%\bin\wasm -6prs -mf -i%wasm_path%\include\WASM %filename%.asm || exit
%wasm_path%\bin\wlink file %filename%.obj form windows nt op c LIBPath ^
%wasm_path%\lib\ Library user32.lib,kernel32.lib,gdi32.lib
goto :m1
:fasmdoscom
:fasmdosexe
%fasm_path%\bin\fasm %filename%.asm
exit
:goasmwindowsgui
%goasm_path%\bin\GoAsm %filename%
if exist %1.rc (
%goasm_path%\bin\GoRc /r %filename%.rc || exit
%goasm_path%\bin\GoLink %filename%.obj %filename%.res user32.dll kernel32.dll ^
gdi32.dll comctl32.dll shell32.dll ole32.dll comdlg32.dll || exit
del %filename%.res
) else (
%goasm_path%\bin\GoLink %filename%.obj user32.dll kernel32.dll gdi32.dll ^
comctl32.dll shell32.dll ole32.dll comdlg32.dll
)
goto :m1
:goasmwindowsconsole
%goasm_path%\bin\GoAsm %filename%
%goasm_path%\bin\GoLink /console %filename%.obj kernel32.dll user32.dll ^
winmm.dll gdi32.dll
goto :m1
:lzasmwindowsconsole
:lzasmwindowsgui
%lzasm_path%\bin\lzasm %filename%.asm
%lzasm_path%\bin\alink %filename%.obj -oPE -c -subsys %kind_of_file%
goto :m1
:lzasmdoscom
%lzasm_path%\bin\lzasm %filename%.asm
%lzasm_path%\bin\alink %filename%.obj -oCOM -c
goto :m1
:lzasmdosexe
%lzasm_path%\bin\lzasm %filename%.asm
%lzasm_path%\bin\alink %filename%.obj -oEXE -c
goto :m1
:masmwindowsgui
set masm_path=d:\masm32
if exist %filename%.rc (
%masm_path%\bin\rc /v %1.rc
%masm_path%\bin\cvtres /machine:ix86 %1.res
%masm_path%\bin\ml /c /Cp /Gz /I%masm_path%\include /coff /nologo %filename%.asm  || exit
%masm_path%\bin\PoLink1 /SUBSYSTEM:WINDOWS /ALIGN:4 /MERGE:.data=.text ^
/LIBPATH:%masm_path%\lib /NOLOGO /STUB:%masm_path%\bin\stubby.exe %1.obj %filename%.res || exit
del %filename%.res
) else (
%masm_path%\bin\ml /c /Cp /Gz /I%masm_path%\include /coff /nologo %filename%.asm  || exit
%masm_path%\bin\PoLink1 /SUBSYSTEM:WINDOWS /ALIGN:4 /MERGE:.data=.text ^
/LIBPATH:%masm_path%\lib /NOLOGO /STUB:%masm_path%\bin\stubby.exe %filename%.obj
)
goto :m1
:masmwindowsnative
if exist %1.sys del if exist %1.sys
set masm_path=d:\masm32
%masm_path%\bin\ml /c /Cp /Gz /I%masm_path%\include /nologo /c /coff %filename%.asm  || exit
%masm_path%\bin\link /LIBPATH:%masm_path%\lib\ /nologo /driver /base:0x10000 ^
/align:32 /out:%filename%.sys /subsystem:native %filename%.obj  || exit
goto :m1
:tasmwindowsgui
%tasm_path%\bin\tasm32 /I%tasm_path%\include\TASM /q /z %filename%.asm  %filename%.obj /ml/m3
%tasm_path%\bin\ilink32 -L%tasm_path%\lib /Tpe /aa /c /o /x %filename%.obj
goto :m1
:tasmwindowsconsole
%tasm_path%\bin\tasm32 /I%tasm_path%\include\TASM %filename%.asm /ml /m3 /q /z %filename%.obj
%tasm_path%\bin\ilink32 -L%tasm_path%\lib /Tpe /ap /c /o /x %filename%.obj
goto :m1
:nasmwindowsconsole
:nasmwindowsgui
%nasm_path%\bin\nasmw -O1 -f bin %filename%.asm -o %filename%.exe ^
-I%nasm_path%\include\NASM\
exit
:nasmdoscom
:nasmdosexe
:nasmwindowsdll
%nasm_path%\bin\nasmw -f bin %filename%.asm -o %filename%.%kind_of_file% ^
-I%nasm_path%\include\NASM\
exit
:tasmdoscom
%tasm_path%\bin\tasm /m3 %filename%.asm
%tasm_path%\bin\tlink /t/x %filename%.obj
goto :m1
:tasmdosexe
%tasm_path%\bin\tasm /m3 %filename%.asm
%tasm_path%\bin\tlink /x %filename%.obj
goto :m1
:wasmdoscom
%wasm_path%\bin\wasm %filename%.asm
%wasm_path%\bin\wlink file %filename%.obj form dos com
goto :m1
:wasmdosexe
%wasm_path%\bin\wasm %filename%.asm
%wasm_path%\bin\wlink file %filename%.obj form dos
goto :m1
:рoasmwindowsconsole
%poasm_path%\bin\poasm /Gz /I%poasm_path%\include\PoAsm %filename%.asm
%poasm_path%\bin\polink /SUBSYSTEM:CONSOLE /LIBPATH:%poasm_path%\lib /MERGE:.data=.text %filename%.obj
goto :m1

:masmdoscom
%masm_path%\bin\ml /AT /c %filename%.asm
%masm_path%\bin\link16 /T %filename%.obj,,,,,
goto :m1
:masmdosexe
%masm_path%\bin\ml /c %filename%.asm
%masm_path%\bin\link16 %filename%.obj,,,,,
:m1
if exist %filename%.map del %filename%.map
if exist %filename%.obj del %filename%.obj
if exist %filename%.ilc del %filename%.ilc
if exist %filename%.ild del %filename%.ild
if exist %filename%.ilf del %filename%.ilf
if exist %filename%.ils del %filename%.ils
if exist %filename%.tds del %filename%.tds
exit
:read_settings
for /f "eol=# tokens=2-5" %%A in (%filename%.asm) do (
set compiler=%%A
set os=%%B
set kind_of_file=%%C
if %%D == # exit /b )
exit /b
If you do not use Far Manager, and write the ASM-file in NotePad or somewhere else, then a bat-file to do it all "% 1" on «% ~ n1», click on "My Computer" —> "Service" —> "folder Options" —> "file kinds" and associate files with ASM-file asm.bat.
Sorry for my English!

jj2007

Nice idea, but avoid hard-coded drives:
set %compiler%_path=d:\Aquila

try instead
set %compiler%_path=\Aquila

When I wrote the batch processing for RichMasm, my testbed was the \Masm32\examples folder. If your batch file works with all the examples, it's OK 8)

dedndave


Mikl__