INCLUDE Irvine32.inc
.data
array DWORD 10,20,30,40,50
sum DWORD 0
.code
main PROC
mov EDI,offset array
mov ECX,5
mov EAX,0
HERE:
add eax,[edi]
add edi,TYPE array
dec ecx
jnz HERE
mov sum,eax
exit
main ENDP
END main
What does TYPE mean inside the loop line: edi, TYPE array? Is it the TYPE of variable we are using in the array that it replaces?
TL;DR
TYPE
return the size, in bytes, of the "representing" type of a data structure (such an array).SIZEOF
return the size, in bytes, of a data structure.LENGTHOF
returns the number of "elements" in a data structure.Note that these operators can be applied either to types (e.g.
BYTE
) or to labels (e.g.TYPE myLabel
).The result can be different for same data structures (e.g. records).
Precise meaning is given made below.
Note that there is similarly named operator
.TYPE
that can be used in macros to return a byte bitfield containing information about an expression (e.g. it names a register).This is what is presented by the poorly written MASM documentation in the MSDN.
There is a MASM 6.1 reference document here, I don't know how much authoritative it is but all of this answer is based on it.
Primitives
That document lists
TYPE
as an operator similar toSIZEOF
Here the operators are applied to a type, I believe it's possible to apply them to labels associated with primitives, resulting in the same output.
Arrays
For an array the difference between
SIZEOF
andTYPE
(andLENGTHOF
) becomes clear:In this case, the operators are used with labels.
Strings
For String, it suffices to recall that Strings are arrays of bytes.
Structures
For Structures, the concept is similar to the arrays':
TYPE
is size of the structure whileSIZEOF
is the size of the all the structure objects associated with the label (MASM consider a thing likemyLabel db 1, 2, 3
as being three bytes associated withmyLabel
):In this case, the operators are used with labels.
Unions
For Unions, something very similar happens:
In this case, the operators are used with labels.
Records
Records are bytes, words, or doublewords in which the individual bits or groups of bits are considered fields. Quoted from the manual, but not formatted as a quote intentionally.
Here there is a little asymmetry when using the operators with labels and with types.
.TYPE
as an operator for macrosThis operator is the old version of
OPATTR
and returns a byte with the following content:This has a taste of metaprogramming and it is normally used to optimise the code generated with macros.