flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [fasmg] make DB, obfuscate (why?)

Author
Thread Post new topic Reply to topic
bitRAKE



Joined: 21 Jul 2003
Posts: 3486
Location: vpcmipstrm
bitRAKE
Code:

; Make data statements from file (don't use this).

if definite FILE
virtual at 0
        DAT::
        file FILE
        BYTES := $
end virtual

db '; ',FILE,13,10
repeat 1,D:BYTES
        db 'label dat:',`D,13,10
end repeat
db 'db \',13,10
data_dump BYTES,,db

calminstruction hex_nibble digit*, command: display
        compute digit, 0FFh and '0123456789ABCDEF' shr (digit*8)
        arrange command, command digit
        assemble command
end calminstruction

define _db
calminstruction data_dump bytes*, i:0, cmd:display
        local awk, digit
jj:     arrange         awk, =load =_db:1 =from =DAT:i
        assemble        awk
        arrange         awk, cmd '0x'
        assemble        awk
        compute         digit, (_db shr 4) and 0Fh
        call            hex_nibble, digit, cmd
        compute         digit, _db and 0Fh
        call            hex_nibble, digit, cmd
        compute         i, i + 1
        check           i = bytes
        jyes            done
        arrange         awk, cmd ','
        assemble        awk
        check           i and 0Fh
        jyes            jj
        arrange         awk, cmd '\',13,10
        assemble        awk
        jump            jj
done:   arrange         awk, cmd 13,10
        assemble        awk
end calminstruction

else
        display 'usage: fasmg -i "define FILE ''<filename>''" ',__FILE__,' <outfile>'
end if    

_________________
¯\(°_o)/¯ unlicense.org
Post 11 May 2022, 18:02
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3486
Location: vpcmipstrm
bitRAKE
The nice thing about fixed VHD files is that it's just composed of a footer structure. So, the disk image can usually still be loaded into other tools. Here is a macro to build that footer structure ...

I could have opted to get rid of all the magic numbers, but this is only mildly more readable:
Code:
                virtual at $ - 512
                        footer VHD.Footer
                end virtual
                load sig:sizeof VHD.Footer.Signature from footer.Signature
                load ver:sizeof VHD.Footer.Version from footer.Version
                load met:sizeof VHD.Footer.NextMeta from footer.NextMeta
                load typ:sizeof VHD.Footer.DiskType from footer.DiskType
                if sig = VHD.Cookie \
                & ver = 0x0000_0100 \
                & met = VHD.FixedMeta \
                & typ = ((VHD.DiskType.Fixed) bswap 4)
                        FILE.BYTES = $ - 512
                else ; assume RAW disk image
                        FILE.BYTES = $
                end if    
... my intention is just to tack the footer on files - not to write additional tools for VHD.

For example, in Linux DD can copy a disk and then add a footer to load it into VM.


Description:
Download
Filename: makevhd.zip
Filesize: 1.99 KB
Downloaded: 57 Time(s)


_________________
¯\(°_o)/¯ unlicense.org
Post 15 May 2022, 22:54
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3486
Location: vpcmipstrm
bitRAKE
Today I had a need to format some JSON, which was output in a minimized form (from a source I don't have control of). It was over 200MB and ballooned to 360MB, but at least my editor can view it now.
Code:
; strip whitespace from JSON file and re-format with tab

if definite FILE
virtual
DAT::
file FILE
DAT_END := $ - $$
end virtual

define _db
calminstruction json_prettier i:0,instring:0,depth:0
        local awk,char
more:
        arrange awk, =load =_db:1 =from =DAT:i
        assemble awk
        compute i, i + 1
        compute char, 0+_db

        check instring
        jyes string_contents

        check _db = '"'
        jyes string_inout
        check _db = ','
        jyes output_indent

        check _db = '{'
        jyes increase_depth
        check _db = '}'
        jyes decrease_depth

        check _db = '['
        jyes increase_depth
        check _db = ']'
        jyes decrease_depth

        ; consume existing whitespace
        check _db = ' '
        jyes skip_ws
        check _db = 10
        jyes skip_ws
        check _db = 13
        jyes skip_ws
        check _db = 9
        jyes skip_ws

output_char:
        arrange awk, =db char
        assemble awk
skip_ws:
        check i < DAT_END
        jyes more
        exit

decrease_depth:
        compute depth, depth - 1
indent_output:
        arrange awk, =db 10,depth =dup 9,char
        assemble awk
        jump skip_ws

increase_depth:
        compute depth, depth + 1
output_indent:
        arrange awk, =db char,10,depth =dup 9
        assemble awk
        jump skip_ws

string_contents:
        check instring = 3
        jyes string_esc
        check _db = '"'
        jyes string_inout
        check _db = '\'
        jno output_char
string_esc:
        compute instring, instring xor 2
        jump output_char
string_inout:
        compute instring, instring xor 1
        jump output_char
end calminstruction
json_prettier

else
display 'usage: fasmg -i "define FILE ''<filename>''" ',__FILE__,' <outfile>'
end if    
Next I'll probably depth prune it to get a better sense of the structure, and then I can begin coding a translator.

_________________
¯\(°_o)/¯ unlicense.org
Post 20 Jun 2022, 23:00
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 GitHub, YouTube, Twitter.

Website powered by rwasa.