]> git.sur5r.net Git - cc65/blob - libsrc/common/memcmp.s
Merge pull request #740 from laubzega/master
[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, popptr1, 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     popptr1         ; Get p1
28
29 ; Loop initialization
30
31         ;ldy     #$00           ; Initialize pointer (Y=0 guaranteed by popptr1)
32         ldx     ptr3            ; Load low counter byte into X
33
34 ; Head of compare loop: Test for the end condition
35
36 Loop:   inx                     ; Bump low byte of (-count-1)
37         beq     BumpHiCnt       ; Jump on overflow
38
39 ; Do the compare
40
41 Comp:   lda     (ptr1),y
42         cmp     (ptr2),y
43         bne     NotEqual        ; Jump if bytes not equal
44
45 ; Bump the pointers
46
47         iny                     ; Increment pointer
48         bne     Loop
49         inc     ptr1+1          ; Increment high bytes
50         inc     ptr2+1
51         bne     Loop            ; Branch always (pointer wrap is illegal)
52
53 ; Entry on low counter byte overflow
54
55 BumpHiCnt:
56         inc     ptr3+1          ; Bump high byte of (-count-1)
57         bne     Comp            ; Jump if not done
58         jmp     return0         ; Count is zero, areas are identical
59
60 ; Not equal, check which one is greater
61
62 NotEqual:
63         bcs     Greater
64         ldx     #$FF            ; Make result negative
65         rts
66
67 Greater:
68         ldx     #$01            ; Make result positive
69         rts
70