]> git.sur5r.net Git - cc65/blob - libsrc/common/longjmp.s
Improved longjmp() and setjmp().
[cc65] / libsrc / common / longjmp.s
1 ;
2 ; 1998-06-06, Ullrich von Bassewitz
3 ; 2015-09-11, Greg King
4 ;
5 ; void __fastcall__ longjmp (jmp_buf buf, int retval);
6 ;
7
8         .export         _longjmp
9         .import         popax
10         .importzp       sp, ptr1, ptr2
11
12 _longjmp:
13         sta     ptr2            ; Save retval
14         stx     ptr2+1
15         ora     ptr2+1          ; Check for 0
16         bne     @L1
17         inc     ptr2            ; 0 is illegal, according to the standard ...
18                                 ; ... and, must be replaced by 1
19 @L1:    jsr     popax           ; get buf
20         sta     ptr1
21         stx     ptr1+1
22         ldy     #0
23
24 ; Get the old parameter stack
25
26         lda     (ptr1),y
27         iny
28         sta     sp
29         lda     (ptr1),y
30         iny
31         sta     sp+1
32
33 ; Get the old stack pointer
34
35         lda     (ptr1),y
36         iny
37         tax
38         txs
39
40 ; Get the return address and push it on the stack
41
42         lda     (ptr1),y
43         iny
44         pha
45         lda     (ptr1),y
46         pha
47
48 ; Load the return value and return to the caller
49
50         lda     ptr2
51         ldx     ptr2+1
52         rts