Jump to content



0

Question from a beginner


2 replies to this topic

#1 rosutokami OFFLINE  

rosutokami

    Combat Commando

  • 2 posts

Posted Mon Aug 27, 2007 11:28 PM

Hello!
Do you all know if it is possible on the 2600 to 'shift' the bits on a byte? As in, if I had a byte 00010100, to somehow with a command or math move the bits so it read 00001010 or 00101000. I have been thinking about ways to modify sprites by putting them into RAM instead of having each image take up ROM space, and this idea came to me.

#2 SeaGtGruff OFFLINE  

SeaGtGruff

    River Patroller

  • 4,543 posts
  • Location:Georgia, USA

Posted Tue Aug 28, 2007 12:47 AM

View Postrosutokami, on Tue Aug 28, 2007 1:28 AM, said:

Hello!
Do you all know if it is possible on the 2600 to 'shift' the bits on a byte? As in, if I had a byte 00010100, to somehow with a command or math move the bits so it read 00001010 or 00101000. I have been thinking about ways to modify sprites by putting them into RAM instead of having each image take up ROM space, and this idea came to me.
Yes, you can shift and rotate the bits in a byte. It's a common assembly programming technique, and is so important for different things (such as math) that there are assembly instructions just for that purpose. The 6502 assembly language has two shift commands, and two rotate commands:

ASL -- Shift Left
All bits are shifted left 1 position. 0 is shifted into bit 0, and bit 7 is shifted into the carry.

LSR -- Shift Right
All bits are shifted right 1 position. 0 is shifted into bit 7, and bit 0 is shifted into the carry.

ROL -- Rotate Left
All bits are shifted left 1 position. The carry is shifted into bit 0, and bit 7 is shifted into the carry.

ROR -- Rotate Right
All bits are shifted right 1 position. The carry is shifted into bit 7, and bit 0 is shifted into the carry.

Note that the rotate instructions do *not* rotate bit 7 into bit 0, or bit 0 into bit 7, so if you want to do a full rotation of a byte like that, you'll need to use a shift instruction instead of a rotate instruction, then follow it with a test of the carry flag, and modify the shifted byte accordingly, like this:

; rotate a byte left
   LDA desired_byte; shifting the accumulator is faster than shifting a byte in memory
   ASL; this shifts the bits ot the accumulator left, 0 goes into bit 0, and bit 7 goes into the carry
   BCC all_done; if the carry is clear (or 0), then we're done
   ORA #000001; otherwise we want to set bit 0 to a 1 without messing up the other bits
all_done
   STA desired_byte; now we store the result back into memory
; rotate a byte right
   LDA desired_byte; shifting the accumulator is faster than shifting a byte in memory
   LSR; this shifts the bits ot the accumulator right, 0 goes into bit 7, and bit 0 goes into the carry
   BCC all_done; if the carry is clear (or 0), then we're done
   ORA #%10000000; otherwise we want to set bit 7 to a 1 without messing up the other bits
all_done
   STA desired_byte; now we store the result back into memory
Michael

#3 rosutokami OFFLINE  

rosutokami

    Combat Commando

  • 2 posts

Posted Tue Aug 28, 2007 5:55 AM

That's really cool, thanks! :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users