]> git.sur5r.net Git - cc65/blob - libsrc/common/atexit.s
This commit was generated by cvs2svn to compensate for changes in r2,
[cc65] / libsrc / common / atexit.s
1 ;
2 ; Ullrich von Bassewitz, 06.06.1998
3 ;
4 ; int atexit (void (*f) (void));
5 ;
6
7 ; The exit functions
8
9         .export         _atexit, doatexit
10         .import         __errno, jmpvec
11
12 .bss
13 ecount: .byte   0               ; Really an index, inc'ed by 2
14 efunc:  .word   0,0,0,0,0       ; 5 exit functions
15 maxcount = * - efunc
16
17
18 .code
19
20 _atexit:
21         ldy     ecount
22         cpy     #maxcount       ; slot available?
23         beq     E0              ; jump if no
24
25 ; Enter the function into the table
26
27         sta     efunc,y
28         iny
29         txa
30         sta     efunc,y
31         iny
32         sty     ecount
33
34 ; Done, return zero
35
36         lda     #0
37         tax
38         rts
39
40 ; Error, no space left
41
42 E0:     lda     #$FF
43         sta     __errno         ; Use -1 until codes are defined ###
44         sta     __errno+1
45         tax
46         rts
47
48 ; Function called from exit
49
50 doatexit:
51         ldy     ecount          ; get index
52         beq     L9              ; jump if done
53         dey
54         lda     efunc,y
55         sta     jmpvec+2
56         dey
57         lda     efunc,y
58         sta     jmpvec+1
59         sty     ecount
60         ldy     #0              ; number of function parms
61         jsr     jmpvec
62         jmp     doatexit        ; next one
63
64 L9:     rts
65
66
67
68
69