File: asr.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 (78 lines) | stat: -rw-r--r-- 2,073 bytes parent folder | download | duplicates (3)
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
71
72
73
74
75
76
77
78
;
; Ullrich von Bassewitz, 2004-06-30
;
; CC65 runtime: right shift support for ints
;
; Note: The standard declares a shift count that is negative or >= the
; bitcount of the shifted type for undefined behaviour.
;
; Note^2: The compiler knowns about the register/zero page usage of this
; function, so you need to change the compiler source if you change it!
;


        .export         tosasrax, asraxy
        .import         popax
        .importzp       tmp1

tosasrax:
        sta     tmp1            ; Save shift count
        jsr     popax           ; Get the left hand operand
        ldy     tmp1            ; Get shift count

; Run into asraxy

asraxy:
        pha
        tya
        and     #$0F
        beq     L2              ; Nothing to shift
        sec
        sbc     #8              ; Shift count 8 or greater?
        beq     L3              ; Jump if exactly 8
        bcc     L6              ; Jump if less than 8

; Shift count is greater than 8.

        tay                     ; Shift count into Y
        pla                     ; Discard low byte
        txa                     ; Get high byte

L1:     cmp     #$80            ; Sign bit into carry
        ror     a               ; Carry into A
        dey
        bne     L1
        beq     L4              ; Sign extend and return

; Shift count is zero

L2:     pla
        rts

; Shift count is exactly 8

L3:     pla                     ; Drop low byte from stack ...
        txa                     ; Move high byte to low
L4:     ldx     #$00            ; Clear high byte
        cmp     #$80            ; Check sign bit
        bcc     L5
        dex
L5:     rts

; Shift count is less than 8

L6:     adc     #8              ; Correct counter
        tay                     ; Shift count into Y
        pla                     ; Restore low byte
        stx     tmp1            ; Save high byte of lhs
L7:     cpx     #$80            ; Sign bit into carry
        ror     tmp1
        ror     a
        dey
        bne     L7

; Done with shift

        ldx     tmp1
        rts