ROL

From C64-Wiki
Jump to: navigation, search

ROL (short for "ROll 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 was 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]

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]

ROL affects 3 of the CPU's status flags:

  • The negative status 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.