flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Pe Batch-Macro Auto Increment Versioning system

Author
Thread Post new topic Reply to topic
hopcode



Joined: 04 Mar 2008
Posts: 563
Location: Germany
hopcode
Hallo all,
playng with Fasm 1.67.26 preprocessor capability i have done this little batch/macro snippet to create maj/min versioning in the PE OPTIONAL HEADER.
Stable Release 26 Nov.2008
To have it working:
- copy the following 4 files in the fasm path dir, for example: "C:\fasm"

-- count.asm
-- counter.asm
-- version.bat
-- vbuilder.asm

- open te file "version.bat" and change the fasmpath accordingly
- and in your prj directory on the command, write for example:

Usage:

    - OK version myprog.asm myprogr.dll (counters start from 0/1)
    - OK version myprogr.dll (myprogr.asm is the main file)
    - OK version myprogr.dll alfa=beta gamma=delta (accept -d alfa=beta...)
    - OK version myprogr.exe minor=20 alfa="beta" major=10h
    - OK version myprogr.exe


Here the code:
count.asm

Code:
vmajor dw 0
vminor dw 0
  
if ~ (defined major | defined minor) 
  load cmajor word from 0
  load cminor word from 2 
  else
        if defined major + minor
            cmajor = major
              cminor = minor
              else
                        if defined minor
                            cminor = minor
                              load cmajor word from 0
                     else if defined major
                               cmajor = major
                              load cminor word from 2
                     end if  
            end if 
     end if
              
 store word cmajor at vmajor
 store word cminor at vminor
    


version.bat

Code:
@ECHO off
    title Pe Versioning Batch-Macro by hopcode[mrk]
     echo.
       echo  PE Versioning by hopcode[mrk] 26.Nov.2008 
    echo   using MS-DOS batching and FASM 1.67.26
REM echo.
REM echo  NOTE: !!! This is only for standard PE-DLL file !!!
REM echo  SEE description in file "vbuilder.asm"

REM change the following line according to your fasm path
REM -------------------------------------------------
     set fasmpath=C:\fasm
REM -------------------------------------------------
REM then copy in it the following files:
REM 
REM count.asm    --- base count
REM counter.asm  --- base incrementer
REM vbuilder.asm --- version "storer"
REM version.bat  --- this file
REM
REM -------------------------------------------------

:test_target
 IF DEFINED fSource GOTO test_target1
        IF "%1"=="" ( 
              ECHO  ERR:target filename required
              GOTO exitprog
       )
       IF "%~x1"=="" ( 
            ECHO  ERR: extension required for [%~n1]
                GOTO exitprog
       )
       IF %~x1==.asm ( 
                REM ECHO  source name %~n1
          SET sName=%~n1
              SHIFT
               SET fSource=OK
              GOTO test_target
    )
:test_target1
  IF "%~x1"=="" ( 
            ECHO  ERR: invalid target [%~n1]
                GOTO exitprog
       )
       IF NOT DEFINED fSource SET sName=%~n1

   IF %~x1==.exe ( 
                REM ECHO  target extension  %~x1
            SET tExt=%~x1
               SET tName=%~n1
              SHIFT
               GOTO test_tparam
    )
       IF %~x1==.dll ( 
                REM ECHO  target extension  %~x1
            SET tExt=%~x1
               SET tName=%~n1
              SHIFT
               GOTO test_tparam
    )

:test_tparam
       ECHO  Ok: source [%sName%.asm]
  ECHO  Ok: target [%tName%%tExt%]
        SET sLine=%sName%.asm
       
:test_tparam1
       IF "%1"=="" ( 
              GOTO compile 
       )
       IF "%2"=="" ( 
              ECHO  ERR:value for [%1] required
               GOTO exitprog
       )
       IF %1==minor ( 
         ECHO  Setting v-Minor to [%2]
               SET vLine=%vLine% -d minor=%2
               GOTO :shift_tparam1
             )

   IF %1==major ( 
         ECHO  Setting v-Major to [%2]
               SET vLine=%vLine% -d major=%2
               GOTO :shift_tparam1
             )
       
:shift_tparam   
    SET sLine=%sLine% -d %1=%2
:shift_tparam1
    SHIFT
       SHIFT
       GOTO test_tparam1

:compile
       ECHO  vLine is [%vLine%]
    ECHO  sLine is [%sLine%]
    ECHO  *********************
 ECHO   COMPILING [%sLine%] to [%tName%%tExt%]
       fasm %sLine% %tName%%tExt%
  IF ERRORLEVEL 1 GOTO err_exit

:update_ver
        IF NOT EXIST %tName%_count.bin (
                fasm %fasmpath%\count.asm %tName%_count.bin %vLine%
                IF ERRORLEVEL 1 GOTO err_exit
               )

:update_ver2
               SET vcount=%tName%_count.bin
                SET filename=%tName%%tExt%
          ECHO.
           ECHO   UPDATING Counter for %tName%%ext%
                fasm %fasmpath%\counter.asm %tName%_count.bin %vLine%
              IF ERRORLEVEL 1 goto err_exit

           fasm %fasmpath%\vbuilder.asm %tName%.bin > nul
      IF ERRORLEVEL 1 goto err_exit

               COPY %tName%.bin %tName%%tExt% > nul
             DEL %tName%.bin > nul
    GOTO exitprog

:exitprog

:err_exit
     SET fasmpath=
       SET tLine=
  SET sLine=
  SET tName=
  SET tExt=
   SET sName=
  SET fSource=
        SET vLine=
  SET vcount=
 set filename=
    

vbuilder.asm
Code:

 virtual at 0
   file  '%vcount%'
   load cmajor word from 0
   load cminor word from 2
  end virtual 
  
  ;------------------------------------------------
  ; --- suitable for PE/DLL with these 
  ; --- following features (the majority):
  ; --- 80h = sizeof MZ HEADER+DOS STUB
  ; --- 18h = sizeof PE FILE HEADER + PE SIGNATURE (dword)
  ; --- 2Ch/2Eh = offset words Mayor/Minor version
  ; ------------------------------------------------
  
  file '%filename%'
  store word cmajor at 80h+18h+2ch
  store word cminor at 80h+18h+2eh
     

counter.asm
Code:
macro display_hex bits,symb_const {
  tmpbits  = bits
  tmpvalue    = symb_const 
   repeat tmpbits/4
        d = '0' + tmpvalue shr (tmpbits-%*4) and 0Fh
      if d > "9"
      d = d + "A"-"9"-1
      end if
      display d
   end repeat
 display 13,10
 }
 
file '%vcount%'
 if ~ (defined major | defined minor) 
   load cmajor word from 0
   load cminor word from 2 
    else
        if defined major + minor
            cmajor = major
              cminor = minor
              else
                        if defined minor
                            cminor = minor
                              load cmajor word from 0
                     else if defined major
                               cmajor = major
                              load cminor word from 2
                     end if  
            end if 
     end if

if cminor = 0FFFFh
  cmajor = cmajor +1
end if
  cminor = (cminor +1) and 0FFFFh
     display " build v-Major is "          
    display_hex 16,cmajor
       display " build v-Minor is "          
    display_hex 16,cminor

 store word cmajor at 0
 store word cminor at 2

    

Enjoy,
hopcode Wink

_________________
⠓⠕⠏⠉⠕⠙⠑


Last edited by hopcode on 05 Apr 2011, 16:41; edited 10 times in total
Post 21 Nov 2008, 19:02
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: 17346
Location: In your JS exploiting you and your system
revolution
Maybe you can also consider detecting when assembly has failed and not update the counters.

BTW: Perhaps it might be better/easier to make a small exe file to do all the work. The bat/asm combo seems a little bit cumbersome to my taste. Is there some reason you chose to use the bat/asm method? I'm trying to think why/where it might be an advantage over a small exe.
Post 21 Nov 2008, 19:23
View user's profile Send private message Visit poster's website Reply with quote
hopcode



Joined: 04 Mar 2008
Posts: 563
Location: Germany
hopcode
revolution wrote:
Maybe you can also consider detecting when assembly has failed and not update the counters.

Maybe you can also consider bringing evidence when assembly has failed and the counters would be updated... Laughing

Regards,
hopcode
Post 22 Nov 2008, 02:40
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: 17346
Location: In your JS exploiting you and your system
revolution
Code:
        @fasm %fasmpath%\counter.asm count.bin
        del %1_count.bin    
I don't see any detection of failure here?
Post 22 Nov 2008, 05:45
View user's profile Send private message Visit poster's website Reply with quote
hopcode



Joined: 04 Mar 2008
Posts: 563
Location: Germany
hopcode
revolution wrote:
I don't see any detection of failure here?

Do you ? But, seriously
It is a real pleasure to talk to you. Very Happy
especially when you are Gone away Fishing... Laughing
The problem is that of the three Musketeers...People believe
that they were only three, but to tell it all, in reality, they are four Musketeers, and they reside in the same %fasmpath%. Also, dont worry ! All would not go in the Wrong (to paraphrase a "Talkinheads-ity").
If someone, sword in hand, chops/cuts up the HDD, where the %fasmpath% resides, well, that is a more serious matter, an other old old story... Wink
Have i persuaded you ? Very Happy
regards,
hopcode
Post 22 Nov 2008, 08:29
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
hopcode,

Update must be done atomically, i.e. all of
Code:
        …
        fasm %1.asm "%1%ext%"
        …
        fasm %fasmpath%\vbuilder.asm %1.exe
        …
        @fasm %fasmpath%\counter.asm count.bin
        del %1_count.bin
        ren count.bin %1_count.bin
        …    
must succeed, error message otherwise.

By the way, my .DLL is still ver. 0.00, and mysteriously .Exe appears… Wink
Post 22 Nov 2008, 13:22
View user's profile Send private message Reply with quote
hopcode



Joined: 04 Mar 2008
Posts: 563
Location: Germany
hopcode
ooOps...
Thanks for the tips.Change the line
baldr wrote:

fasm %fasmpath%\vbuilder.asm %1.exe

in
Code:
   fasm %fasmpath%\vbuilder.asm
    

(or rather see the updated source).
Quote:

By the way, my .DLL is still ver. 0.00, and mysteriously .Exe appears…

right, if you dont specify :
version filename dll.
BTW, I say it publicly: according to me, you and revolution are the same person, dont you ? Very Happy
Should i explain why ?

regards,
hopcode
Post 22 Nov 2008, 16:37
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: 17346
Location: In your JS exploiting you and your system
revolution
hopcode wrote:
BTW, I say it publicly: according to me, you and revolution are the same person, dont you ? Very Happy
Should i explain why ?
Yes, I should think an explanation is warranted here. Question

I'm curious as to what makes you come to that conclusion?
Post 22 Nov 2008, 16:41
View user's profile Send private message Visit poster's website Reply with quote
hopcode



Joined: 04 Mar 2008
Posts: 563
Location: Germany
hopcode
revolution wrote:

I'm curious as to what makes you come to that conclusion?

Ok, consider the things in the following light ...
Smile
fassen wir mal zusammen... (~tr: let summarize...)
1) You and papa Baldr help a lot the primers. a + for you both

2) You and papa Baldr for HL discussion (like the one here)
have lot of tips/suggestions and no direct code.(that is normal, one asm-expert would say)
Also, a ++ for papa and a + for you, because he makes use
of pseudolanguages too, appositedly created by himself.

3) When you disappear for a short time,papa does his appearance.

4) Same idiomatic peculiarities...

5) You are both not aggressive. a + for you both

6) And Last, but not least, he is, with few posts, like your protégé
not on technical matters. In fact, on technical matters he can manage himself, alone by himself, without this "your protective attitude", i would say,
a papa's attitude. Very Happy

Note: point 6 could be understandable by old frequent board people only.
This doesnt act as evidence.

Finally, it seems you are both the compliment to 1 person.
or i like figuring it so out...

regards,
hopcode
Post 22 Nov 2008, 17:31
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
hopcode,

In updated source
Code:
fasm %fasmpath%\vbuilder.asm    
compiles to vbuilder.bin which is deleted several lines later, thus destroying the result. What's the catch?

revolution,

I'm stunned. No comments. Wink
Post 22 Nov 2008, 17:35
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17346
Location: In your JS exploiting you and your system
revolution
hopcode: Curious, while I am flattered to get all those +'s I am also surprised that someone has taken the time to consider things like this. Why would you imagine I would need a sock-puppet? Have you also considered that if what you say is true then I would have also taken to replying to myself, sometimes contradictorily, that would be weird don't you think? Kind of like Dr. Jekyll and Mr. Hyde, hehe.

Do you know that posting on here is not anonymous? Any admin can simply look up the IP code of each poster.
Post 22 Nov 2008, 17:42
View user's profile Send private message Visit poster's website Reply with quote
hopcode



Joined: 04 Mar 2008
Posts: 563
Location: Germany
hopcode
baldr wrote:

...fasm %fasmpath%\vbuilder.asm

you are right, my shame Embarassed
after that line, it needs something like this
Code:
copy %fasmpath%\vbuilder.bin %1%ext%
    

...see updated 2 source and thanks a lot
for your attention.

regards,
hopcode
Post 22 Nov 2008, 17:59
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
revolution,

Randall Hyde Wink

hopcode,

So what about error checking/recovery (apart from main source compilation)? Wink
Post 22 Nov 2008, 19:40
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17346
Location: In your JS exploiting you and your system
revolution
Hey baldr are you me, I mean am I you? Oh heck, I am so confused. Who am I today? The real scary Q is which of us is Mr. Hyde? Pick me, pick me, I wanna be the bad guy Mr. Hyde, and baldr can be the clever scientist Dr. Jekyll Twisted Evil
Post 22 Nov 2008, 19:48
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
revolution, we make a deal: on even julian day number you're Mr. Hyde, on odd — I'm. On February 29th Mr. Hyde is Mr. Hyde himself. Wink
Post 22 Nov 2008, 20:15
View user's profile Send private message Reply with quote
hopcode



Joined: 04 Mar 2008
Posts: 563
Location: Germany
hopcode
Very Happy a good mood,
...apart eror checking/recovery...

regards,
hopcode (aka Mark Rainer Kranz)
Post 23 Nov 2008, 02:27
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
hopcode,

Another batch Wink of suggestions:

version.bat

fasm+ver.cmd source.ext target.ext may be more descriptive name and consistent (with fasm.exe) syntax for version.bat. With little effort, you can even accept it fully, with options (but, there is more: you can introduce your own options as well! Just don't forget to cut them out from fasm command line Wink).

Well, not so little and not fully (batch language is barely documented and full of quirks), here is example:
Code:
@echo off
set source=
set target=

:ParseCmdLine
  if ""=="%1" goto ParseDone
rem With command extensions enabled you may use /i if option
rem -d option requires specific handling because of =
  for %%o in (D d) do if "%1"=="-%%o" ( echo Option: "%1 %2=%3" & shift & shift & goto Continue )
  for %%o in (M m P p S s) do if "%1"=="-%%o" ( echo Option: "%1 %2" & shift & goto Continue )
  if ""=="%source%" (
    set source=%1 & goto Continue
  ) else (
    if ""=="%target%" ( set target=%1 & goto Continue )
  )
rem Here we come with unrecognized syntax
  echo WTF is "%1"?
:Continue
  shift
goto ParseCmdLine

:ParseDone
echo Source: "%source%"
echo Target: "%target%"    
It's not fully tested though.

You may use %~p#, %~n# and %~x# for path, name and extension portions, respectively, of batch parameter %#. You don't really need the extension, do you?

count.asm

Is it necessary to load file "%vcount%" twice in count.asm?

Idea for the next major upgrade: VERSION.INC from fasm package. It's simple to parse, not much harder to update ("9" => "10").

vbuilder.asm

Parse MZ/PE headers.
Post 23 Nov 2008, 10:49
View user's profile Send private message Reply with quote
hopcode



Joined: 04 Mar 2008
Posts: 563
Location: Germany
hopcode
Quote:

count.asm
Is it necessary to load file "%vcount%" twice in count.asm?

No, that is so written only for a clearness pourpose.
and
Quote:

vbuilder.asm
Parse MZ/PE headers.

Yes, the next versions, as i have time
baldr wrote:

Another batch Wink of suggestions:

..follows your example
Well,i think it is a bit too much. And consequently, in this sense, i agree with the
"cumbersome" adjective that revolution said about using of asm/bat, if i am not mistaken. I like this simple solution (that doesnt exlude merging other suggestions from you,...mmmh..thinking... Rolling Eyes ) because i perceive it even integrated, and on the "minimum requirement" for a version incrementing system.
BTW, i see that the thing is of some interest for you,isnt it ?

regards
Post 23 Nov 2008, 11:34
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
hopcode wrote:
BTW, i see that the thing is of some interest for you,isnt it ?
As in almost any problem discussed on this board. When I see some solution, several questions arise immediately in my mind:

1. Can it be improved?
2. Is it as generic as possible?
3. Is there any other way to solve that problem?
And last, but not least:
4. Does it handle marginal cases / errors? Wink

For example, my question about duplicate file "%vcount%" was because when I encounter almost equal and self-contained code fragments I ask myself: "Why don't define macro for that?"

Even simple 4-liner for loading word from file will clearly state it's purpose and (moreover) similar semantic of that fragments (up to the parameters).
Post 23 Nov 2008, 15:20
View user's profile Send private message Reply with quote
hopcode



Joined: 04 Mar 2008
Posts: 563
Location: Germany
hopcode
Stable Release 26 Nov.2008
see first post
special thanks to "papa" Baldr
special thanks to revolution

hopcode
Very Happy
Post 26 Nov 2008, 16:11
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-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.