assembler copy char[]
Daniel Keep
daniel.keep.lists at gmail.com
Thu Jun 7 05:42:04 PDT 2007
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
More information about the Digitalmars-d-learn
mailing list