VLA in Assembler

uri via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Dec 17 04:15:22 PST 2014


On Wednesday, 17 December 2014 at 11:39:43 UTC, Foo wrote:
> On Wednesday, 17 December 2014 at 10:59:09 UTC, bearophile 
> wrote:
>> Foo:
>>
>>> 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. 
>>> ;)
>>
>> Doing it with alloca is simpler:
>>
>>
>> void main() @nogc {
>>    import core.stdc.stdlib: alloca, exit;
>>
>>    alias T = int;
>>    enum n = 42;
>>
>>    auto ptr = cast(T*)alloca(T.sizeof * n);
>>    if (ptr == null)
>>        exit(1); // Or throw a memory error.
>>    auto arr = ptr[0 .. n];
>> }
>>
>>
>> Bye,
>> bearophile
> Yes I know, but I really want it in inline assembly. It's for 
> learning purpose. :)

You could look at the disassembly.


More information about the Digitalmars-d-learn mailing list