flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > How to store 32bit (or more) CRC @end of file @compile time?

Author
Thread Post new topic Reply to topic
Matrix



Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 08 Nov 2004, 18:56
Hy,
can someone give me a hint on some macro?
i'd like to store my executable's 32bit crc @ the end so i can later verify it during runtime, and see if any modifications were made.
thnx
Post 08 Nov 2004, 18:56
View user's profile Send private message Visit poster's website Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard 08 Nov 2004, 19:17
quick dirty solution, but you should be able to modify it that it would fit your needs.
Code:
macro generate_crc starting_lbl, ending_lbl {
  __ptr = starting_lbl
  __times = (ending_lbl-starting_lbl)/4
  __sum = 0
  repeat __times
    load __tmp dword from __ptr
    __sum = __sum + __tmp
    __ptr = __ptr + 4
  end repeat
  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:
     dd 11
     dd 2
     dd 2
finish:

db "CRC:"
generate_crc begin,finish    
Post 08 Nov 2004, 19:17
View user's profile Send private message Visit poster's website Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 08 Nov 2004, 19:32
Thank you Decard Smile
Post 08 Nov 2004, 19:32
View user's profile Send private message Visit poster's website Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 08 Nov 2004, 19:42
Here is what i did with it, not much modification was needed Smile
now works with any block size
well exacly this is not a full crc32, but it should be enough for me now

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:

     dd $ABCD
begin:
     dd 10101010101010101010101010101010b
     dd 11111111111111111111111111111111b
     dd 11111111111111111111111111111111b
     dd 10101010101010101010111010101010b
     db 1b,0
;     dw 1b
;     dw 0
;     dw 100000001b

finish:

db "CRC:"
generate_crc begin,finish

    
Post 08 Nov 2004, 19: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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.