Thread overview
Inline assembler for Dummies
Dec 02, 2007
Mike
Dec 02, 2007
Mike
Dec 02, 2007
novice2
December 02, 2007
From COM to assembler ... I've got a lot of questions today ... need assembler but my knowledge is somehow limited. That means I've got a lot of nice access violations :)

Anyway: what's the difference between EAX and [EAX]? I suspect it's value vs. pointer dereferencing, but I'm not sure. And: how do you get the address of a variable in assembler anyway? I tried "&var", but that doesn't compile (obvisously), "[var]" crashes.

-Mike
December 02, 2007
"Mike" <vertex@gmx.at> wrote in message news:fiut5k$8c8$1@digitalmars.com...
> From COM to assembler ... I've got a lot of questions today ... need assembler but my knowledge is somehow limited. That means I've got a lot of nice access violations :)
>
> Anyway: what's the difference between EAX and [EAX]? I suspect it's value vs. pointer dereferencing, but I'm not sure.

Right.  EAX gets you the value in EAX, [EAX] gets you the value in memory at the address held in EAX.

>And: how do you get the address of a variable in assembler anyway? I tried "&var", but that doesn't compile (obvisously), "[var]" crashes.

Use lea with just the name of the var:

int x = 5;

asm
{
    lea EAX, x;
    mov [EAX], 3; // save 3 to the memory location in EAX
}

writefln(x);

This prints 3.


December 02, 2007
> Anyway: what's the difference between EAX and [EAX]?
EAX="opearnd is EAX register itself"
[EAX]="operand is memory pointed by address in EAX"

>And: how do you get the address of a variable in assembler anyway?
lea register, variable

small eample:

#void main()
#{
#  uint var1;
#  uint var2;
#
#  asm
#  {
#    mov  var1, 13;  //var1 = 13
#  }
#  printf("var1 now contain: %d\n", var1);
#
#  asm
#  {
#    lea  EAX, var1;  //EAX = address of var1
#    mov  var2, EAX;  //var2 = var1
#  }
#  printf("var2 now contain address of var1: 0x%p\n", var2);
#}

December 02, 2007
Thanks to you both. I did that:

long var;
long *pvar = &var;

asm
{
mov EAX, pvar
push EAX
}

for now to find out if at least the concept works :)

So ... the code pushes the pointer to var on the stack and later calls a function which fills var with the (correct) value. Works. Interestingly "mov EAX, [pvar]" does the same thing, although it shouldn't ... it should - as I understand it - push the content of var (which is 0) to the stack, the function should then try to write to address 0 and fail with an access violation. Or shouldn't it? Does [] only dereference registers, not values?

Jarrett: go work on MiniD, you're just answering my questions all day long, you've got better things to do :)

-Mike

Jarrett Billingsley Wrote:

> "Mike" <vertex@gmx.at> wrote in message news:fiut5k$8c8$1@digitalmars.com...
> > From COM to assembler ... I've got a lot of questions today ... need assembler but my knowledge is somehow limited. That means I've got a lot of nice access violations :)
> >
> > Anyway: what's the difference between EAX and [EAX]? I suspect it's value vs. pointer dereferencing, but I'm not sure.
> 
> Right.  EAX gets you the value in EAX, [EAX] gets you the value in memory at the address held in EAX.
> 
> >And: how do you get the address of a variable in assembler anyway? I tried "&var", but that doesn't compile (obvisously), "[var]" crashes.
> 
> Use lea with just the name of the var:
> 
> int x = 5;
> 
> asm
> {
>     lea EAX, x;
>     mov [EAX], 3; // save 3 to the memory location in EAX
> }
> 
> writefln(x);
> 
> This prints 3.
> 
>