Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the 6502 microcontroller not have a arithmetic right shift?

I'm trying to understand the instruction sets of old microcontrollers, especially the 6502.

The documentation of the instruction set that can be found here lists two shift instructions (beside the rotate instructions):

ASL - arithmetic shift left

LSR - logical shift right

Why are there no arithmetic shift right and logical shift left instructions?

I know that one can simply add a number to itself to logically shift it to the left, but a dedicated instruction would be much more convenient and also faster, even if it does just that. But a missing arithmetic shift right instruction does not make sense to me.

like image 492
uzumaki Avatar asked Dec 14 '22 13:12

uzumaki


1 Answers

I've always thought that particular pair of instructions to be a bit of a con. There are no arithmetic shifts at all on the original 6502. Arithmetic shifts preserve the sign of the number being shifted and the so called ASL only does that by virtue of the fact that the 6502 uses 2's complement.

The original 6502 only has two shift operations: shift right and shift left with bit shifted out going to the carry flag and the bit shifted in being set to zero. For shifts left with 2's complement, there is no distinction between arithmetic and logical. However, for shifts right, an arithmetic shift has to preserve the sign bit. This means, to do it, you would need circuitry to propagate the top bit of input into the shifter to the top bit of the output and an extra control line to select between that and 0. These all require more transistors and an arithmetic shift can be simulated with a rotate.

    CLC
    LDA number
    BPL positive
    SEC
positive:
    ROR A

The primary driver behind the design of the 6502 was to make it outperform the Motorola 6800 at a lower price. This was done by stripping inessential features out of the 6800 so they could make the transistor count as low as possible. The 6502 has a transistor count of around 3,500. The earlier 6800 has a transistor count of around 4,500. Inessential features like a proper arithmetic shift were therefore left out.

In fact, given that shifts can be emulated by clearing the carry flag and then doing a rotate, I'm surprised they bothered with them at all. The designers must have found they could add the shift instructions with a minimal increase in complexity, much like the SBC instruction is exactly the same as the ADC instructions but with the bits of the second operand inverted.

like image 167
JeremyP Avatar answered Feb 23 '23 00:02

JeremyP