]> git.sur5r.net Git - cc65/blob - libsrc/runtime/asr.s
Added SER_ prefix. Whitespace cleanup
[cc65] / libsrc / runtime / asr.s
1 ;
2 ; Ullrich von Bassewitz, 2004-06-30
3 ;
4 ; CC65 runtime: right shift support for ints
5 ;
6 ; Note: The standard declares a shift count that is negative or >= the
7 ; bitcount of the shifted type for undefined behaviour.
8 ;
9 ; Note^2: The compiler knowns about the register/zero page usage of this
10 ; function, so you need to change the compiler source if you change it!
11 ;
12
13
14         .export         tosasrax, asraxy
15         .import         popax
16         .importzp       tmp1
17
18 tosasrax:
19         sta     tmp1            ; Save shift count
20         jsr     popax           ; Get the left hand operand
21         ldy     tmp1            ; Get shift count
22
23 ; Run into asraxy
24
25 asraxy:
26         pha
27         tya
28         and     #$0F
29         beq     L2              ; Nothing to shift
30         sec
31         sbc     #8              ; Shift count 8 or greater?
32         beq     L3              ; Jump if exactly 8
33         bcc     L6              ; Jump if less than 8
34
35 ; Shift count is greater than 8.
36
37         tay                     ; Shift count into Y
38         pla                     ; Discard low byte
39         txa                     ; Get high byte
40
41 L1:     cmp     #$80            ; Sign bit into carry
42         ror     a               ; Carry into A
43         dey
44         bne     L1
45         beq     L4              ; Sign extend and return
46
47 ; Shift count is zero
48
49 L2:     pla
50         rts
51
52 ; Shift count is exactly 8
53
54 L3:     pla                     ; Drop low byte from stack ...
55         txa                     ; Move high byte to low
56 L4:     ldx     #$00            ; Clear high byte
57         cmp     #$80            ; Check sign bit
58         bcc     L5
59         dex
60 L5:     rts
61
62 ; Shift count is less than 8
63
64 L6:     adc     #8              ; Correct counter
65         tay                     ; Shift count into Y
66         pla                     ; Restore low byte
67         stx     tmp1            ; Save high byte of lhs
68 L7:     cpx     #$80            ; Sign bit into carry
69         ror     tmp1
70         ror     a
71         dey
72         bne     L7
73
74 ; Done with shift
75
76         ldx     tmp1
77         rts
78