In assembly, say I have an array of however many elements each with a different character stored in it. At the end of the program I would like to clear all the elements of the array so that I have an empty array. How can this be done? Thanks.
Posted on 2009-10-19 00:39:16 by dre
Since you are using assembly, and basic arrays are just chunks of continuous memory, you can simply find the base address and length of the array, and use that information to execute a clearing loop.


;quick n' dirty array clear
xor eax,eax ;Write Zero
mov ecx,ARRAY_SIZE ;Size of the Array, in Bytes
mov edi,ARRAY_ADDR ;Location of Array start, in RAM
cld
rep stosb


If you know the size of your array, you can optimize the clearing loop to work on larger data boundaries, e.g. DWORDS (stosd) instead of BYTES (stosb).

If you are working with multi-dimensional arrays or linked lists, you'll have to take an entirely different approach.
Posted on 2009-10-19 00:56:40 by SpooK
Thanks. How do I find the address of my array?
Posted on 2009-10-19 01:06:23 by dre

Thanks. How do I find the address of my array?


The address of your array is the offset of your array.

Andy

.data
array        db          10 dup(?)

.code

mov          bx,offset array
Posted on 2009-10-19 07:56:23 by skywalker
oooh bad habits

LEA
Posted on 2009-10-19 08:07:35 by Homer

oooh bad habits

LEA


Yes, in MASM you may wish to explicitly use LEA to avoid introducing a bug due to how MASM handles offsets vs values.

If you work with 64-bit register operations, using LEA can also shave a byte off of the instruction sequence.
Posted on 2009-10-19 11:34:07 by SpooK

oooh bad habits

LEA
Are you saying that using "offset" instead of LEA is bad habit? For global data?
Posted on 2009-10-20 05:50:37 by f0dder
I'm saying that lazy coding habits lead to problems which could have been avoided.
Sure, in this simple example, offset works just fine.
Learning to avoid hardcoded addresses/offsets is a good habit, and will always work, in all cases.
It will also eliminate the upcoming question of "how come offset won't work for local arrays".

Posted on 2009-10-20 07:28:42 by Homer