]> git.sur5r.net Git - cc65/blob - libsrc/common/memset.s
removed some duplicated GEOS conio stuff
[cc65] / libsrc / common / memset.s
1 ;
2 ; void* memset (void* ptr, int c, size_t n);
3 ; void* _bzero (void* ptr, size_t n);
4 ; void bzero (void* ptr, size_t n);
5 ;
6 ; Ullrich von Bassewitz, 29.05.1998
7 ;
8 ; NOTE: bzero will return it's first argument as memset does. It is no problem
9 ;       to declare the return value as void, since it may be ignored. _bzero
10 ;       (note the leading underscore) is declared with the proper return type,
11 ;       because the compiler will replace memset by _bzero if the fill value
12 ;       is zero, and the optimizer looks at the return type to see if the value
13 ;       in a/x is of any use.
14 ;
15
16         .export         _memset, _bzero, __bzero
17         .import         popax
18         .importzp       sp, ptr1, ptr2, ptr3, tmp1
19
20 _bzero:
21 __bzero:
22         sta     ptr3
23         stx     ptr3+1          ; Save n
24         lda     #0
25         sta     tmp1            ; fill with zeros
26         beq     common
27
28 _memset:
29         sta     ptr3            ; Save n
30         stx     ptr3+1
31         jsr     popax           ; Get c
32         sta     tmp1            ; Save c
33
34 ; Common stuff for memset and bzero from here
35
36 common:
37         ldy     #1
38         lda     (sp),y
39         tax
40         dey
41         lda     (sp),y          ; Get ptr
42         sta     ptr1
43         stx     ptr1+1          ; Save work copy
44
45         lda     tmp1            ; Load fill value
46         ldy     #0
47         ldx     ptr3+1          ; Get high byte of n
48         beq     L2              ; Jump if zero
49
50 ; Set 256 byte blocks
51
52 L1:     sta     (ptr1),y        ; Set one byte
53         iny
54         sta     (ptr1),y        ; Unroll this a bit to make it faster
55         iny
56         bne     L1
57         inc     ptr1+1
58         dex                     ; Next 256 byte block
59         bne     L1              ; Repeat if any
60
61 ; Set the remaining bytes if any
62
63 L2:     ldx     ptr3            ; Get the low byte of n
64         beq     L9              ; Low byte is zero
65
66 L3:     sta     (ptr1),y        ; Set one byte
67         iny
68         dex                     ; Done?
69         bne     L3
70
71 L9:     jmp     popax           ; Pop ptr and return as result
72
73