The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: James Ladd on March 25, 2005, 12:08:24 AM

Title: stupid dereferencing ...
Post by: James Ladd on March 25, 2005, 12:08:24 AM
I have an array of handles like this:

threads HANDLE 32 dup(0)

and I have a routine I pass the address of this array to (invoke thingo, addr threads) :

thingo proc threads:PHANDLE
    ; put item into array.
    ; item is in eax.
    mov [threads], eax
    ret
thingo endp

this compiles with masm without error.
I am assuming that the first element of the array is now the value in eax and the rest of the array if zero(s).
I have a routine that needs to iterate through the array:

thingo2 proc threads:PHANDLE
    myloop:
        mov eax, [threads]
        .if eax == 0
            jmp endLoop
        .endif

        ; do something with array element value in eax

        ; move to next element.
        mov edx, threads
        add eax, 2
        mov threads, eax
        jmp myloop
    endloop:
    ret
thingo2 endp

The questions I have are:
1. Is the way I am putting values into the array correct?
2. Is the way I am looping through the array and detecting the the array element zero correct?
I ask these questions because I seen to be in an endless loop.
rgs, striker
Title: Re: stupid dereferencing ...
Post by: James Ladd on March 25, 2005, 12:18:43 AM
ok, I fixed this, here are my changes ...

thingo proc threads:PHANDLE
    ; put item into array.
    ; item is in eax.
    mov edx, threads
    mov [edx], eax
    ret
thingo endp

and

thingo2 proc threads:PHANDLE
    myloop:
        mov eax, threads
        mov eax, [eax]
        .if eax == 0
            jmp endLoop
        .endif

        ; do something with array element value in eax

        ; move to next element.
        mov edx, threads
        add eax, sizeof PHANDLE
        mov threads, eax
        jmp myloop
    endloop:
    ret
thingo2 endp


One thing I find strange is I cant do this

    mov edx, threads
    mov [edx], 0

but I can do it if I put zero into an register then move it.
eg:

    mov edx, threads
    mov eax, 0
    mov [edx], eax

oh well...
Title: Re: stupid dereferencing ...
Post by: roticv on March 25, 2005, 02:20:28 AM
Quote from: striker on March 25, 2005, 12:18:43 AM
One thing I find strange is I cant do this

    mov edx, threads
    mov [edx], 0

but I can do it if I put zero into an register then move it.
eg:

    mov edx, threads
    mov eax, 0
    mov [edx], eax

oh well...

You have to tell the compiler you want to move 0 to the dword at [edx]. Therefore it should be


    mov edx, threads
    mov dword ptr[edx], 0
Title: Re: stupid dereferencing ...
Post by: sluggy on March 25, 2005, 07:54:50 AM
You are doing two things "wrong" (depending on your point of view):

- you are moving values in/out of registers needlessly. Example: you don't need to move the threads pointer into eax before adding  sizeof PHANDLE. sizeof is evaluated to a constant (immediate value) at compile time, you can add an immediate to a value in a variable (memory location).

- IMO, you are using a dodgy mechanism for determining when you have reached the end of the array. You should pass in another parameter which is the size (length) of the array, it is a lot safer.

Title: Re: stupid dereferencing ...
Post by: James Ladd on March 25, 2005, 11:11:43 PM
Thanks for the comment sluggy.
Title: Re: stupid dereferencing ...
Post by: rea on March 25, 2005, 11:52:51 PM
Also remember than the size of the array is difeernt than the number of handles elemenst saved ;), you can make function for have a structure that hold the number of items, the last item and a pointer to the items, insert, delete, find are good functions to have... if you will be inserting elements, how they are handled FIFO LIFO??? dosent matter only insert and delete is suficient in no special order????
Title: Re: stupid dereferencing ...
Post by: tenkey on March 28, 2005, 10:27:52 AM
Quote from: striker on March 25, 2005, 12:18:43 AM

thingo2 proc threads:PHANDLE
    myloop:
        mov eax, threads
        mov eax, [eax]
        .if eax == 0
            jmp endLoop
        .endif

        ; do something with array element value in eax

        ; move to next element.
        mov edx, threads         ; ----- load pointer parameter
        add eax, sizeof PHANDLE  ; ----- adding size to array element ?
        mov threads, eax         ; ----- update copied parameter (it's not the caller's pointer)
        jmp myloop
    endloop:
    ret
thingo2 endp

Title: Re: stupid dereferencing ...
Post by: hutch-- on March 28, 2005, 11:11:43 AM
Don't mind, its a form of adiction.


myloop:
        mov eax, threads
        cmp DWORD PTR [eax], 0
        je endloop

Title: Re: stupid dereferencing ...
Post by: James Ladd on March 29, 2005, 01:00:26 AM
ta.
BTW - Tenkey, thats not a quote of mine. I didnt write that. Although I did suggest it.