flat assembler
Message board for the users of flat assembler.

Index > Main > Embedded Checksum

Author
Thread Post new topic Reply to topic
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 06 Jun 2005, 14:24
Hi All,

I was searching the site for an Embedded Checksum feature built into FASM and wasn't able to find one. Is there such a thing? Perhaps my explanation isn't clear either: I am looking to place something, call it a tag, into the code where the compiler automatically embeds the checksum of the entire project I compile. Soemthing like:

Code:
...entire code above...

32-bitChecksum               ; <-- Embedded checksum of entire build (possible? )
    


I realize I can simply leave space for this type of thing and read in the file and place a checksum at that location, but I was hoping that there may be a built in feature within FASM?!?


Last edited by smiddy on 06 Jun 2005, 14:33; edited 1 time in total
Post 06 Jun 2005, 14:24
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 06 Jun 2005, 14:30
There is no such thing in general, though formatter for some output formats might generate some particular types of checksums, like for the PE format. However you can calculate the checksum yourself using "load" directive (and implement whatever checksum type you need), like:
Code:
checksum = 0

repeat $
 load a byte from %-1
 checksum = (checksum + a) and 0FFFFFFFFh
end repeat

dd checksum    

This will actually work with raw output format only, with other formats the problem is a bit more complex.
Post 06 Jun 2005, 14:30
View user's profile Send private message Visit poster's website Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 06 Jun 2005, 14:35
Thanks! You're the MAN!

Raw format is what I'm concerned with presently...
Post 06 Jun 2005, 14:35
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 07 Jun 2005, 18:52
I found this on the board a while ago and thought it was worth saving.
Haven't used it myself, now that I've translated MD5 to FASM(Win32|MeOS)

See for yourself:
Code:
macro generate_crc starting_lbl, ending_lbl {
  __ptr = starting_lbl
  __lenght = ending_lbl-starting_lbl
  __times = (__lenght)shr 2
  __timesleft = (__lenght)-(((__lenght)shr 2)shl 2) ; remaining 3 - 0
  __timesleft2 =__timesleft shr 1 ; word
  __timesleft3 =__timesleft - (__timesleft2 shl 1) ; last byte

  __sum = 0
  repeat __times
    load __tmp dword from __ptr
    __sum = __sum xor __tmp
    __ptr = __ptr + 4
  end repeat
    if __timesleft2>0   ; word
    load __tmp word from __ptr
    __sum = __sum xor (__tmp and $ffff)
    __ptr = __ptr + 2
    end if
    if __timesleft3>0 ; last byte
    load __tmp byte from __ptr
    __sum = __sum xor (__tmp and $ff)
    __ptr = __ptr + 1
    end if

  dd __sum and 0xffffffff
}

;it will store dword value that would be a sum of all dwords in the output code, between two given labels. Example:
;Code:
begin:
     db "All data and..",0
   MyCode:
     mov eax,ebx
     and edx,ebp
     je  MyCode
     db "should be here Very Happy"
finish:

db "CRC:" ;This should be left out - you just can see better with some hexviewer
generate_crc begin,finish 
    
Post 07 Jun 2005, 18:52
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 07 Jun 2005, 22:42
Hi Madis731,

I'll give it a whirl this evening, thanks!
Post 07 Jun 2005, 22:42
View user's profile Send private message 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.