File: memcmp.s

package info (click to toggle)
cc65 2.19-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,268 kB
  • sloc: ansic: 117,151; asm: 66,339; pascal: 4,248; makefile: 1,009; perl: 607
file content (70 lines) | stat: -rw-r--r-- 1,765 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
;
; Ullrich von Bassewitz, 15.09.2000
;
; int memcmp (const void* p1, const void* p2, size_t count);
;

        .export         _memcmp
        .import         popax, popptr1, return0
        .importzp       ptr1, ptr2, ptr3

_memcmp:

; Calculate (-count-1) and store it into ptr3. This is some overhead here but
; saves time in the compare loop

        eor     #$FF
        sta     ptr3
        txa
        eor     #$FF
        sta     ptr3+1

; Get the pointer parameters

        jsr     popax           ; Get p2
        sta     ptr2
        stx     ptr2+1
        jsr     popptr1         ; Get p1

; Loop initialization

        ;ldy     #$00           ; Initialize pointer (Y=0 guaranteed by popptr1)
        ldx     ptr3            ; Load low counter byte into X

; Head of compare loop: Test for the end condition

Loop:   inx                     ; Bump low byte of (-count-1)
        beq     BumpHiCnt       ; Jump on overflow

; Do the compare

Comp:   lda     (ptr1),y
        cmp     (ptr2),y
        bne     NotEqual        ; Jump if bytes not equal

; Bump the pointers

        iny                     ; Increment pointer
        bne     Loop
        inc     ptr1+1          ; Increment high bytes
        inc     ptr2+1
        bne     Loop            ; Branch always (pointer wrap is illegal)

; Entry on low counter byte overflow

BumpHiCnt:
        inc     ptr3+1          ; Bump high byte of (-count-1)
        bne     Comp            ; Jump if not done
        jmp     return0         ; Count is zero, areas are identical

; Not equal, check which one is greater

NotEqual:
        bcs     Greater
        ldx     #$FF            ; Make result negative
        rts

Greater:
        ldx     #$01            ; Make result positive
        rts