	.file	"fiber_continue_x86_64.s"
	.text
	.global	fiber_continue
	.type	fiber_continue, @function

.align 16
fiber_continue:
        # rdi = address of new stack top and to store current stack top (parameter to function)

        ############### Register State Storage ################
        # make space for all register state we will store
        leaq -0x38(%rsp), %rsp

        # store calee saved general registers
        movq %r12, 0x00(%rsp)
        movq %r13, 0x08(%rsp)
        movq %r14, 0x10(%rsp)
        movq %r15, 0x18(%rsp)
        movq %rbx, 0x20(%rsp)
        movq %rbp, 0x28(%rsp)
        # store MMX control- and status-word
        stmxcsr 0x30(%rsp)
        # store x87 control-word
        fnstcw 0x34(%rsp)

        ############### Stack Pointer Manipulation #############
        # keep the old stack
        movq %rsp, %r12
        # switch to new stack pointer
        movq (%rdi), %rsp
        # store old stack pointer
        movq %r12, (%rdi)

        # We stored our old execution context and are now on the target stack
        # on which we would like to contiune our execution.

        ############ Restore State Storage (as needed) #########
        # restore calee saved general registers
        movq 0x00(%rsp), %r12
        movq 0x08(%rsp), %r13
        movq 0x10(%rsp), %r14
        movq 0x18(%rsp), %r15
        movq 0x20(%rsp), %rbx
        movq 0x28(%rsp), %rbp
        # restore MMX control- and status-word
        ldmxcsr 0x30(%rsp)
        # restore x87 control-word
        fldcw 0x34(%rsp)

        # Free space for restored state
        leaq 0x38(%rsp), %rsp

        # just return back from the call
        ret
