Fun with CPP - APP (Assembly language PreProcessor)
Posted: Wed Mar 23, 2016 12:32 pm
Fun with CPP - APP (Assembly language PreProcessor)
Write once, assemble anywhere (well at least with FASM and
NASM).
The actual x86 assembly language syntax is the same for
both and probably some other assemblers. It's the
directives that are different. The same program needs
different files for each assembler.
By using the C preprocessor CPP to #define FASM define('s)
and NASM %define('s) the same file can be assembled by both.
Similarly, CPP can #include FASM include('s) and NASM
%include('s). For those directives which are different and not
supported by CPP such as FASM rb and NASM resb a few
lines at the top will take care of it. Also FASM and NASM
differ on what can be equ(ated), whereas CPP merely
#define('s) everything. And don't forget the use of CPP
multiline comments /* */.Put the C preprocessor (on Windows the gcc package contains
cpp.exe and cc1.exe both of which are required) in the same
folder or specify the location in the path statement. The
same goes for your assembler of choice (try both to confirm
that this does indeed work or have a look at the *.2 file
which is sent to the assembler).
** Windows command line script **
app.cmd yourfile.asmAn experienced Linux user could contribute a similar
bash script for use on Linux.
Write once, assemble anywhere (well at least with FASM and
NASM).
The actual x86 assembly language syntax is the same for
both and probably some other assemblers. It's the
directives that are different. The same program needs
different files for each assembler.
By using the C preprocessor CPP to #define FASM define('s)
and NASM %define('s) the same file can be assembled by both.
Similarly, CPP can #include FASM include('s) and NASM
%include('s). For those directives which are different and not
supported by CPP such as FASM rb and NASM resb a few
lines at the top will take care of it. Also FASM and NASM
differ on what can be equ(ated), whereas CPP merely
#define('s) everything. And don't forget the use of CPP
multiline comments /* */.
Code: Select all
//#define NASM /* uncomment if using NASM */
//#define FASM /* uncomment if using FASM */
#ifdef NASM
#define rb(value) resb value
#endif
#ifdef FASM
#define rb(value) rb value
#endif
label: rb(0x200)
cpp.exe and cc1.exe both of which are required) in the same
folder or specify the location in the path statement. The
same goes for your assembler of choice (try both to confirm
that this does indeed work or have a look at the *.2 file
which is sent to the assembler).
** Windows command line script **
app.cmd yourfile.asm
Code: Select all
@echo off
:: for some Linux reason cpp needs to be told where it is
set current_path=%~p0
path=%current_path%;%path%
if exist %~n1.2 del %~n1.2
cpp.exe %1 > %~n1.1
:: preprocess cpp to get rid of cpp comments that begin
:: with "#"
for /f "delims=" %%a in (%~n1.1) do (
set AA=%%a
@setlocal enabledelayedexpansion
if not "!AA:~0,1!"=="#" (
echo !AA! >> %~n1.2
)
@endlocal
)
:: uncomment your assembler of choice and fix up the
:: command line for your format if not "bin"
rem fasm %~n1.2
rem nasm %~n1.2 -o %~n1.bin
pause
bash script for use on Linux.