flat assembler
Message board for the users of flat assembler.

flat assembler > High Level Languages > Fun with CPP - APP (Assembly language PreProcessor)

Author
Thread Post new topic Reply to topic
Mike Gonta



Joined: 26 Dec 2010
Posts: 212
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:
//#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:
  @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.

_________________
Mike Gonta
look and see - many look but few see

http://mikegonta.com


Last edited by Mike Gonta on 24 Mar 2016, 11:13; edited 1 time in total
Post 23 Mar 2016, 18:21
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16108
Location: Squiddler's Patch
Interesting.

But, umm, why would I want to run NASM when I already have FASM? Confused
Post 23 Mar 2016, 23:40
View user's profile Send private message Visit poster's website Reply with quote
Mike Gonta



Joined: 26 Dec 2010
Posts: 212
revolution wrote:
Interesting.
But, umm, why would I want to run NASM when I already have FASM? Confused
Mike Gonta wrote:
(try both to confirm that this does indeed work or have a look at the *.2 file which is sent to the assembler)
And you don't need to run NASM.
I have FASM and I also have NASM, but I prefer FASM.
It's simply a matter of writing one set of assembly language files that will assemble on both FASM and NASM and
probably some other intel syntax assemblers (but not MASM or GAS).

_________________
Mike Gonta
look and see - many look but few see

http://mikegonta.com
Post 23 Mar 2016, 23:59
View user's profile Send private message Reply with quote
Mike Gonta



Joined: 26 Dec 2010
Posts: 212
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. FASM, 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:
;  @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:
#define HELLO "Hello Assembly Language PreProcessor World!",0    
And here is the resulting file that is passed to the assembler
Code:
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    

_________________
Mike Gonta
look and see - many look but few see

http://mikegonta.com


Last edited by Mike Gonta on 24 Mar 2016, 15:16; edited 1 time in total
Post 24 Mar 2016, 00:06
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16108
Location: Squiddler's Patch
Mike Gonta wrote:
And you don't need to run NASM.
I am happy to read that Smile
Mike Gonta wrote:
(but not MASM or GAS).
I am happy to read that also Smile

So all I need is fasm, right? Razz

But perhaps this is more useful for someone who mixes C/C++ with fasm?
Post 24 Mar 2016, 00:37
View user's profile Send private message Visit poster's website Reply with quote
Mike Gonta



Joined: 26 Dec 2010
Posts: 212
revolution wrote:
So all I need is fasm, right?
Right. But you're missing the point.This will assemble on FASM but not on NASM:
Code:
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    
And this will assemble on NASM but not on FASM:
Code:
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    
They are both same and yet the directives are different.
This will assemble on both after preprocessing with CPP:
Code:
/***************************************

 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    

_________________
Mike Gonta
look and see - many look but few see

http://mikegonta.com


Last edited by Mike Gonta on 24 Mar 2016, 15:15; edited 1 time in total
Post 24 Mar 2016, 00:54
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16108
Location: Squiddler's Patch
Okay, further to this, how does this handle macro blocks? Postpone? Format? Register arithmetic?
Post 24 Mar 2016, 01:32
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2018, Tomasz Grysztar.

Powered by rwasa.