bug in shift right operator with longwords

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

bug in shift right operator with longwords

Post by Jerry Messina » Sat Jul 17, 2010 9:46 am

Somebody was having an issue with '>>' over on digital-diy, and since it looks like this site's going to stick around for a while longer, I thought I should post it here.

The following code doesn't work...

Code: Select all

    dim shiftreg as longword
    dim shift as byte

        for shift = 1 to 31
            shiftreg = $80005A5A             // test pattern
            shiftreg = shiftreg >> shift      // this line doesn't work correctly
        next

It does an optimization where it places (or tries to) 'shift' into the WREG, and then does a little loop to do the shift, decrementing WREG as the loop count. The problem is it screws up the initial loading of WREG. This doesn't seem to happen if 'shiftreg' is declared as a byte or a word, or for doing left shifts (as it uses a different algorithm).

A fix/kludge around this would be

Code: Select all

        for shift = 1 to 31
            shiftreg = $80005A5A
            WREG = shift       
            shiftreg = shiftreg >> WREG
        next
which appears to work.

Jerry

Post Reply