Reputation: 118
I have a difficult exam (for me :D ) and can't find the signification of '$' character. As an example, I have the next code:
DATA SEGMENT
vector db 00h,10h,20h,30h,40h
db 50h,60h,70h,80h,90h
lv equ ($ - vector)/TYPE vector
Can someone tell me which is the value of lv?
Upvotes: 1
Views: 67
Reputation:
I am pretty sure the last part:
lv equ ($ - vector)/TYPE vector
Should get you the number of elements in the vector array. The entire code is roughly equivalent to the following in C:
int vector[10] = {0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90};
int lv = sizeof(vector) / sizeof(vector[0]);
Explanation: in MASM, $
denotes the current memory offset. I.e. you've stuffed the array vector
into memory, and the memory offset will be right where it ends. Thus, $ - vector
will subtract the pointer to the array from the current offset, effectively giving you the array's size.
In MASM,
The TYPE operator returns the size (in bytes) of each element in an array.
(quote from http://www.c-jump.com/CIS77/ASM/Instructions/lecture.html - seems like a good MASM doc, by the way). I.e. it's equivalent to C's sizeof(vector[0]);
.
Upvotes: 2
Reputation: 700152
The $
contains the address where the current instruction will be.
The value of lv
will be the number of items in the vector
data. The expression ($ - vector)
calculates the number of bytes from the vector
label to the place in the code where the $
is used, i.e. where lv
is declared.
The length is divided by the size of the data used in vector
. If you had ten words instead of bytes, then lv
would still be 10, as the 20 bytes that it occupies would be divided by 2.
Upvotes: 4