Hey guys :D,
I swear i've been looking for the solution to this for like 4 hours so i hope I don't get any RTFMs hehe. Here goes...

I have a struct containing a char and a short int. The operators for this struct (+,-,/,*) are all going to be overloaded. So for x + y, i need to access the member variables of x in my inline asm and (the easier part) pass y as a parameter to my overloaded function(1 param). Easy enough? So far i've had success setting a register such as ecx to "this" and then incrementing it to access x's variables. However i didn't want to have the extra step of copying "this" since "this" is already an address that i should be able to increment right? However, if I try to copy the value at "this" directly i get operand size conflicts since i'm using 16-bit registers (adding shorts). If i copy "this" into a 32-bit register it pulls too much information out of the memory pointed to by "this" (the short plus whatever 16 bits come after). Here's the code:

//the struct
struct 24bit{

//what works:
  mov ecx, this;        // gotta copy "this" to ecx
  mov ax, ;    // x's short as pointed to by ecx(or the "this")
  add ax, ;   // y's short on the stack

//what doesn't work:
  mov eax, ;  // note the eax. doesn't compile otherwise. however it pulls too much data off of "this"
  add ax, ;   // y's short on the stack

I'm leaning towards using movl ax, in gcc... i have a feeling this one is going to be really obvious once someone explains it  :shock:. i was wondering that since all member functions have "this" as a parameter so it should also be on the stack, but that doesn't save me any steps right?

so to recap. i just want to add the shorts from x and y using the overloaded operator in as few instructions as possible.
Posted on 2008-04-12 04:30:35 by elokide
x86 can't do double indirection - you need to move the 'this' pointer to a register before you can use it. Also, mixing inline assembly and C++ this way is a bad idea - alignment, padding, potential vtbl, et cetera.

You should save assembly for where it can make a big difference, and then rather use external than inline assembly - more portable :P between compilers too.
Posted on 2008-04-12 12:18:35 by f0dder
the only reason i wanted to use asm for this was to catch for overflows since the data type is so small. seems like it'd take more comparisons to catch the overflow manually in c. i guess i'll extern this one :). thanks fodder!
Posted on 2008-04-12 15:36:18 by elokide