string to char array?
Kyoji Klyden via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Jun 5 12:19:22 PDT 2015
On Friday, 5 June 2015 at 18:30:53 UTC, Kagamin wrote:
> Well, reading assembler is good enough:
>
> void f(int[] a)
> {
> a[0]=0;
> a[1]=1;
> a[2]=2;
> }
>
> Here pointer is passed in rsi register and length - in rdi:
>
> void f(int[]):
> push rax
> test rdi, rdi
> je .LBB0_4
> mov dword ptr [rsi], 0
> cmp rdi, 1
> jbe .LBB0_5
> mov dword ptr [rsi + 4], 1
> cmp rdi, 2
> jbe .LBB0_6
> mov dword ptr [rsi + 8], 2
> pop rax
> ret
> .LBB0_4:
> mov edi, 55
> mov esi, .L.str
> mov edx, 5
> call _d_arraybounds
> .LBB0_5:
> mov edi, 55
> mov esi, .L.str
> mov edx, 6
> call _d_arraybounds
> .LBB0_6:
> mov edi, 55
> mov esi, .L.str
> mov edx, 7
> call _d_arraybounds
>
> You play with assembler generated for D code at
> http://ldc.acomirei.ru/
Never said I was good at asm but I'll give it a shot...
So push rax to the top of the memory stack, test if rdi == rdi
since yes jump to.LBB0_4, in LBB0_4 move the value 55 into edi,
then move .L.str (whatever that is) into esi, then 5 into edx,
then call _d_arraybounds (something from Druntime maybe?) then
LBB0_4 has nothing left so go back, move the value 0 into a
32-bit pointer(to rsi register), if rdi == 1 jump to LBB0_5
(pretty much the same as LBB0_4), then move 1 into the pointer
(which points to rsi[+ 4 bytes cuz it's an int]), so on and so
forth until we pop rax from the memory stack and return.
How did I do? :P (hopefully at least B grade)
I'm not really sure what .L.str or _d_arraybounds is, but I'm
guessing it's the D runtime?
Also in the mov parts, is that moving 1 into the pointer or into
the rsi register? And is rsi + 4, still in rsi, or does it move
to a different register?
More information about the Digitalmars-d-learn
mailing list