]> git.sur5r.net Git - cc65/blob - libsrc/runtime/shr.s
Added C callable entry point for the 16x16=32 multiplication.
[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
15         .import         popax
16         .importzp       tmp1
17
18 tosshrax:
19         and     #$0F            ; Bring the shift count into a valid range
20         sta     tmp1            ; Save it
21
22         jsr     popax           ; Get the left hand operand
23
24         ldy     tmp1            ; Get shift count
25         beq     L9              ; Bail out if shift count zero
26
27         cpy     #8              ; Shift count 8 or greater?
28         bcc     L3              ; Jump if not
29
30 ; Shift count is greater 7. The carry is set when we enter here.
31
32         tya
33         sbc     #8
34         tay                     ; Adjust shift count
35         txa
36         ldx     #$00            ; Shift by 8 bits
37         beq     L2              ; Branch always
38 L1:     lsr     a
39 L2:     dey
40         bpl     L1
41         rts
42
43 ; Shift count is less than 8. Do the actual shift.
44
45 L3:     stx     tmp1            ; Save high byte of lhs
46 L4:     lsr     tmp1
47         ror     a
48         dey
49         bne     L4
50
51 ; Done with shift
52
53         ldx     tmp1
54 L9:     rts
55