Shift and Rotate

z80 » Advanced

Before I start to teach you the shift and rotate instructions. You probably want to review the bit placements. You'll need to look at the bit place names especially. Each of these shift and rotate commands rotates one bit to the left or right.

There is only really one difference between the shift and rotate instructions: rotate cycles the bits around going out one side and coming in the other, while shift rotates the bits out one side or the other leaving the space where the rotated bits where either unchanged or zeroed.

rl

rl arg1 - Rotates arg1 register to the left with the carry's value put into bit 0 and bit 7 is put into the carry.
rl d
rl (hl)
rl (iy+02)
rl (ix+3)

rla

rla - Rotates a register to the left with the carry's value put into bit 0 and bit 7 is put into the carry. One byte shorter than rl a.
rla

rlc

rlc arg1 - Rotates arg1 to the left with bit 7 being moved to bit 0 and also stored into the carry.
rlc (hl)
rlc (iy+5)
rlc (ix+$10)
rlc a

rlca

rlca - Rotates a to the left with bit 7 being moved to bit 0 and also stored into the carry. It's one byte shorter than rlc a.
rlca

rr

rr arg1 - Rotates arg1 to the right with the carry put in bit 7 and bit 0 put into the carry.
rr (hl)
rr b
rr (ix+5)
rr (iy+$00)

rra

rra - Rotates a to the right with the carry put into bit 7 and bit 0 put into the carry flag. It's one byte shorter than rr a.
rra

rrc

rrc arg1 - Rotates arg1 to the right with bit 0 moved to bit 7 and also stored into the carry.
rrc (hl)
rrc (ix+3)
rrc (iy+$09)
rrc e

rrca

rrca - Rotates arg1 to the right with bit 0 moved to bit 7 and also stored into the carry. This is one byte shorter than rrc a.
rrca

sla

sla arg1 - Shifts arg1 register to the left with bit 7 moved to the carry flag and bit 0 reset (zeroed).
sla (hl)
sla (iy+$08)
sla (ix+3)
sla e

sra

sra arg1 - Shifts arg1 register to the right with bit 0 moved to the carry flag and bit 7 retaining it's original value.
sra (hl)
sra (ix+0)
sra (iy+200)
sra h

srl

srl arg1 - Shifts arg1 register to the right with bit 0 moved to the carry flag and bit 7 zeroed.
srl (hl)
srl (ix+$fe)
srl (iy+32)
srl c


More from z80 » Advanced
All The Ports // APD // Assembler Directives // Entry Stack // User Fonts // IM 1 // IM 2 // Index, Shadow, and Other Registers // User Interrupt // Morphic Code // On-Off // Reading Keypresses from Port // Shift and Rotate // Simulating Key Presses // Sound // Square-Root Programs // System Flags of TI-OS