Array length: Difference between revisions

m
Line 50:
 
=={{header|68000 Assembly}}==
Array length is computed at compilation time with the formula: (Array_End-Array). The "bit width" of this value is the bit width of the result, not the bit width of the inputs. In other words, even though code labels are 32-bit memory addresses, if their difference is 8 or 16-bit you can use a <code>.B</code> or <code>.W</code> instruction or directive on them without a compile-time error.
{{trans|360 Assembly}}
 
Array length is computed at compilation time with the formula: (Array_End-Array). Even though the labels Array and Array_End are both 32-bit memory addresses, if their difference is small enough it can fit into a 16-bit or even an 8-bit instruction operand.
The biggest limitation to the (Array_End-Array) method is that it always measures in total bytes. So if your array elements are of a different type you'll have to adjust accordingly. For an array strings, you should instead construct an array of pointers to strings and divide the result of the difference by 4 to get the total number of "strings" in the array.
 
 
<lang 68000devpac>start:
MOVE.B #(Array_EndMyArray_End-ArrayMyArray)/4 ;evaluates to 142
RTS
 
Apple:
Array:
DC.B "apple",0
even
Orange:
DC.B "orange",0
even
 
Array_End:</lang>
MyArray:
DC.L Apple
DC.L Orange
Array_EndMyArray_End:</lang>
 
=={{header|8th}}==
1,489

edits