]> git.sur5r.net Git - cc65/blob - libsrc/common/memset.s
New loadable mouse drivers
[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              ; Fill with zeros
25         beq     common
26         
27 _memset:
28         sta     ptr3            ; Save n
29         stx     ptr3+1
30         jsr     popax           ; Get c
31
32 ; Common stuff for memset and bzero from here
33
34 common: sta     tmp1            ; Save the fill value
35         ldy     #1
36         lda     (sp),y
37         tax
38         dey
39         lda     (sp),y          ; Get ptr
40         sta     ptr1
41         stx     ptr1+1          ; Save work copy
42
43         lda     tmp1            ; Load fill value
44         ldy     #0
45         ldx     ptr3+1          ; Get high byte of n
46         beq     L2              ; Jump if zero
47
48 ; Set 256 byte blocks
49
50 L1:     .repeat 2               ; Unroll this a bit to make it faster
51         sta     (ptr1),y        ; Set one byte
52         iny
53         .endrepeat
54         bne     L1
55         inc     ptr1+1
56         dex                     ; Next 256 byte block
57         bne     L1              ; Repeat if any
58
59 ; Set the remaining bytes if any
60
61 L2:     ldx     ptr3            ; Get the low byte of n
62         beq     L9              ; Low byte is zero
63
64 L3:     sta     (ptr1),y        ; Set one byte
65         iny
66         dex                     ; Done?
67         bne     L3
68
69 L9:     jmp     popax           ; Pop ptr and return as result
70
71