OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Mar 29, 2024 6:48 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Fun with CPP - APP (Assembly language PreProcessor)
PostPosted: Wed Mar 23, 2016 12:32 pm 
Offline
Member
Member

Joined: Thu May 19, 2011 5:13 am
Posts: 228
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

https://mikegonta.com


Last edited by mikegonta on Thu Mar 24, 2016 5:12 am, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: Fun with CPP - APP (Assembly language PreProcessor)
PostPosted: Wed Mar 23, 2016 2:54 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1925
Location: Athens, GA, USA
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.


Top
 Profile  
 
 Post subject: Re: Fun with CPP - APP (Assembly language PreProcessor)
PostPosted: Wed Mar 23, 2016 5:59 pm 
Offline
Member
Member

Joined: Thu May 19, 2011 5:13 am
Posts: 228
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:
;  @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

https://mikegonta.com


Last edited by mikegonta on Thu Mar 24, 2016 9:23 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Fun with CPP - APP (Assembly language PreProcessor)
PostPosted: Wed Mar 23, 2016 11:07 pm 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 46 guests


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 post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group