]> git.sur5r.net Git - cc65/blob - libsrc/runtime/asr.s
Working on the division and multiplication routines.
[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
15         .import         popax
16         .importzp       tmp1
17
18 tosasrax:
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     L1              ; Jump if not
29
30 ; Shift count is greater 8. 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         cmp     #$00            ; Test sign bit
38         bpl     L1
39         dex                     ; Make X the correct sign extended value
40
41 ; Save the high byte so we can shift it
42
43 L1:     stx     tmp1            ; Save high byte
44         jmp     L3
45
46 ; Do the actual shift
47
48 L2:     cpx     #$80            ; Copy bit 15 into the carry
49         ror     tmp1
50         ror     a
51 L3:     dey
52         bpl     L2
53
54 ; Done with shift
55
56         ldx     tmp1
57 L9:     rts
58
59
60