Alloca implementation

前端 未结 11 1000
误落风尘
误落风尘 2020-11-29 08:50

How does one implement alloca() using inline x86 assembler in languages like D, C, and C++? I want to create a slightly modified version of it, but first I need to know how

11条回答
  •  醉酒成梦
    2020-11-29 09:45

    What we want to do is something like that:

    void* alloca(size_t size) {
         -= size;
        return ;
    }
    

    In Assembly (Visual Studio 2017, 64bit) it looks like:

    ;alloca.asm
    
    _TEXT SEGMENT
        PUBLIC alloca
        alloca PROC
            sub rsp, rcx ; -= size
            mov rax, rsp ;return ;
            ret
        alloca ENDP
    _TEXT ENDS
    
    END
    

    Unfortunately our return pointer is the last item on the stack, and we do not want to overwrite it. Additionally we need to take care for the alignment, ie. round size up to multiple of 8. So we have to do this:

    ;alloca.asm
    
    _TEXT SEGMENT
        PUBLIC alloca
        alloca PROC
            ;round up to multiple of 8
            mov rax, rcx
            mov rbx, 8
            xor rdx, rdx
            div rbx
            sub rbx, rdx
            mov rax, rbx
            mov rbx, 8
            xor rdx, rdx
            div rbx
            add rcx, rdx
    
            ;increase stack pointer
            pop rbx
            sub rsp, rcx
            mov rax, rsp
            push rbx
            ret
        alloca ENDP
    _TEXT ENDS
    
    END
    

提交回复
热议问题