ROL (short for "ROtate Left") is the mnemonic for a machine language instruction which "shifts" the bits in either the accumulator or a specified address in RAM, one bit position towards the "left", or most significant, "end" of the byte. The least significant bit, "left empty" by the shift operation, is "filled in" with the bit held in the carry flag prior to the ROL instruction, and the "excess bit" that gets "pushed out" of the most significant end, is subsequently placed in the carry flag.
If the byte affected by the operation, plus the carry flag, are taken as an unsigned 8-bit binary number (with carry as its single binary decimal, weight ½), the ROL operation effectively doubles the number, leaving a result of up to nine bits, the most significant of which is subsequently held in the carry flag. Doubling signed integers in this way requires programming efforts to keep the sign bit from mixing with the other bits, causing erratic sign-changes.
It is interesting to note that whereas ADC and SBC do not come in "flavours" which do not take an incoming carry or borrow, both ROL and its counterpart, ROR, are complemented by the instructions ASL and LSR, which "fills the vacant bit" with a zero. The "language" of the 65xx CPUs could easily "do without" ASL and LSR; this would merely require programmers to clear the carry prior to shift operations, as is the case when using ADC and SBC. Oddly enough, ASL and LSR were implemented in the very first 65xx CPUs, whereas ROL and ROR was only added in CPUs manufactured after a certain date.
The following example shows how to left-shift, or double the value of, an integer spanning several bytes:
ASL Num Shift least significant byte ROL Num+1 Shift next-to-least-significant byte ROL Num+2 Shift the byte after that ... ROL Num+7 Shift most significant byte
Addressing modes[edit | edit source]
CPU flags[edit | edit source]
- The negative flag is set if the result is negative, i.e. has it's most significant bit set.
- The zero flag is set if the result is zero, or cleared if it is non-zero.
- The carry flag is set or cleared depending on the result.
Examples[edit | edit source]
The 6502 family CPU's rotate instructions are all 9-bit oriented, and do not provide 8-bit as often needed for graphical programming.
For an 8-bit rotate, we must set carry corresponding to bit 7 in advance of the rotate:
; 8-bit rotate left A CMP #$80 ; copies the state of bit 7 to carry! ROL ; bit 7 (the copy in carry) is moved into bit 0
If the operand is in memory: