A guide to making codes shorter.

Started by spunit262, November 11, 2008, 11:56:09 PM

Previous topic - Next topic

spunit262

I have an obsession with making all my code as short as possible, and it bugs me when I see codes that could easily be shorter. For more details check the Code Type Document. I'm not very good at explaining thing, but I'll try to at least introduce you to some of the obscure code types and the obscure features of the common ones.

Ifs
All if codes have a nifty feature that if you add 1 to the address they automatically include an end if before them.
Example:
200002224 00000022
040002224 00000044
E20000001 00000000
200002224 00000033
040002224 00000055
E00000000 80008000
Becomes
200002224 00000022
040002224 00000044
200002225 00000033
040002224 00000055
E00000000 80008000
If you have to end 2 or more ifs you still have to use E200000X 00000000 (X=number of ifs to end)

Serial Code
Serial Codes can be used to write a constant (or regularly increasing) value, to non-continuous but regularly spaced addresses.

8- and 16-bit fills
The standard 8 and 16 bit write codes also have a fill option. They are shorter than Serial Codes, but can only write a constant value (8- or 16-bit, no 32-bit) to continuous addresses.

String Code
If you modify several consecutive bytes that aren't repetitive, use a string code instead of several writes.
Example: Music Fixer
Menu My Music Reset [Phantom Wings & Full Metal Kirby]
4A000000 9017F304
14000000 000026FA
14000004 00002A32
14000008 000026FB
1400000C 00002A32
14000010 0000281C
14000014 00002A32
14000018 000026F9
1400001C 00002A32
E0000000 80008000
Becomes
4A000000 9017F304
16000000 00000020
000026FA 00002A32
000026FB 00002A32
0000281C 00002A32
000026F9 00002A32
E0000000 80008000

Loops (Repeats)
Loops can be used to perform the same complex action on different address (just one possibility)
Example:
Character/Costume Replacement (all) [spunit262, includes Y.S.'s color mod]
80000000 90180B48 gr0 = first address
80000001 90180BF8 gr1 = second address
60000003 00000000 set loop for 4 iteration
4A001000 00000000 po = gr0
38000002 00FFWW00 if character is WW
4A001001 00000000 po = gr1
38000004 FF0000XX if costume is XX
10000000 000000YY set character to YY
100003C5 000Q00ZZ set costume to ZZ
E0000000 80008000 end all ifs and reset ba and po
86000000 00000004 increment gr0 to next address
86000001 0000005C increment gr1 to next address
62000000 00000000 repeat

ASM vs. Regular codes
C0 codes are asm codes that are executed when the code handler comes to them. If you're using a C0 code (or D0, they're exactly the same as they don't use the ba or po) try writing it using regular codes. the one using regular codes might be shorter. I wouldn't recommend trying to make a C0 from regular codes though.

Don'ts

Don't leave off the E0000000 80008000 if you have modified the ba or po (and haven't reset them) or have unended ifs. Your code will work fine but you'll wreck the codes that come after yours.

Don't use
DE000000 80009380
Use: (copied form Code Type Document)
DE000000 80008180 : checks that po is in the [80000000,81800000[.
66000001 00000000 : skip next line if the code execution status is set to true.
DE000001 90009380 : endif, then checks that po is in [90000000,93800000[.
Not active but may still sporadically make codes.
(ooo)
``´´

hetoan2

moved to collective for easier access