Difference between revisions of "General Coding Tricks"

From SizeCoding
Jump to: navigation, search
Line 1: Line 1:
== Data is code, code is data ==
+
=== Data is code, code is data ===
  
 
Code is nothing more than data that the CPU interprets.  For example, consider this multi-byte instruction:
 
Code is nothing more than data that the CPU interprets.  For example, consider this multi-byte instruction:

Revision as of 00:40, 7 August 2016

Data is code, code is data

Code is nothing more than data that the CPU interprets. For example, consider this multi-byte instruction:

        mov ah,37h

This assembles to B4 37. B4 by itself isn't interesting, but 37 is the opcode for AAS. Let's say you had this code before a loop, and you needed to perform AAS at the top of a loop. Rather than put AAS at the top of the loop, you can reuse the opcode that will already be there as part of the mov ah,37 that comes before it. Just jump directly into the middle of the mov ah,37h, which will get interpreted and executed as AAS:

label:
        mov ah,37h
        ;misc. stuff
        loop label+1

The +1 specifies the jump should go to 1 byte past the actual location.

If your environment holds you back, change it

The default MCGA palette is fairly horrible, but can be size advantages to changing it: While setting a new palette costs bytes, the new palette arrangement could save you headaches down the road. For example, if your code is calculating pixel colors that fall into goofy ranges, rather than constantly adjust the colors to sane ranges (ie. aligned to powers of 2), just set the palette so that values falling into those ranges look the way you want. (This assumes you have very small ways of redefining the palette, of course.)

The above is maybe not the best example. Rewrites welcome.

Need a constant?

If you need a constant value but you're out of space, search your assembled code for a byte value you can use.