flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > HEX.INC converted from fasmg to fasm 1

Author
Thread Post new topic Reply to topic
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 09 Dec 2017, 20:43
This is a direct conversion of the set of macros that allow to generate Intel HEX output format:
Code:
format binary as 'hex'

virtual at 0
        HEX.digits:: db '0123456789ABCDEF'
end virtual

macro HEX.byte value
{
        HEX.checksum = (HEX.checksum + (value)) and 0FFh
        local digit
        load digit byte from HEX.digits:(value) shr 4
        db digit
        load digit byte from HEX.digits:(value) and 0Fh
        db digit
}

macro HEX.line length,address,type,bytes_low,bytes_high
{
        HEX.checksum = 0
        db ':'
        HEX.byte length
        HEX.byte (address) shr 8
        HEX.byte (address) and 0FFh
        HEX.byte type
        HEX.data = bytes_low
        repeat length
                HEX.byte HEX.data and 0FFh
                if % = 8
                        HEX.data = bytes_high
                else
                        HEX.data = HEX.data shr 8
                end if
        end repeat
        HEX.data = (-HEX.checksum) and 0FFh
        HEX.byte HEX.data
        db 13,10
}

macro HEX.seg address:0
{
        virtual at address
}

macro HEX.endseg
{
        local code,address,size,bytes_low,bytes_high,tmp
        code:: address = $$
               size = $-$$
        end virtual
        while size
                if size > 16
                        load bytes_low qword from code:address
                        load bytes_high qword from code:address+8
                        HEX.line 10h,address,0,bytes_low,bytes_high
                        address = address + 16
                        size = size - 16
                else
                        if size > 8
                                load bytes_low qword from code:address
                                load bytes_high qword from code:address+size-8
                                bytes_high = bytes_high shr (8*(16-size))
                        else
                                bytes_high = 0
                                if size = 0
                                        bytes_low = 0
                                else if size = 1
                                        load bytes_low byte from code:address
                                else if size = 2
                                        load bytes_low word from code:address
                                else if size = 3
                                        load bytes_low word from code:address
                                        load tmp byte from code:address+2
                                        bytes_low = bytes_low + tmp shl 16
                                else if size = 8
                                        load bytes_low qword from code:address
                                else
                                        load bytes_low dword from code:address
                                        load tmp dword from code:address+size-4
                                        bytes_low = bytes_low or tmp shl (8*(size-4))
                                end if
                        end if
                        HEX.line size,address,0,bytes_low,bytes_high
                        break
                end if
        end while
}

macro org address
{
        if $ <> address
                HEX.endseg
                HEX.seg address
        end if
}

HEX.seg

postpone
{
        HEX.endseg
        HEX.line 0,0,1,0
}    
The changes were needed mainly to get around the limitation of 65 bits per value in fasm 1 engine. There is a third-party modification of fasm 1 that could allow to port these macros from fasmg in almost unmodified form (other than changing preprocessor's braces to "end" and adapting "load" directives to fasmg's syntax).
Post 09 Dec 2017, 20:43
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.