]> git.sur5r.net Git - cc65/blob - libsrc/common/atexit.s
Replace strdup by an assembler implementation
[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         .include        "errno.inc"
13
14 .bss
15 ecount: .byte   0               ; Really an index, inc'ed by 2
16 efunc:  .word   0,0,0,0,0       ; 5 exit functions
17 maxcount = * - efunc
18
19
20 .code
21
22 _atexit:
23         ldy     ecount
24         cpy     #maxcount       ; slot available?
25         beq     E0              ; jump if no
26
27 ; Enter the function into the table
28
29         sta     efunc,y
30         iny
31         txa
32         sta     efunc,y
33         iny
34         sty     ecount
35
36 ; Done, return zero
37
38         lda     #0
39         tax
40         rts
41
42 ; Error, no space left
43
44 E0:     lda     #ENOSPC         ; No space left
45         sta     __errno
46         ldx     #$00
47         stx     __errno+1
48         dex
49         txa
50         rts
51
52 ; Function called from exit
53
54 doatexit:
55         ldy     ecount          ; get index
56         beq     L9              ; jump if done
57         dey
58         lda     efunc,y
59         sta     jmpvec+2
60         dey
61         lda     efunc,y
62         sta     jmpvec+1
63         sty     ecount
64         ldy     #0              ; number of function parms
65         jsr     jmpvec
66         jmp     doatexit        ; next one
67
68 L9:     rts
69
70
71
72
73