]> git.sur5r.net Git - cc65/blob - libsrc/common/memcmp.s
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / libsrc / common / memcmp.s
1 ;
2 ; Ullrich von Bassewitz, 15.09.2000
3 ;
4 ; int memcmp (const void* p1, const void* p2, size_t count);
5 ;
6
7         .export         _memcmp
8         .import         popax, return0
9         .importzp       ptr1, ptr2, ptr3
10
11 _memcmp:
12
13 ; Calculate (-count-1) and store it into ptr3. This is some overhead here but
14 ; saves time in the compare loop
15
16         eor     #$FF
17         sta     ptr3
18         txa
19         eor     #$FF
20         sta     ptr3+1
21
22 ; Get the pointer parameters
23
24         jsr     popax           ; Get p2
25         sta     ptr2
26         stx     ptr2+1
27         jsr     popax           ; Get p1
28         sta     ptr1
29         stx     ptr1+1
30
31 ; Loop initialization
32
33         ldx     ptr3            ; Load low counter byte into X
34         ldy     #$00            ; Initialize pointer
35
36 ; Head of compare loop: Test for the end condition
37
38 Loop:   inx                     ; Bump low byte of (-count-1)
39         beq     BumpHiCnt       ; Jump on overflow
40
41 ; Do the compare
42
43 Comp:   lda     (ptr1),y
44         cmp     (ptr2),y
45         bne     NotEqual        ; Jump if bytes not equal
46
47 ; Bump the pointers
48
49         iny                     ; Increment pointer
50         bne     Loop
51         inc     ptr1+1          ; Increment high bytes
52         inc     ptr2+1
53         bne     Loop            ; Branch always (pointer wrap is illegal)
54
55 ; Entry on low counter byte overflow
56
57 BumpHiCnt:
58         inc     ptr3+1          ; Bump high byte of (-count-1)
59         bne     Comp            ; Jump if not done
60         jmp     return0         ; Count is zero, areas are identical
61
62 ; Not equal, check which one is greater
63
64 NotEqual:
65         bcs     Greater
66         ldx     #$FF            ; Make result negative
67         rts
68
69 Greater:
70         ldx     #$01            ; Make result positive
71         rts
72