I'm creating a 16-bit ALU that has to perform logical AND, logical OR, addition, subtraction and rotate left one bit. I have everything working perfectly except the rotate left one bit. I think I need to implement this at the 16-bit level instead of inside the 1-bit ALU, which I've chained together to create the 16-bit ALU. Any advice on how to do this would appreciated. I'm thinking it has something to do with the carry out bit, but I can't quite figure it out.
1 Answer
The simplest thing is to just add the input number to itself. Doubling a number (multiplication by 2) is equivalent to a left shift in binary. In other words, select ADD as the ALU operation, and feed the input value to both inputs of the ALU. If you do add-with-carry, then the carry bit will automatically get shifted into the LSB. In either case, the carry-out will be the MSB of the original input number.
-
\$\begingroup\$ That would be a left shift not rotate, or 17-bit rotate-through-carry, like x86
rcl
. A 16-bit rotate left (like x86rol
) would need to feed the carry-out directly into the low bit, as well as into the carry flag. (To be fair, some ISAs only have rotate-through-carry and just call it rotate, e.g. AVR where it uses the ADC opcode.) And yes,adc same,same
is equivalent torcl reg
by 1. To emulaterol reg
, you couldadd reg,reg
/adc reg,0
to left shift and then add the carry-out to the zero you shifted in. \$\endgroup\$ Oct 12, 2020 at 12:47 -
1\$\begingroup\$ @PeterCordes: If that's what you want, it just takes a few more gates to route the MSB of the input directly to the carry-in of the ALU. Or you could simply run the two-instruction sequence "add register to itself" followed by "add-with-carry #0". \$\endgroup\$ Oct 12, 2020 at 13:06
-
\$\begingroup\$ Yes, agreed. But since that wasn't obvious to the OP, it's probably best to be clear about exactly what rotate you're implementing. \$\endgroup\$ Oct 12, 2020 at 13:10
-
\$\begingroup\$ @PeterCordes: Actually, the answer is intentionally vague, just like the OP's question is. Without details about the actual ALU circuit or the datapath in which it is embedded, it's difficult to be specific without creating possible confusion. I'm just providing a hint about how the ALU's carry logic can be used to shift the data to the left, just as the OP suspected. \$\endgroup\$ Oct 12, 2020 at 14:09
x + x
. However a more useful function is a right shift which cannot be emulated by a subtraction. \$\endgroup\$