]> git.sur5r.net Git - cc65/blob - libsrc/common/atexit.s
removed some duplicated GEOS conio stuff
[cc65] / libsrc / common / atexit.s
1 ;
2 ; Ullrich von Bassewitz, 06.06.1998
3 ;
4 ; int atexit (void (*f) (void));
5 ;
6
7         .export         _atexit
8         .destructor     doatexit, 5
9         .import         __errno
10         .import         callax
11
12         .include        "errno.inc"
13
14 ; ---------------------------------------------------------------------------
15
16 .proc   _atexit
17
18         ldy     exitfunc_index
19         cpy     #exitfunc_max           ; Slot available?
20         beq     @Error                  ; Jump if no
21
22 ; Enter the function into the table
23
24         sta     exitfunc_table,y
25         iny
26         txa
27         sta     exitfunc_table,y
28         iny
29         sty     exitfunc_index
30
31 ; Done, return zero
32
33         lda     #0
34         tax
35         rts
36
37 ; Error, no space left
38
39 @Error: lda     #ENOSPC         ; No space left
40         sta     __errno
41         ldx     #$00
42         stx     __errno+1
43         dex                     ; Make return value -1
44         txa
45         rts
46
47 .endproc
48
49
50
51 ; ---------------------------------------------------------------------------
52
53 .code
54
55 .proc   doatexit
56
57         ldy     exitfunc_index          ; Get index
58         beq     @L9                     ; Jump if done
59         dey
60         lda     exitfunc_table,y
61         tax
62         dey
63         lda     exitfunc_table,y
64         sty     exitfunc_index
65         jsr     callax                  ; Call the function
66 .ifpc02
67         bra     doatexit
68 .else
69         jmp     doatexit                ; Next one
70 .endif
71
72 @L9:    rts
73
74 .endproc                      
75
76
77
78 ; ---------------------------------------------------------------------------
79
80 .bss
81 exitfunc_index: .res    1       ; Index into table, inc'ed by 2
82 exitfunc_table: .res    10      ; 5 exit functions
83 exitfunc_max    = <(* - exitfunc_table)
84
85