Let;s say I declare

arr db 1,2,3

How do I know arr's size afterwards? is there a command for it?
I know there is something about arr-$ or something that I can declare in the ds segment but I think it works for strings only...

Recommended Answers

All 5 Replies

declare another byte after the array. Get the address of arr and the address of that new byte, then subtract the two addresses.

That means, if I get an array I dunno its size (not stored in any variable and nothing to end it like null or $ or anything stated) I can;t find the size?
(In the code segment,like a command, like in C# there is 'sizeof').

I mean let's say I have something that is dup 100 (?), and I fill it in, like getting some numbers from the keyboard, does it mean I can;t know how many numbers I got by the vector itself? (let;s say I chose not to use a counter to count the times the user inserted another number..

I think MASM has a SIZEOF operator that works very much like C and C# sizeof. See this link

Yes, in MASM you can use the sizeof operator:

mov		eax, SIZEOF arr

OR after you define arr, you can get the size using the $ char (I forgot what it is called)

.data
arr db 1,2,3
ARR_SIZE		equ	$ - arr

.code
	mov		eax, ARR_SIZE

Wich is more versatile than the sizeof operator. You cannot use the sizeof operator on an array of DWORDS because it will return 4 no matter how many elements you have... so you would use the $ after the array like so:

.data
sz1				BYTE	"Hello", 0
sz2				BYTE	"There", 0
sz3				BYTE	"My Friend!", 0

SomeArray		DWORD	offset sz1
				DWORD	offset sz2
				DWORD	offset sz3
SOMEARRAY_SIZE	equ	($ - SomeArray) / SIZEOF DWORD ; or use 4

Now, no matter how big your dword array is, it will give you the number of items..

Comes in handy when you are looping to load a combobox with strings or something :-)

Thanks for the help :3

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.