0
\$\begingroup\$

A while ago, I was reading the book Some Assembly Required on programming assembly for the avr family of microcontrollers... Early on, it explained that the LSL ( logical shift left ) instruction was translated to an ADD instruction that essentially multiplied the register by 2. I understand why that would work, but what I don't understand is what I am reading about the LSR ( logical shift right ) instruction on Atmel's own documentation: http://www.atmel.com/webdoc/avrassembler/avrassembler.wb_LSR.html

The documentation displays the instructions opcode as, 1001 010d dddd 0110 and shows the cycle count as 1, meaning that the instruction is completed in a single clock cycle.

So, what exactly is happening here? I really can't tell.. Thanks!

\$\endgroup\$
  • 1
    \$\begingroup\$ you might wanna look up what a barrel shifter. \$\endgroup\$ – robert bristow-johnson Jun 9 '17 at 3:51
  • \$\begingroup\$ Notice that you can easily hardwire a single-bit shift by just copying input bit#0 to output bit#1, bit#1 to bit#2, &c. So the complexity of the single-bit shift is the same as that of a copy from one register to another. \$\endgroup\$ – JimmyB Jun 13 '17 at 14:41
  • \$\begingroup\$ That is what a multiplexer is used for right? From what I read, that is what it sounds like. \$\endgroup\$ – Holden Jun 14 '17 at 19:11
0
\$\begingroup\$

Look up 'barrel-shifter'. That's pretty much the only way to do a right shift.

There are at least two ways to do a left shift, add or a barrel shifter.

Re-use of add for a left shift allows the barrel shifter to be simpler, so saves hardware.

\$\endgroup\$
  • \$\begingroup\$ even though i knew that a left shift of one bit can be accomplished with an adder, i didn't know that, say on the microcode level (where the latches and adders and shifters and other logic live) they would implement ASL with the adder and not just pipe the bits over one to the left. \$\endgroup\$ – robert bristow-johnson Jun 9 '17 at 5:27
  • 1
    \$\begingroup\$ I always notice the word 'just', which usually means the person using it doesn't appreciate the potential cost. When you 'just' pipe the bits over to the left, you've added another set of inputs to the multiplexers in the barrel shifter. This might be a small hardware change, or it might kick the mux design into the next cell size, so be very expensive. If OTOH the microcode alteration to use the add is cheap or even free, then that will save a few gates from the final implementation. Why save a few gates? Maybe that kept the MCU in a smaller die size? Who knows. I'm sure there was a reason. \$\endgroup\$ – Neil_UK Jun 9 '17 at 5:53
  • \$\begingroup\$ i agree that saving a few gates (when this few gets multiplied by a larger number) is a desirable design goal. but using the adder might affect the condition codes (such as the Carry bit or the Negative bit or the Two's-Complement Overflow bit) differently than implementing the shift directly. and if shifting with displacement exceeding one bit is implemented, it seems to me that the multibit LSL and LSR will be symmetrically. and ASR and ASL the same way, except ASR will copy the sign bit and shift that in from the left, rather than zero. ASL and LSL might be the same. \$\endgroup\$ – robert bristow-johnson Jun 9 '17 at 7:31
  • 1
    \$\begingroup\$ A barrel shifter will not necessarily be the way that a compute engine will perform a single bit right shift. The barrel shifter generally comes into play in a design where the assembly language instruction set includes instructions to shift the operand a specified number of places rather than just one place. If you look at almost all of the simpler instruction sets around you will note that they only support one bit at a time shifts. I would suggest that the ALUs in these simpler devices use a simple MUX circuit to provide the right shift function (continued) \$\endgroup\$ – Michael Karas Jun 9 '17 at 10:23
  • \$\begingroup\$ (continued from above) instead of implementing a barrel shifter. It will be interesting to note that one of the first ALUs, the 74181, which was a 4-bit wide cascadable unit, did not directly support a right shift and if such was needed extra logic external to the ALU was required. \$\endgroup\$ – Michael Karas Jun 9 '17 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.