News:

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

PROC - Parameters handling

Started by cobold, November 04, 2007, 08:16:20 PM

Previous topic - Next topic

cobold

Hello,

I defined a procedure and two arrays of 6 DWORDs each:
drucke_ziehung      proto :dword
.data
; -------------------------------------------------------------------------
Ziehung     dd   Anzahl dup (0)         ; Vom PC erzeugt
SechserMi   dd  21,26,28,36,39,40       ; Sechser vom Mittwoch

and want to print them on the console with either
invoke drucke_ziehung, ADDR Ziehung
or
invoke drucke_ziehung, ADDR SechserMi

When drucke_ziehung looks like this, it works:
drucke_ziehung proc Zi:dword
    xor ebx, ebx
[b]   mov esi, Zi[/b]
    .repeat
[b]       mov eax, [esi+ebx*4][/b]
        ;mov eax, [Zi+ebx*4] fails
        print str$(eax)
        inc ebx
        .if ebx < Anzahl
            print ", "
        .endif
    .until ebx == Anzahl
    print chr$(CRLF)
    ret
drucke_ziehung endp


Why do I have to use ESI instead of the parameter Zi ??
Using mov eax, [Zi+ebx*4] obviously references to some wrong address.

Can someone explain me why? Many thanks in advance.

rgds
cobold

BogdanOntanu

Hi,

Because in a PROC first argument (Zi in your case) is in fact:[ebp + 8], seccond argument is: [ebp + 0Ch]
First local is in fact: [ebp - 4], seccond local is: [ebp - 8]
And so on...

Hence:

mov eax, [Zi+ebx*4]

would be in fact:

mov eax, [ebp + 8 + 4*ebx]


First it all depends IF and how the assembler will encode this.
But more important is that conceptually this is wrong, and probably NOT what you actually have in mind.

In fact if "Zi" argument contains a pointer to an area you want to index into by ebx*4 then you must firsts establish a base pointer to that area and then index into it.

Hence

mov esi,Zi

is in fact

mov esi,[ebp+8]


And by doing this you first retrieve your pointer to data area argument and from now on you can index based on it with expressions like [esi + 4*ebx + 1234h].

Otherwise you do index into the procedure's arguments or local area not into your data area given as parameter to the procedure ;)

In part this confusion comes from the fact that "mov esi,Zi" is the same as "mov esi,[Zi]" in MASM.
the reason for this identity is that you do not have to type [...] a lot.


Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

cobold

Thanks a lot, Bogdan!

I knew it had something to do with the stack!
Thanks to your explanation I understand now that I referenced the stack-address of the parameter.




BogdanOntanu

Yes, you understand correctly.

Sometimes accessing the stack address of the parameters is useful. For example when the PROC has an variable / unknown number of parameters and you want to index into each parameter.
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro