News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

stupid dereferencing ...

Started by James Ladd, March 25, 2005, 12:08:24 AM

Previous topic - Next topic

James Ladd

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

James Ladd

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...

roticv

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

sluggy

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.


James Ladd

Thanks for the comment sluggy.

rea

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????

tenkey

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

A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

hutch--

Don't mind, its a form of adiction.


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

Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

James Ladd

ta.
BTW - Tenkey, thats not a quote of mine. I didnt write that. Although I did suggest it.