As the name suggests, the right (logical) shift operation is the opposite of the left shift operation. Every bit in the word is shifted one place to the right, and the leftmost bit becomes a zero. For example:
1111111001111111 becomes 0111111100111111 1010111000011011 becomes 0101011100001101
We call this a right logical shift to distinguish it from a right arithmetic shift, which reads the word as a signed integer and divides it by 2. Notice that if we read the word as a positive integer, then a right logical shift and a right arithmetic shift are the same thing!
Write a Hack assembly program that performs the left rotation operation on the word in RAM[0]
a total of RAM[1]
times, and stores the result in RAM[2]
. You may assume RAM[1]
is at most 15, and you don’t need to optimise when
RAM[1]
≥ 16. You may want to use your answer for the left shift exercise as a base.
Hint: By far the easiest way of doing this involves starting with a right rotation! Left and right shifts are very commonly-used bitwise operations and are also relatively easy to implement in hardware, so most CPUs include them as instructions — the only reason Hack doesn’t is to keep the ALU design simple.