Mixin in Inline Assembly

Era Scarecrow via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 22 20:33:38 PDT 2017


On Wednesday, 11 January 2017 at 17:32:35 UTC, Era Scarecrow 
wrote:
>  Still I think I'll impliment my own version and then if it's 
> faster I'll submit it.


Decided I'd give my hand at writing a 'ScaledInt' which is 
intended to basically allow any larger unsigned type. Coming 
across some assembly confusion.

Using mixin with assembly here's the 'result' of the mixin (as a 
final result)

alias UCent = ScaledInt!(uint, 4);

struct ScaledInt(I, int Size)
if (isUnsigned!(I) && Size > 1) {
     I[Size] val;

     ScaledInt opBinary(string op)(const ScaledInt rhs) const
     if (op == "+") {
         ScaledInt t;
         asm pure nothrow { //mixin generated from another 
function, for simplicity
             mov EBX, this;
             clc;
             mov EAX, rhs[EBP+0];
             adc EAX, val[EBX+0];
             mov t[EBP+0], EAX;
             mov EAX, rhs[EBP+4];
             adc EAX, val[EBX+4];
             mov t[EBP+4], EAX;
             mov EAX, rhs[EBP+8];
             adc EAX, val[EBX+8];
             mov t[EBP+8], EAX;
             mov EAX, rhs[EBP+12];
             adc EAX, val[EBX+12];
             mov t[EBP+12], EAX;
         }

         return t;
     }
}



Raw disassembly for my asm code shows this:
     mov     EBX,-4[EBP]
     clc
     mov     EAX,0Ch[EBP]
     adc     EAX,[EBX]
     mov     -014h[EBP],EAX
     mov     EAX,010h[EBP]
     adc     EAX,4[EBX]
     mov     -010h[EBP],EAX
     mov     EAX,014h[EBP]
     adc     EAX,8[EBX]
     mov     -0Ch[EBP],EAX
     mov     EAX,018h[EBP]
     adc     EAX,0Ch[EBX]
     mov     -8[EBP],EAX


 From what I'm seeing, it should be 8, 0ch, 10h, then 14h, all 
positive. I'm really scratching my head why I'm having this 
issue... Doing an add of t[0] = val[0] + rhs[0]; i get this 
disassembly:

     mov     EDX,-4[EBP] //mov EDX, this;
     mov     EBX,[EDX]   //val[0]
     add     EBX,0Ch[EBP]//+ rhs.val[0]
     mov     ECX,8[EBP]  //mov ECX, ???[???]
     mov     [ECX],EBX   //t.val[0] =

If i do "mov ECX,t[EBP]", i get "mov ECX,-014h[EBP]". If i try to 
reference the exact variable val within t, it complains it 
doesn't know it at compiler-time (although it's a fixed location).

What am i missing here?


More information about the Digitalmars-d-learn mailing list