I need explanation about mov instruction

Hi,

why is 0 passed into esi if I use mov esi, vec; or [vec] instead of mov esi, vec1?

Code: (look at NormalizeVec)

class VectorSoA
{
public:
VectorSoA():test(0)
{
vec = (Vector*)_aligned_malloc(sizeof(Vector), 16);
vec[0] = Vector(1,2,3,0);
}

~VectorSoA()
{
_aligned_free(vec);
}

Vector* vec;

void NormalizeVec()
{
Vector *vec1 = vec;
__asm
{
mov esi, vec1
movaps xmm0, [esi]
movaps xmm1, xmm0
mulps xmm0, xmm0
haddps xmm0, xmm0
haddps xmm0, xmm0
rsqrtps xmm0, xmm0
mulps xmm1, xmm0
movaps [esi], xmm1
}
}

private:
float test;
};

greetz, thanks

Comments

  • : Hi,
    :
    : why is 0 passed into esi if I use mov esi, vec; or [vec] instead of
    : mov esi, vec1?
    :

    Consider the variables:
    [code]
    int a = 25;
    void* p = (void*)&a;
    [/code]
    Then, the assembly code translates as follows:
    [code]mov eax, a
    eax = &a; // = p
    [/code][code]
    mov eax, p
    eax = &p;
    [/code][code]
    mov eax, [a]
    eax = a; // = 25
    [/code][code]
    mov eax, [p]
    eax = p; // = &a[/code]

    Summary: the variable name is an assembly label, and the value at the label is the variable value ( ASM: [var] is C: var, and ASM: var is C: &var )

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • : : Hi,
    : :
    : : why is 0 passed into esi if I use mov esi, vec; or [vec] instead of
    : : mov esi, vec1?
    : :
    :
    : Consider the variables:
    : [code]:
    : int a = 25;
    : void* p = (void*)&a;
    : [/code]:
    : Then, the assembly code translates as follows:
    : [code]: mov eax, a
    : eax = &a; // = p
    : [/code]: [code]:
    : mov eax, p
    : eax = &p;
    : [/code]: [code]:
    : mov eax, [a]
    : eax = a; // = 25
    : [/code]: [code]:
    : mov eax, [p]
    : eax = p; // = &a[/code]:
    :
    : Summary: the variable name is an assembly label, and the value at
    : the label is the variable value ( ASM: [var] is C: var, and ASM: var
    : is C: &var )
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry
    Thank you Richard!

    So, is there a way to put address of vec into esi?

    Best Regards
  • : So, is there a way to put address of vec into esi?
    :

    Sure:
    [code]
    mov esi, vec
    [/code]

    Or, if you insist on using vec1:
    [code]
    mov esi, [vec1]
    [/code]

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • : : So, is there a way to put address of vec into esi?
    : :
    :
    : Sure:
    : [code]:
    : mov esi, vec
    : [/code]:
    :
    : Or, if you insist on using vec1:
    : [code]:
    : mov esi, [vec1]
    : [/code]:
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry

    Thanks Richard,

    but that is exactlly what I wrote first, and in both ways, 0 was passed into esi
    so I had to make temp vec1 (i guess compiler knows its a pointer that way?) and only then esi got address of vec :(

    So, when I wrote:
    [code]
    mov esi, vec
    [/code]
    esi got 0
    and also:
    [code]
    mov esi, [vec] ;esi = 0
    [/code]
    esi got 0

    Best Regards!
  • : Thanks Richard,
    :
    : but that is exactlly what I wrote first, and in both ways, 0 was
    : passed into esi
    : so I had to make temp vec1 (i guess compiler knows its a pointer
    : that way?) and only then esi got address of vec :(
    :

    Where/how did you check the address that gets loaded into ESI?
    If you used a compiler-generated ASM file, then you'd have to be worried if it wasn't 0! Basically, the compiler doesn't know where 'vec' will be stored in memory, so it just says: "MOV ESI, 0" and elsewhere it says "Once you know the address of VEC, change line xx into MOV ESI, {real address}"

    One other thing though: most calling conventions don't allow a function to modify ESI/EDI. So, don't forget to add this code:
    [code]
    push esi
    ... // Change/Use ESI here
    pop esi
    [/code]
    To make sure ESI remains unchanged (except for in the specific block between the push and pop)

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • : : Thanks Richard,
    : :
    : : but that is exactlly what I wrote first, and in both ways, 0 was
    : : passed into esi
    : : so I had to make temp vec1 (i guess compiler knows its a pointer
    : : that way?) and only then esi got address of vec :(
    : :
    :
    : Where/how did you check the address that gets loaded into ESI?
    : If you used a compiler-generated ASM file, then you'd have to be
    : worried if it wasn't 0! Basically, the compiler doesn't know where
    : 'vec' will be stored in memory, so it just says: "MOV ESI, 0" and
    : elsewhere it says "Once you know the address of VEC, change line xx
    : into MOV ESI, {real address}"
    :
    : One other thing though: most calling conventions don't allow a
    : function to modify ESI/EDI. So, don't forget to add this code:
    : [code]:
    : push esi
    : ... // Change/Use ESI here
    : pop esi
    : [/code]:
    : To make sure ESI remains unchanged (except for in the specific block
    : between the push and pop)
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry

    Thank you Richard! you helped me a lot.
    Best Regards

    "The complement of truth is clearness." - Niels Bohr
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories