]> git.sur5r.net Git - cc65/blob - libsrc/runtime/lswitch.s
Remove bash pecularities.
[cc65] / libsrc / runtime / lswitch.s
1 ;
2 ; Ullrich von Bassewitz, 17.08.1998
3 ;
4 ; CC65 runtime: switch statement with long selector
5 ;
6
7 ; Subroutine to handle a switch statement with an long 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 three words for each case
10 ; label, the first two being the value, and the last 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         lswitch
14         .importzp       sreg, ptr1, ptr2, ptr3
15
16 lswitch:
17         sta     ptr1
18         stx     ptr1+1          ; Save AX
19         clc
20         pla
21         adc     #1
22         sta     ptr2
23         pla
24         adc     #0
25         sta     ptr2+1          ; Get pointer to table
26
27         ldy     #0
28         lda     (ptr2),y
29         sta     ptr3
30         iny
31         lda     (ptr2),y
32         sta     ptr3+1          ; Remember the count of labels
33
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:     ldy     #0
45         lda     (ptr2),y
46         cmp     ptr1
47         bne     L1
48         iny
49         lda     (ptr2),y
50         cmp     ptr1+1
51         bne     L1
52         iny
53         lda     (ptr2),y
54         cmp     sreg
55         bne     L1
56         iny
57         lda     (ptr2),y
58         cmp     sreg+1
59         beq     L3
60 L1:     clc
61         lda     ptr2
62         adc     #6              ; Skip table entry
63         sta     ptr2
64         bcc     L2
65         inc     ptr2+1
66
67 ; Check if there are any labels left
68
69 L2:     inc     ptr3
70         bne     L0
71         inc     ptr3+1
72         bne     L0
73
74 ; Out of labels
75
76         jmp     (ptr2)
77
78 ; Label found
79
80 L3:     ldy     #4              ; Jump label offset
81         lda     (ptr2),y
82         sta     ptr3
83         iny
84         lda     (ptr2),y
85         sta     ptr3+1
86         jmp     (ptr3)
87