shithub: drawcpu

ref: 847cd0b5262ebdf9deb3c294bedffc162b4a80d2
dir: /posix-arm64/start.s/

View raw version
.text
.global _start
.align 4
_start:
    // Function prologue
    stp x29, x30, [sp, #-16]!  // Save frame pointer and link register
    mov x29, sp                // Set up frame pointer

    // Save callee-saved registers
    stp x19, x20, [sp, #-16]!

    // Your original code starts here
    mov x19, x0         // entry (x0 is the first argument)
    mov x9, x1          // _tos (x1 is the second argument)
    mov x20, x2         // argc (x2 is the third argument)
    mov x21, x3         // argv (x3 is the fourth argument)

    // Push argv onto stack
    mov x10, x20        // x10 = argc
    add x10, x10, #1    // x10 = argc + 1
    lsl x10, x10, #3    // x10 = (argc + 1) * 8
    sub sp, sp, x10     // Allocate space on stack
    
    // Ensure 16-byte stack alignment
    and x11, x10, #15   // Get the misalignment amount
    sub sp, sp, x11     // Adjust stack to ensure alignment

    mov x0, sp          // x0 = new stack pointer (destination for memcpy)
    mov x1, x21         // x1 = argv (source for memcpy)
    mov x2, x10         // x2 = number of bytes to copy
    bl _memcpy

    // Push argc onto stack (after the argv data)
    str x20, [sp, x10]

    // Call the entry point
    blr x19

    // Restore stack pointer
    mov sp, x29

    // Restore callee-saved registers
    ldp x19, x20, [sp], #16

    // Function epilogue
    ldp x29, x30, [sp], #16

    ret