flat assembler
Message board for the users of flat assembler.

Index > Main > Batch file doubling as a fasmg source

Author
Thread Post new topic Reply to topic
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7414
Location: Kraków, Poland
Tomasz Grysztar
Recently I came up with this fun little trick to make a batch file that could also be assembled with fasmg, and when executed would run fasmg to assemble itself:
Code:
@echo off
goto batch
end if
macro @echo off
        if 0
end macro

virtual at 0
        digits::
        n = %t / 50
        while n
             d = '0' + n mod 36
             if d > '9'
                d = d - '0' - 10 + 'a'
             end if
             n = n / 36
             db d
        end while
        count = $
end virtual
repeat count
        load d byte from digits:count-%
        display d
end repeat

macro ?! any&
end macro

:batch
fasmg %~f0 nul -n    
The inner assembly script that is executed here is the one I use to generate fasmg's version number (it works either with fasm 1 or fasmg, but the trick that allowed to embed it in batch file only works with fasmg).
Post 09 May 2019, 11:12
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16847
Location: In your JS exploiting you and your system
revolution
Is this to save having a second file on disk?

We can also do this in fasm 1 slightly differently by creating temporary files:
Code:
@echo off

echo ;This file is auto generated by %~f0    >%~f0.temp

echo virtual at 0                            >>%~f0.temp
echo         digits::                        >>%~f0.temp
echo         n = %t / 50                     >>%~f0.temp
echo         while n                         >>%~f0.temp
echo              d = '0' + n mod 36         >>%~f0.temp
echo              if d > '9'                 >>%~f0.temp
echo                 d = d - '0' - 10 + 'a'  >>%~f0.temp
echo              end if                     >>%~f0.temp
echo              n = n / 36                 >>%~f0.temp
echo              db d                       >>%~f0.temp
echo         end while                       >>%~f0.temp
echo         count = $                       >>%~f0.temp
echo end virtual                             >>%~f0.temp
echo repeat count                            >>%~f0.temp
echo         load d byte from digits:count-% >>%~f0.temp
echo         db d                            >>%~f0.temp
echo end repeat                              >>%~f0.temp

fasm %~f0.temp %~f0.temp2

copy %~f0.temp2 con

del %~f0.temp
del %~f0.temp2    
Or something similar. I can't test it ATM. But the idea is there to create a temp file and the assemble it to another temp file. And after displaying the output delete the two temp files.
Post 09 May 2019, 12:59
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7414
Location: Kraków, Poland
Tomasz Grysztar
revolution wrote:
Is this to save having a second file on disk?
Yes, but it is also just a fun thing to do, to design polyglot batch files. And polyglot files in general, that seems to be an entire form of art on its own.
Post 09 May 2019, 13:33
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4217
Location: 2018
edfed
very cool feature, i never use os capabilities. maybe it's time to...

This trick makes possible to postcompile generated sources , i like it. Smile
Post 13 May 2019, 07:40
View user's profile Send private message Visit poster's website Reply with quote
d469964



Joined: 19 Mar 2012
Posts: 3
d469964
I think you overcomplicate things:
Code:
@rem: if 0
@fasmg "%~f0" nul
@exit
end if

virtual at 0
        digits::
        n = %t / 50
        while n
             d = '0' + n mod 36
             if d > '9'
                d = d - '0' - 10 + 'a'
             end if
             n = n / 36
             db d
        end while
        count = $
end virtual
repeat count
        load d byte from digits:count-%
        display d
end repeat    

Works perfectly with fasm & fasmg.
Post 20 May 2019, 01:18
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16847
Location: In your JS exploiting you and your system
revolution
d469964 wrote:
I think you overcomplicate things:
Code:
@rem: if 0
@fasmg "%~f0" nul
@exit
end if

virtual at 0
        digits::
        n = %t / 50
        while n
             d = '0' + n mod 36
             if d > '9'
                d = d - '0' - 10 + 'a'
             end if
             n = n / 36
             db d
        end while
        count = $
end virtual
repeat count
        load d byte from digits:count-%
        display d
end repeat    

Works perfectly with fasm & fasmg.
Cool Nice.

Is there a way to also make it work with bash?
Post 20 May 2019, 07:48
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7414
Location: Kraków, Poland
Tomasz Grysztar
d469964 wrote:
I think you overcomplicate things
I think you missed the "fun" word in my initial post. Wink

Your variant is of course great at being concise, but I designed mine to be able to accomodate existing source code and existing batch script and have them cleanly separated without a risk of having a command within the batch portion that could be misinterpreted by the assembler. In your case inserting a script containing a command like "if errorlevel" would break the assembly.
Post 20 May 2019, 08:03
View user's profile Send private message Visit poster's website Reply with quote
d469964



Joined: 19 Mar 2012
Posts: 3
d469964
d469964 wrote:
In your case inserting a script containing a command like "if errorlevel" would break the assembly.

It's not a problem:
Code:
@rem: if 0
@fasm "%~f0" nul 
@if errorlevel 1 (
  echo Compilation error!
  pause
)  
@goto :EOF
end if    
Post 20 May 2019, 17:18
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7414
Location: Kraków, Poland
Tomasz Grysztar
This was my point exactly - you needed to check and modify the batch commands to ensure that they are written in way that does not cause problems. The framework I proposed should allow to take a script as-is and not worry about possible interference.
Post 20 May 2019, 17:26
View user's profile Send private message Visit poster's website Reply with quote
d469964



Joined: 19 Mar 2012
Posts: 3
d469964
Quote:

Is there a way to also make it work with bash?

I'm not a Linux guy and create only this (bash only):
Code:
echo=0
if echo; then
  fasm "$0"
  exit
fi
end if    

and this (both bash and batch, but print one line of garbage):
Code:
rem=""; : '
@rem: if 0 
@fasmg "%~f0" nul
@exit
;'
fasmg "$0"
exit
end if    
Post 20 May 2019, 19:30
View user's profile Send private message Reply with quote
Grom PE



Joined: 13 Mar 2008
Posts: 114
Location: i@grompe.org.ru
Grom PE
Thankfully the semicolon is both a comment in fasm and ignored in batch, so I was able to come up with a polyglot file with batch and fasm source completely separate:

https://github.com/grompe/kbdasm/blob/e12b8c1a41f36ecbf8d5486af1716226233e72c2/diskbd.bat
Post 21 May 2019, 06:37
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo
I'm no expert, but this can also be done in Perl, Ruby, etc. with .BAT. (Also, Bash shell scripts can have here docs or whatever.)
Post 21 May 2019, 17:42
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-2019, Tomasz Grysztar.

Powered by rwasa.