2 ; Ullrich von Bassewitz, 2004-06-30
4 ; CC65 runtime: right shift support for ints
6 ; Note: The standard declares a shift count that is negative or >= the
7 ; bitcount of the shifted type for undefined behaviour.
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!
19 and #$0F ; Bring the shift count into a valid range
22 jsr popax ; Get the left hand operand
24 ldy tmp1 ; Get shift count
25 beq L9 ; Bail out if shift count zero
27 cpy #8 ; Shift count 8 or greater?
30 ; Shift count is greater 8. The carry is set when we enter here.
34 tay ; Adjust shift count
36 ldx #$00 ; Shift by 8 bits
37 cmp #$00 ; Test sign bit
39 dex ; Make X the correct sign extended value
41 ; Save the high byte so we can shift it
43 L1: stx tmp1 ; Save high byte
48 L2: cpx #$80 ; Copy bit 15 into the carry