Isaac Maldonado
Isaac Maldonado

Reputation: 1

Using TYPE instruction assembly MASM x86

I'm still learning assembly and I got confused when I reach this portion of a code:

add SI, TYPE word

Since I don't quite get what the TYPE instruction stands for there, what exacly are we adding to SI? If someone can ilustrate me how this works assuming SI set 0 before reaching add I will be very grateful!

Upvotes: 0

Views: 602

Answers (2)

Peter Cordes
Peter Cordes

Reputation: 365277

You're adding an assemble-time-constant integer 2. It assembles to add si,2.
MASM TYPE foo is the same idea as C sizeof(typeof(foo)).1

Remember in asm you have to scale from element index to byte offset yourself, e.g. increment a pointer by 2 bytes to go to the next word, where in C you'd just use p++ to increment a short *p.

MASM also has a sizeof which can give you the size of a whole array, instead of just one element.


Normally you'd use add si, type some_array_name so your increment code could automatically change if you change the array to dw vs. db.

Although in that case it's somewhat pointless if you also use AX instead of AL in other instructions that actually load or store from/to [SI].

You could use type foo as part of some other expression to calculate a size or loop bound, though.


Footnote 1: C/C++ doesn't actually have a typeof keyword; that's a GNU C extension. But the name is more self explanatory than the standard C++11 keyword decltype which does the same thing.

Upvotes: 0

Michael
Michael

Reputation: 58487

From the MASM 6.1 Programmer's Guide:

The SIZEOF and TYPE operators, when applied to a type, return the size of an integer of that type. The size attribute associated with each data type is:

Data Type      Bytes
--------------------
BYTE, SBYTE    1
WORD, SWORD    2
DWORD, SDWORD  4
FWORD          6
QWORD          8
TBYTE          10

Side note: For arrays and strings, SIZEOF and TYPE are not equivalent. SIZEOF will give you the total size of the array/string in bytes, whereas TYPE will give you the size of a single array/string element.

Upvotes: 2

Related Questions