-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcoroutine_yield-x86-64.asm
105 lines (91 loc) · 2.47 KB
/
coroutine_yield-x86-64.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
;***************************************************************************
;
; Copyright 2017 by Sean Conner.
;
; This library is free software; you can redistribute it and/or modify it
; under the terms of the GNU Lesser General Public License as published by
; the Free Software Foundation; either version 3 of the License, or (at your
; option) any later version.
;
; This library is distributed in the hope that it will be useful, but
; WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
; License for more details.
;
; You should have received a copy of the GNU Lesser General Public License
; along with this library; if not, see <http://www.gnu.org/licenses/>.
;
; Comments, questions and criticisms can be sent to: [email protected]
;
;***************************************************************************
bits 64
default rel
global coroutine_init64
global coroutine_yield64
global _coroutine_init64 ; for Mac OS-X
global _coroutine_yield64 ; (how archaic!)
;***************************************************************************
section .text
;===========================================================================
%assign L_co -8
%assign L_fun -16
start_it_up: mov rdi,[rbp + L_co]
mov rsi,rax
call [rbp + L_fun]
do_it_again: mov rdi,[rbp + L_co]
mov rsi,rax
call coroutine_yield64
jmp do_it_again
;===========================================================================
;
; RDI -> co
; RSI -> fun
; RDX -> stack
; RAX -> 0
; RCX -> adjusted stack
;
; L_co 64
; L_run 56
; start_it_up 48
; rbp 40
; rbx 32
; r12 24
; r13 16
; r14 8
; r15 0
;
coroutine_init64:
_coroutine_init64:
lea rax,[start_it_up]
lea rcx,[rdx - 9*8]
mov [rcx + 64],rdi ; L_co
mov [rcx + 56],rsi ; L_fun
mov [rcx + 48],rax ; start_it_up
mov [rcx + 40],rdx ; "saved" rbp
xor eax,eax
mov [rcx + 32],rax ; "saved" rbx
mov [rcx + 24],rax ; "saved" r12
mov [rcx + 16],rax ; "saved" r13
mov [rcx + 8],rax ; "saved" r14
mov [rcx],rax ; "saved" r15
mov [rdi],rcx
ret
;===========================================================================
coroutine_yield64:
_coroutine_yield64:
push rbp
push rbx
push r12
push r13
push r14
push r15
mov rax,rsi ; value to return
xchg rsp,[rdi] ; YIELD!
pop r15
pop r14
pop r13
pop r12
pop rbx
pop rbp
ret
;***************************************************************************