• 0

# Rotating bytes

## Question

Hi guys,

I'm pretty much a total beginner with 6502 assembly.

After watching David M's Planet X3 series, I was puzzled by his tile editor. In particular, with the feature to rotate the tile horizontally and vertically (essentially, is behaves like a torus). I've been playing around with writing a routine to do just that. Horizontal rotation is the easy part, we traverse an 8-byte array and use ROR and ROL for each byte, something like:

Quote

LDX #8

loop:

CLC

DEX

ROL \$1000,X

BNE loop

I can get a similar behavior for vertical rotation. Conceptually, I get it. Y = \$1000, \$1000 = \$1001, \$1001 = \$1002, ... , \$1007 = Y. But what's an efficient way to implement this?

Edited by Felipe Balbi

## Recommended Posts

• 0

Answering my own question. I can rotate vertically in one direction with:

Quote

LDY \$1007

LDX #7

loop:

lda \$fff,x

sta \$1000,x

dex

bne loop

sty \$1000

Just missing vertical rotation in the other direction.

##### Share on other sites

• 0

You should look into (zp),y addressing mode. In assembly,

lda (r0)

sta (r1)

will copy a byte from the address referred to in r0 and store it in the address referred to in r1. If you did lda (r0),y the index register y is added to the address in r0. ie, you would get (in pseudocode) r1[y]=r0[y]

##### Share on other sites

• 0

By the way, your logic with the bit rotation is right, but the ROL and ROR operators on the 6502 aren't actually 8-bit rotate operations. The bit that is shifted off goes into the carry, and the bit that is shifted on comes from the old carry. I've found them more useful for doing multibyte bitshifts than single byte bit rotates. They are 9-bit rotates, but I find it's more useful to think of them as logical shift with carry.

I usually do 8-bit rotate left as

asl a

but that works best when the value you want is already in the accumulator.

##### Share on other sites

• 0
12 hours ago, lamb-duh said:

By the way, your logic with the bit rotation is right, but the ROL and ROR operators on the 6502 aren't actually 8-bit rotate operations. The bit that is shifted off goes into the carry, and the bit that is shifted on comes from the old carry. I've found them more useful for doing multibyte bitshifts than single byte bit rotates. They are 9-bit rotates, but I find it's more useful to think of them as logical shift with carry.

I usually do 8-bit rotate left as

asl a

but that works best when the value you want is already in the accumulator.

Thank you, this is great information. I'll have a deeper look at ZP addressing modes

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.