flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [fasmg] [fasm2] hexdump.inc macro to make raw hexdump

Author
Thread Post new topic Reply to topic
dosmancer



Joined: 20 Feb 2025
Posts: 7
Location: Kingdom of Sweden
dosmancer 02 Mar 2025, 22:46
I took fasm2's listing.inc as inspiration to make a raw hexdump.

Double-checked against `hexdump` tool in Ubuntu.

Code:
; Include hexdump.inc at top of main assembly file
; to generate a hexdump with hex file extension.
; Number of bytes per line can be customized.
; Inspired by fasm2's listing.inc.

define Hexdump? Hexdump

namespace Hexdump

    virtual at 0
        HexDigits:: db '0123456789abcdef'
    end virtual

    bytes_per_line := 16

    calminstruction generate
            local filesize, i, byte, nibble
            compute filesize, $%%
            compute i, 0

        loop:
            check i >= filesize
            jyes done
            load byte, i, 1

            compute nibble, byte shr 4
            load nibble, HexDigits:nibble, 1
            emit 1, nibble

            compute nibble, byte and 0Fh
            load nibble, HexDigits:nibble, 1
            emit 1, nibble

            compute i, i + 1

            check i mod bytes_per_line = 0
            jyes newline
            emit 1, ' '
            jump loop

        newline:
            emit 1, 13
            emit 1, 10
            jump loop

        done:
    end calminstruction
end namespace

postpone ?
    purge ?
    virtual as 'hex'
        Hexdump.generate
    end virtual
end postpone
    


Creates a .hex file containing hexdump:
Code:
4d 5a 69 01 07 00 1f 00 0a 00 20 00 ff ff cd 00
00 02 00 00 29 00 a0 00 1c 00 00 00 3d 00 00 00
a3 01 00 00 0f 06 00 00 0c 09 00 00 49 09 00 00
73 09 00 00 9d 09 00 00 2a 0a 00 00 36 0a 00 00
e1 0a 00 00 e6 0a 00 00 eb 0a 00 00 45 0b 00 00
[...]
    


For me it's convenient to create a hexdump after each assembling. I set the bytes_per_line to 1 and then check the hexdump into my repo. One byte per lines makes it the easiest for git diff to understand what changed in the binary when I change the assembly code.
Post 02 Mar 2025, 22:46
View user's profile Send private message Reply with quote
Mat-Quasar



Joined: 02 Mar 2025
Posts: 26
Mat-Quasar 04 Mar 2025, 15:22
There is another hdmp.asm fasm1 macro by @macomics:
https://board.flatassembler.net/topic.php?p=221329#221329
Post 04 Mar 2025, 15:22
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4161
Location: vpcmpistri
bitRAKE 04 Mar 2025, 18:57
macomics other script is quite elegant, imho:
https://board.flatassembler.net/topic.php?p=220038#220038

It's what I based my fasmg script on.
https://github.com/bitRAKE/fasmg-umbrella/blob/main/scripts/hexdump.g

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 04 Mar 2025, 18:57
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.