VLA in Assembler

btdc via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Dec 17 04:55:56 PST 2014


On Wednesday, 17 December 2014 at 12:54:44 UTC, btdc wrote:
> On Wednesday, 17 December 2014 at 10:35:39 UTC, Foo wrote:
>> Hi,
>> Could someone explain me, if and how it is possible to 
>> allocate a variable length array with inline assembly?
>> Somewhat like
>> ----
>> int[] arr;
>> int n = 42;
>> asm {
>>    // allocate n stack space for arr
>> }
>> ----
>> I know it is dangerous and all that, but I just want it know. 
>> ;)
>
> It's probably something like that:
>
> module runnable;
>
> import std.stdio;
> import std.c.stdlib;
>
> ubyte[] newArr(size_t aLength)
> {
>     asm
>     {
>         naked;
>
>         mov ECX, EAX;       // saves aLength in ECX
>
>         push ECX;
>         call malloc;        // .ptr =  malloc(aLength);
>         mov ECX,[EAX];      // saved the .ptr of our array
>
>         mov EAX, 8;         // an array is a struct with length 
> and ptr
>                             // so 8 bytes in 32 bit
>         call malloc;        // EAX points to the first byte of 
> the struct
>
>         mov [EAX + 4], ECX; // .ptr
>         pop ECX;
>         mov [EAX], ECX;     // .length
>         mov EAX, [EAX];     // curretnly EAX is a ref, so need 
> to dig...
>
>         ret;
>     }
> }
>
> try and see ;) Actually it may be wrong

fuck...the comments are once again cut...


More information about the Digitalmars-d-learn mailing list