assembler copy char[]

nobody not at possible.net
Fri Jun 8 00:17:21 PDT 2007


nobody Wrote:

> Daniel Keep Wrote:
> 
> > 
> > 
> > nobody wrote:
> > > Wow, that's exactly what i want.
> > > In dmd it's all ok, but the gdc  didn't like it: 
> > > 
> > > mov EDX, dword ptr [mystring+4];
> > > mov [st+4],EDX;
> > > 
> > > 
> > > gdmd  string.d
> > > /tmp/cc4h5AKo.s: Assembler messages:
> > > /tmp/cc4h5AKo.s:30: Error: junk `(%ebp)+4' after expression
> > > /tmp/cc4h5AKo.s:31: Error: junk `(%ebp)+4' after expression
> > 
> > IIRC, that's because local variables are actually EBP plus some offset.
> >  I think DMD is inlining the two offsets, but GDC isn't.
> > 
> > If you want to learn assembler, the best way is to just write code in D,
> > compile it, and then disassemble it.  I assume you're running under
> > Linux; gdb should have an option to disassemble the current function.
> > That way, you can read the original line of source code, and what the
> > compiler actually produces.  Here's what ddbg gives me for the program
> > 
> > void main()
> > {
> >     auto mystring = "Hello, World!";
> >     auto st = mystring;
> > }
> > 
> > Disassembly:
> > 
> > copy_string.d:2 void main()
> > 00402010: c8200000                enter 0x20, 0x0
> > 00402014: 53                      push ebx
> > 
> > copy_string.d:4     auto mystring = "Hello, World!";
> > 00402015: 8d45e0                  lea eax, [ebp-0x20]
> > 00402018: 50                      push eax
> > 00402019: 6a0d                    push 0xd
> > 0040201b: ff3594f04000            push dword [0x40f094]
> > 00402021: ff3590f04000            push dword [0x40f090]
> > 00402027: 6a01                    push 0x1
> > 00402029: e87e010000              call 0x4021ac __d_arraycopy
> > 
> > copy_string.d:5     auto st = mystring;
> > 0040202e: 8d4df0                  lea ecx, [ebp-0x10]
> > 00402031: 51                      push ecx
> > 00402032: 6a0d                    push 0xd
> > 00402034: 8d55e0                  lea edx, [ebp-0x20]
> > 00402037: bb0d000000              mov ebx, 0xd
> > 0040203c: 52                      push edx
> > 0040203d: 53                      push ebx
> > 0040203e: 6a01                    push 0x1
> > 00402040: e867010000              call 0x4021ac __d_arraycopy
> > 00402045: 31c0                    xor eax, eax
> > 00402047: 83c428                  add esp, 0x28
> > copy_string.obj
> > 0040204a: 5b                      pop ebx
> > 0040204b: c9                      leave
> > 0040204c: c3                      ret
> > 
> > 	-- Daniel
> 
> 
> With gdb
> 
> Dump of assembler code for function main:
> 0x08049a70 <main+0>:    lea    0x4(%esp),%ecx
> 0x08049a74 <main+4>:    and    $0xfffffff0,%esp
> 0x08049a77 <main+7>:    pushl  0xfffffffc(%ecx)
> 0x08049a7a <main+10>:   push   %ebp
> 0x08049a7b <main+11>:   mov    %esp,%ebp
> 0x08049a7d <main+13>:   push   %ecx
> 0x08049a7e <main+14>:   sub    $0x14,%esp
> 0x08049a81 <main+17>:   mov    (%ecx),%edx
> 0x08049a83 <main+19>:   mov    0x4(%ecx),%eax
> 0x08049a86 <main+22>:   mov    $0x8049154,%ecx
> 0x08049a8b <main+27>:   mov    %ecx,0x8(%esp)
> 0x08049a8f <main+31>:   mov    %edx,(%esp)
> 0x08049a92 <main+34>:   mov    %eax,0x4(%esp)
> 0x08049a96 <main+38>:   call   0x8049af0 <_d_run_main>
> 0x08049a9b <main+43>:   add    $0x14,%esp
> 0x08049a9e <main+46>:   pop    %ecx
> 0x08049a9f <main+47>:   pop    %ebp
> 
> Ok,  i try it tomorrow again, with ESP .
> I have thought that's only needed by naked asm blocks?


Here my new versions:
With ESP

import tango.io.Stdout;
void main()
{
        char[] mystring = "Hey Assembler";
        char[] str;

        asm
        {
                // Load Adress
                lea ECX,dword ptr [str]        ;

                // Copy length
                mov EDX, dword ptr [ESP]       ;
                mov [ECX],EDX                  ;

                // Copy ptr
                mov EDX, dword ptr [ESP+4]     ;
                mov [ECX+4],EDX                ;
        }
        Stdout("str ")(str).newline;
}

With lea:
 import tango.io.Stdout;
void main()
{
        char[] mystring = "Hey Assembler";
        char[] str;

        asm
        {
                // Load Adress
                lea ECX,dword ptr [str]        ;
                lea EBX,dword ptr [mystring]   ;

                // Copy length
                mov EDX, dword ptr [EBX]       ;
                mov [ECX],EDX                  ;

                // Copy ptr
                mov EDX, dword ptr [EBX+4]     ;
                mov [ECX+4],EDX                ;
        }
        Stdout("str ")(str).newline;
}



 



More information about the Digitalmars-d-learn mailing list