ROL
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]
Opcode | Addressing mode |
Assembler format |
Length in bytes |
Number of cycles | |
Dec | Hex | ||||
42 | 2A | Accumulator | ROL A | 1 | 2 |
46 | 2E | Absolute | ROL nnnn | 3 | 6 |
62 | 3E | Absolute,X | ROL nnnn,X | 3 | 7 |
38 | 26 | Zeropage | ROL nn | 2 | 5 |
54 | 36 | Zeropage,X | ROL nn,X | 2 | 6 |
ROL supports the five addressing modes shown in the table at right. In the assembler formats listed, nn represents a single-byte (8-bit) figure, and nnnn is a two-byte (16-bit) address.
CPU flags[edit | edit source]
ROL affects 3 of the CPU's status flags:
- 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]
8-bit rotate
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:
; 8-bit rotate left Value LDA Value ASL ROL Value