github.com/DQNEO/babygo@v0.0.3/src/runtime/runtime.s (about)

     1  # runtime.s
     2  .text
     3  
     4  # shortcut entrypoint to simplify linker invocation
     5  .global _start
     6  _start:
     7    jmp _rt0_amd64_linux
     8  
     9  # Start of the program
    10  # (runtime/rt0_linux_amd64.s)
    11  .global _rt0_amd64_linux
    12  _rt0_amd64_linux:
    13    jmp _rt0_amd64
    14  
    15  # (runtime/asm_amd64.s)
    16  _rt0_amd64:
    17    movq 0(%rsp), %rdi # argc
    18    leaq 8(%rsp), %rsi # argv
    19    jmp runtime.rt0_go
    20  
    21  # (runtime/asm_amd64.s)
    22  runtime.rt0_go:
    23    movq %rdi, %rax # argc
    24    movq %rsi, %rbx # argv
    25    movq %rbx, runtime.__argv__+0(%rip)  # ptr
    26    movq %rax, runtime.__argv__+8(%rip)  # len
    27    movq %rax, runtime.__argv__+16(%rip) # cap
    28  
    29    movq %rdi, %rax # argc
    30    imulq $8,  %rax # argc * 8
    31    addq %rsp, %rax # stack top addr + (argc * 8)
    32    addq $16,  %rax # + 16 (skip null and go to next) => envp
    33    movq %rax, runtime.envp+0(%rip) # envp
    34  
    35    callq runtime.heapInit
    36  
    37    callq runtime.__initGlobals
    38    callq runtime.envInit
    39  
    40    callq main.__initGlobals
    41  
    42    callq os.init # set os.Args
    43    callq main.main
    44  
    45    movq $0, %rdi  # status 0
    46    movq $60, %rax # sys_exit
    47    syscall
    48    # End of program
    49  
    50  // func Write(fd int, p []byte) int
    51  runtime.Write:
    52    movq  8(%rsp), %rax # arg0:fd
    53    movq 16(%rsp), %rdi # arg1:ptr
    54    movq 24(%rsp), %rsi # arg2:len
    55    subq $8, %rsp
    56    pushq %rsi
    57    pushq %rdi
    58    pushq %rax
    59    pushq $1  # sys_write
    60    callq syscall.Syscall
    61    addq $8 * 4, %rsp # reset args area
    62    popq %rax # retval
    63    movq %rax, 32(%rsp) # r0 int
    64    ret
    65  
    66  runtime.printstring:
    67    movq  8(%rsp), %rdi # arg0:ptr
    68    movq 16(%rsp), %rsi # arg1:len
    69    subq $8, %rsp
    70    pushq %rsi
    71    pushq %rdi
    72    pushq $2 # stderr
    73    pushq $1 # sys_write
    74    callq syscall.Syscall
    75    addq $8 * 4, %rsp
    76    popq %rax # retval
    77    ret
    78  
    79  // func Syscall(trap, a1, a2, a3 uintptr) uintptr
    80  runtime.Syscall:
    81    movq   8(%rsp), %rax # syscall number
    82    movq  16(%rsp), %rdi # arg0
    83    movq  24(%rsp), %rsi # arg1
    84    movq  32(%rsp), %rdx # arg2
    85    syscall
    86    movq %rax, 40(%rsp) # r0 uintptr
    87    ret
    88  
    89  // func Syscall(trap, a1, a2, a3 uintptr) uintptr
    90  syscall.Syscall:
    91    movq   8(%rsp), %rax # syscall number
    92    movq  16(%rsp), %rdi # arg0
    93    movq  24(%rsp), %rsi # arg1
    94    movq  32(%rsp), %rdx # arg2
    95    syscall
    96    movq %rax, 40(%rsp) # r0 uintptr
    97    ret
    98