Fun with CPP - APP (Assembly language PreProcessor)

This forums is for OS project announcements including project openings, new releases, update notices, test requests, and job openings (both paying and volunteer).
Post Reply
mikegonta
Member
Member
Posts: 229
Joined: Thu May 19, 2011 5:13 am
Contact:

Fun with CPP - APP (Assembly language PreProcessor)

Post by mikegonta »

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 /* */.

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)
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.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
An experienced Linux user could contribute a similar
bash script for use on Linux.
Last edited by mikegonta on Thu Mar 24, 2016 5:12 am, edited 2 times in total.
Mike Gonta
look and see - many look but few see

https://mikegonta.com
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: Fun with CPP - APP (Assembly language PreProcessor)

Post by Schol-R-LEA »

Why use cpp when m4 and dozens of other macro preprocessors (almost all of which are superior to cpp) are equally available?
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
mikegonta
Member
Member
Posts: 229
Joined: Thu May 19, 2011 5:13 am
Contact:

Re: Fun with CPP - APP (Assembly language PreProcessor)

Post by mikegonta »

Schol-R-LEA wrote:Why use cpp when m4 and dozens of other macro preprocessors (almost all of which are superior to cpp) are equally available?
That's a very good suggestion.
In the mean time, here's a self preprocessing, self assembling "Hello World!" Windows command script. Make a folder and place the
script in it. Edit the "path" command in the script or make additional folders to contain the programs needed. An assembler - FASM
or NASM, the CPP (in this case cpp.exe and cc1.exe from GCC) and QEMU (BOCHS won't emulate a 512 byte hard drive). The
"hello.inc" file is also required. This file is "#include"(d) by and contains the "#define" used in the "hello.cmd" file.
hello.cmd

Code: Select all

;  @echo off
;  set current_path=%~p0
;  path=%current_path%;%current_path%cpp;%current_path%fasm;%current_path%nasm;%current_path%qemu;%path%
;:: for some linux reason cpp needs to be told where it is
;  if exist %~n0.2 del %~n0.2
;  cpp.exe %0 > %~n0.1
;:: pre-process cpp to get rid of cpp comments that begin
;:: with "#"
;  for /f "delims=" %%a in (%~n0.1) do ( 
;    set AA=%%a
;    setlocal enabledelayedexpansion
;    if not "!AA:~0,1!"=="#" (
;      echo !AA! >> %~n0.2
;    )
;    @endlocal
;  )
;  del %~n0.1
;:: uncomment your assembler of choice and fix up the
;:: command line for your format if not "bin"
;  fasm %~n0.2
;rem  nasm %~n0.2 -o %~n0.bin
;  del %~n0.2
;  pause
;  start %current_path%qemu\qemu -L %current_path%qemu\ -name "Assembly language PreProcessor" -m 1000 -localtime -hda %~n0.bin -display sdl
;  goto finish

/***************************************

 this is a simple Hello World! example

***************************************/
org 0x7C00
start:
#include "hello.inc"
  xor ax, ax
  mov ds, ax
  mov si, hello
  mov ah, 0xE
  xor bx, bx
.1:
  lodsb
  test al, al
  je $
  int 0x10
  jmp .1

hello db HELLO
  times 510-($-$$) db 0
  dw 0xAA55

;:finish
hello.inc

Code: Select all

#define HELLO "Hello Assembly Language PreProcessor World!",0
And here is the resulting file that is passed to the assembler

Code: Select all

org 0x7C00
start:
  xor ax, ax 
  mov ds, ax 
  mov si, hello 
  mov ah, 0xE 
  xor bx, bx 
.1: 
  lodsb 
  test al, al 
  je $ 
  int 0x10 
  jmp .1 
hello db "Hello Assembly Language PreProcessor World!",0 
  times 510-($-$$) db 0 
  dw 0xAA55
Last edited by mikegonta on Thu Mar 24, 2016 9:23 am, edited 1 time in total.
Mike Gonta
look and see - many look but few see

https://mikegonta.com
alexfru
Member
Member
Posts: 1111
Joined: Tue Mar 04, 2014 5:27 am

Re: Fun with CPP - APP (Assembly language PreProcessor)

Post by alexfru »

And I just wrote a NASM to FASM syntax converter for my compiler. It also combines section fragments, which may be a bit too much work for a C preprocessor.
Post Reply