Multibyte constant

From GbdevWiki
Jump to: navigation, search

A convenient way to declare a hexadecimal literal spanning multiple bytes

RGBDS

;;
; Multibyte constant declaration macro
; by ISSOtm, 2020-07-26, license: CC0
; Example:
;   hexdata 0123, 45, 6789ABCDEF
; produces this:
;   db $01,$23,$45,$67,$89,$AB,$CD,$EF
hexdata: macro
ARG_INDEX = 1
    REPT _NARG
        IF STRLEN("\1") % 2 == 1
            FAIL "Length of argument #{d:ARG_INDEX} is not even!"
        ENDC

I = 1
        REPT STRLEN("\1") / 2
BYTE equs STRCAT("$", STRSUB("\1", I, 2))
            db BYTE
            PURGE BYTE
I = I + 2
        ENDR

        PURGE I
        SHIFT
ARG_INDEX = ARG_INDEX + 1
    ENDR
endm

External links