]> git.sur5r.net Git - cc65/blob - libsrc/runtime/switch.s
Added mouse module from C64
[cc65] / libsrc / runtime / switch.s
1 ;
2 ; Ullrich von Bassewitz, 17.08.1998
3 ;
4 ; CC65 runtime: switch statement with int selector
5 ;
6
7 ; Subroutine to handle a switch statement with an int selector. The table
8 ; is located at the return address from the function. It contains the negative
9 ; of the case label count as first word, followed by two words for each case
10 ; label, the first one being the value, and the second one the label to jump
11 ; to in case of a match. The default case is located at the end of the table.
12
13         .export         switch
14         .importzp       ptr1, ptr2, ptr3
15
16 switch: sta     ptr1
17         stx     ptr1+1          ; Save AX
18         clc
19         pla
20         adc     #1
21         sta     ptr2
22         pla
23         adc     #0
24         sta     ptr2+1          ; Get pointer to table
25
26         ldy     #0
27         lda     (ptr2),y
28         sta     ptr3
29         iny
30         lda     (ptr2),y
31         sta     ptr3+1          ; Remember the count of labels
32
33         ldy     #0
34         clc                     ; Skip the label count
35         lda     ptr2
36         adc     #2
37         sta     ptr2
38         bcc     L2
39         inc     ptr2+1
40         bne     L2              ; Branch always
41
42 ; Search for the label
43
44 L0:     lda     (ptr2),y
45         iny
46         cmp     ptr1
47         beq     L4
48 L1:     iny
49         iny
50         iny                     ; Overflow only here
51         bne     L2
52         inc     ptr2+1          ; Bump high byte
53
54 ; Check if there are any labels left
55
56 L2:     inc     ptr3
57         bne     L0
58         inc     ptr3+1
59         bne     L0
60
61 ; Out of labels
62
63         tya
64         clc
65         adc     ptr2
66         sta     ptr2
67         bcc     L3
68         inc     ptr2+1
69 L3:     jmp     (ptr2)
70
71
72 ; Check high byte
73
74 L4:     lda     (ptr2),y
75         cmp     ptr1+1
76         bne     L1
77
78 ; Label found
79
80         iny
81         lda     (ptr2),y
82         sta     ptr3
83         iny
84         lda     (ptr2),y
85         sta     ptr3+1
86         jmp     (ptr3)
87
88