Multibyte constant: Difference between revisions

From NESdev Wiki
Jump to navigationJump to search
(0xBEE52 is hex. 0xACTION52 is not unless it equals 0xFECE5.)
 
(→‎External links: Game Boy counterpart)
 
(8 intermediate revisions by 4 users not shown)
Line 1: Line 1:
This macro allows including long hexadecimal constants on one line, like an arbitrary extension of the .dbyt (2 byte big endian constant).
This macro allows including long hexadecimal constants on one line, like an arbitrary extension of the .dbyt (2 byte big endian constant).
<source>
 
== asm6 ==
<pre>
; ASM6 uses HEX, Use a Macro to convert it to use MBYT!
; Macro by Hamtaro126, under no licence.
macro mbyt string
hex string
endm
</pre>
 
== ca65 ==
<pre>
; mbyt.s
; mbyt.s
; Multibyte constant macro for ca65
; Multibyte constant macro for ca65
Line 28: Line 39:
   .endif
   .endif
   .byte highdig * $10 + lowdig
   .byte highdig * $10 + lowdig
.endmacro
   ;.out .sprintf(".byte %02x", highdig * $10 + lowdig)
 
.macro mbyt_rgn inbytes, ifrom, ito
  ; Subdivide region into two, for O(log n) nested macros
   ; to make it more "5ca1ab1e"
  .if (ito - ifrom) > 2
    .local imid
    imid = (ifrom + (ito - ifrom) / 4 * 2)
    mbyt_rgn inbytes, ifrom, imid
    mbyt_rgn inbytes, imid, ito
  .elseif ito > ifrom
    mbyt_hex2nibs {.strat(inbytes, ifrom)}, {.strat(inbytes, ifrom + 1)}
  .endif
.endmacro
.endmacro


.macro mbyt inbytes
.macro mbyt inbytes
   .local len
  ; thanks to thefox who recommended .set
   len = .strlen(inbytes)
   .local pos
  mbyt_rgn inbytes, 0, len
  pos .set 0
  .out .sprintf("%s", inbytes)
   .repeat .strlen(inbytes)
    .if pos < .strlen(inbytes)
      nib .set .strat(inbytes, pos)
      ; these characters can be used as separators
      .if (nib = ' ' || nib = ',' || nib = '$' || nib = '_')
        pos .set pos + 1
      .else
        mbyt_hex2nibs nib, {.strat(inbytes, pos + 1)}
        pos .set pos + 2
      .endif
    .endif
  .endrepeat
.endmacro
.endmacro


; use it like this:
; use it like this:
; mbyt "ba5eba11"
; mbyt "09F91102 9D74E35B D84156C5 635688C0"
</source>
</pre>
 
Movax12 has a [http://pastebin.com/jiWdS68E another implementation].
 
== Unofficial MagicKit ==
<pre>
; Implementation of multibyte constants for Unofficial-MagicKit.
; By zzo38, Creative Commons Zero
 
macro mbyt
macset 2,4,\$1>1
macgoto mbyt_\2
endm
 
macro mbyt_0
endm
 
macro mbyt_1
macset 2,2,1
macset 2,5,2
db $\2
mbyt_0 \>1
macset 1,1,1
mbyt_0 \>1
macset 1,1,1
macgoto mbyt
endm
 
; Example:
; mbyt 09F91102 9D74E35B D84156C5 635688C0
</pre>


== External links ==
== External links ==
* [http://nedbatchelder.com/text/hexwords.html Ned Batchelder: Hex words] has test cases
* [http://nedbatchelder.com/text/hexwords.html Ned Batchelder: Hex words] has test cases
* [http://pastebin.com/0xkpKgCh Alternate implementation by thefox]
* [https://gbdev.gg8.se/wiki/articles/Multibyte_constant Multibyte constant] on GbdevWiki

Latest revision as of 23:13, 26 July 2020

This macro allows including long hexadecimal constants on one line, like an arbitrary extension of the .dbyt (2 byte big endian constant).

asm6

; ASM6 uses HEX, Use a Macro to convert it to use MBYT!
; Macro by Hamtaro126, under no licence.
 macro mbyt string
 hex string
 endm

ca65

; mbyt.s
; Multibyte constant macro for ca65
;
; Copyright 2013 Damian Yerrick
; Copying and distribution of this file, with or without
; modification, are permitted in any medium without royalty provided
; the copyright notice and this notice are preserved in any source
; code copies.  This file is offered as-is, without any warranty.
;
.macro mbyt_hex2nibs highnib, lownib
.local highdig, lowdig
  ; "dec0de" the hex nibbles
  .if highnib >= 'A' && highnib <= 'F'
    highdig = highnib - 'A' + 10
  .elseif highnib >= 'a' && highnib <= 'f'
    highdig = highnib - 'a' + 10
  .elseif highnib >= '0' && highnib <= '9'
    highdig = highnib - '0'
  .endif
  .if lownib >= 'A' && lownib <= 'F'
    lowdig = lownib - 'A' + 10
  .elseif lownib >= 'a' && lownib <= 'f'
    lowdig = lownib - 'a' + 10
  .elseif lownib >= '0' && lownib <= '9'
    lowdig = lownib - '0'
  .endif
  .byte highdig * $10 + lowdig
  ;.out .sprintf(".byte %02x", highdig * $10 + lowdig)
.endmacro

.macro mbyt inbytes
  ; thanks to thefox who recommended .set
  .local pos
  pos .set 0
  .out .sprintf("%s", inbytes)
  .repeat .strlen(inbytes)
    .if pos < .strlen(inbytes)
      nib .set .strat(inbytes, pos)
      ; these characters can be used as separators
      .if (nib = ' ' || nib = ',' || nib = '$' || nib = '_')
        pos .set pos + 1
      .else
        mbyt_hex2nibs nib, {.strat(inbytes, pos + 1)}
        pos .set pos + 2
      .endif
    .endif
  .endrepeat
.endmacro

; use it like this:
; mbyt "09F91102 9D74E35B D84156C5 635688C0"

Movax12 has a another implementation.

Unofficial MagicKit

; Implementation of multibyte constants for Unofficial-MagicKit.
; By zzo38, Creative Commons Zero

	macro mbyt
	macset 2,4,\$1>1
	macgoto mbyt_\2
	endm

	macro mbyt_0
	endm

	macro mbyt_1
	macset 2,2,1
	macset 2,5,2
	db $\2
	mbyt_0 \>1
	macset 1,1,1
	mbyt_0 \>1
	macset 1,1,1
	macgoto mbyt
	endm

; Example:
;	mbyt 09F91102 9D74E35B D84156C5 635688C0

External links