1;--------------------------------------------------------------------------
2;  setjmp.s
3;
4;  Copyright (C) 2011-2014, Philipp Klaus Krause
5;
6;  This library is free software; you can redistribute it and/or modify it
7;  under the terms of the GNU General Public License as published by the
8;  Free Software Foundation; either version 2, or (at your option) any
9;  later version.
10;
11;  This library is distributed in the hope that it will be useful,
12;  but WITHOUT ANY WARRANTY; without even the implied warranty of
13;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;  GNU General Public License for more details.
15;
16;  You should have received a copy of the GNU General Public License
17;  along with this library; see the file COPYING. If not, write to the
18;  Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
19;   MA 02110-1301, USA.
20;
21;  As a special exception, if you link this library with other files,
22;  some of which are compiled with SDCC, to produce an executable,
23;  this library does not by itself cause the resulting executable to
24;  be covered by the GNU General Public License. This exception does
25;  not however invalidate any other reasons why the executable file
26;   might be covered by the GNU General Public License.
27;--------------------------------------------------------------------------
28
29	.area   _CODE
30
31	.globl ___setjmp
32
33___setjmp:
34	pop	bc
35	pop	de
36	push	de
37	push	bc
38
39	; Store stack pointer.
40	ldhl	sp, #0
41	push	de
42	push	hl
43	pop	de
44	pop	hl
45	ld	(hl), e
46	inc	hl
47	ld	(hl), d
48	inc	hl
49
50	; Store return address.
51	ld	(hl), c
52	inc	hl
53	ld	(hl), b
54
55	; Return 0.
56	xor	a, a
57	ld	e, a
58	ld	d, a
59	ret
60
61.globl _longjmp
62
63_longjmp:
64	pop	af
65	pop	hl
66	pop	de
67
68	; Ensure that return value is non-zero.
69	ld	a, e
70	or	a, d
71	jr	NZ, 0001$
72	inc	de
730001$:
74
75	; Get stack pointer.
76	ld	c, (hl)
77	inc	hl
78	ld	b, (hl)
79	inc	hl
80
81	; Adjust stack pointer.
82	push	hl
83	push	bc
84	pop	hl
85	pop	bc
86	ld	sp, hl
87	push	bc
88	pop	hl
89
90	; Get return address.
91	ld	c, (hl)
92	inc	hl
93	ld	b, (hl)
94
95	; Set return address.
96	pop	af
97	push	bc
98
99	; Return value is in de.
100
101	; Jump.
102	ret
103