Machine language programs: Determining the length of code

This article discusses how to determine the length of machine language programs.
In the case of a short machine language program, you just count the bytes to determine the length of code. This method is not practical for longer programs, though; for such programs, hexadecimal arithmetic is more useful. If you're familiar with hex digits (A = 10, B = 11, C = 12, D = 13, E = 14, F = 15), you can subtract hex locations, just like decimal numbers.

To discover the length of a longer machine language program, simply add 1 to the last location used by the machine language program, and then subtract from that the first location used by the program. For example, an ending address of $0357 and a starting address of $0300 would yield the calculation ($0357 + $0001) - $0300, or a length of $58:

last + 1             0  3  5  8
first              - 0  3  0  0
length               0  0  5  8

Likewise, the length of a program with an ending address of $03AC and a starting address of $031B is $92:

last + 1             0  3  A  D
first              - 0  3  1  B
length               0  0  9  2

In each case above, the answer is the length of the machine language program, and you may use it in a BSAVE command:

BSAVE PROGRAM, A$0300, L$58

You'll find more extensive explanations of hexadecimal arithmetic in most introductory machine language books. Check with a local book store or Apple dealer to help you locate one.
Published Date: Feb 19, 2012