Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LC3 Assembly Bitwise Right Shift

What I need to do it implement both a bitwise left shift, and a bitwise right shift using LC-3 Assembly. Basically, every bit has to be moved over one space in the direction of the shift, and a zero fills the empty space created.

Examples:

Right Shift:

 01001001
 00100100→

Left Shift:

 01001001
←10010010

I've successfully implemented a left shift, by taking the binary string, and adding it to itself.

I'm stumped on how to perform a right shift. Any thoughts would be greatly appreciated. I have AND, NOT, ADD operations, data movement operations, seven registers to store values and whole range of memory. I just need some basic ideas how it could be implemented.

If you need a LC-3 Instruction Set reference, there is one here.

like image 859
Will Haynes Avatar asked Apr 09 '12 18:04

Will Haynes


People also ask

How do I use bitwise right shift?

The signed right shift operator '>>' uses the sign bit to fill the trailing positions. For example, if the number is positive then 0 will be used to fill the trailing positions and if the number is negative then 1 will be used to fill the trailing positions. In Java, negative numbers are stored as 2's complement.

What is a bitwise Shift Right?

The bitwise shift operators are the right-shift operator ( >> ), which moves the bits of an integer or enumeration type expression to the right, and the left-shift operator ( << ), which moves the bits to the left.

Which bitwise operator is used to shift right a bit?

The right shift operator ( >> ) returns the signed number represented by the result of performing a sign-extending shift of the binary representation of the first operand (evaluated as a two's complement bit string) to the right by the number of bits, modulo 32, specified in the second operand.

What does bit shifting by 0 do?

1 in binary is 0001 , then bitshifting it by 0 won't do anything, which aligns with what you observed. So any number x << 0 is equivalent to x * 2^0 , which is x * 1 , which is just x .


2 Answers

Suppose you set up R2 so that it has just a single bit set. Then, if you do an AND with another register and branch on the Z condition, you are testing whether that bit is set. If it is, you want to set the previous bit in your "result" register.

If you then shift your single-bit register over one place and repeat in a loop, you should have what you need.

(Apologies if this is vague; since this is presumably homework I'm trying to avoid just giving you the answer)

Edit:

So, suppose your input is 01001011. You start with an output of 00000000, an input mask of 00000010, and an output mask of 00000001. You do the AND and find that it is nonzero, so you add your output mask to the output. Then you shift both masks over to get 00000100 and 00000010.

On the next time through the loop, the AND is zero, so you add nothing, and so forth. The loop terminates when shifting the mask makes it zero.

like image 121
Russell Zahniser Avatar answered Sep 28 '22 17:09

Russell Zahniser


Wow, that's quite a minimal instruction set.

If you have 256 bytes of memory available, then a lookup table might be the way to go.

You could do it without data memory using a loop over each bit position, using AND to extract the bit.

like image 27
Oliver Charlesworth Avatar answered Sep 28 '22 18:09

Oliver Charlesworth