]> git.sur5r.net Git - cc65/blob - libsrc/runtime/shr.s
cfg/atari-xex.cfg: fix typo in comment
[cc65] / libsrc / runtime / shr.s
1 ;
2 ; Ullrich von Bassewitz, 2004-06-30
3 ;
4 ; CC65 runtime: right shift support for unsigneds
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         tosshrax, shraxy
15         .import         popax
16         .importzp       tmp1
17
18 tosshrax:
19         sta     tmp1            ; Save shift count
20         jsr     popax           ; Get the left hand operand
21         ldy     tmp1            ; Get shift count
22
23 ; Run into shraxy
24
25 shraxy:
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     L4              ; 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:     lsr     a
42         dey
43         bne     L1
44         ldx     #$00            ; High byte is zero
45         rts
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         ldx     #$00            ; Clear high byte
57         rts
58
59 ; Shift count is less than 8
60
61 L4:     adc     #8              ; Correct counter
62         tay                     ; Shift count into Y
63         pla                     ; Restore low byte
64         stx     tmp1            ; Save high byte of lhs
65 L5:     lsr     tmp1
66         ror     a
67         dey
68         bne     L5
69
70 ; Done with shift
71
72         ldx     tmp1
73         rts
74
75