By that I mean, is there a circuit that can compare two signed words against one another, but also be able to compare two unsiged words together, not comparing a signed word to an unsigned one.
I am currently working on a 4bit ALU and I intend on making it support operations for both signed and unsigned inputs. But to the extent of my knowledge, the 2's complement representation of signed words means that the only way to make it work is just to have one comparator for unsigned and one for signed inputs.
Is there a single circuit that can perform those comparisons?
-
1\$\begingroup\$ With the right status bits available, and given that you know which inputs are signed and which are unsigned, all signed and unsigned operations can be carried out with a basic ALU that doesn't know or care about which is which. \$\endgroup\$jonk– jonk2020年05月05日 03:48:37 +00:00Commented May 5, 2020 at 3:48
-
1\$\begingroup\$ The ALU does not know what type of numbers it is comparing. It just looks at the input bit patters and produces a result. It is the job of the user of the ALU to give it numbers of known type to compare and look the result based on what type of numbers they were. \$\endgroup\$Justme– Justme2020年05月05日 05:10:42 +00:00Commented May 5, 2020 at 5:10
1 Answer 1
There is no circuit with a single output that can do greater/equal/less comparisons on both signed and unsigned values without some input that specifies whether the operands should be interpreted as signed or unsigned.
One approach is to subtract one operand from the other and look at two important carries: the carry into the MSB and the carry out of the MSB. Depending on how your subtractor works you may see different behavior from these two signals when there is an underflow for signed vs. unsigned operands.
You can also look at the sign of the result compared to the signs of the operands. For example, suppose you compute \$A - B\$ with two unsigned operands. You can zero extend the operands and then perform the subtraction. If the result is negative then \$B > A\$. If the result is zero then \$A = B\$. Else, \$A > B\$.
This sounds like a homework question so I'll leave you to work out the details. Even if it's not homework, it's a great learning opportunity.
-
\$\begingroup\$ It's not homework, I just do it as a hobby, but thanks for your answer! I suspected I could just look at the sign of the difference, but I didnt know about the carry thing. \$\endgroup\$Thomas.M– Thomas.M2020年05月05日 11:45:51 +00:00Commented May 5, 2020 at 11:45
-
\$\begingroup\$ Just coming across this very helpful answer as I study from Harris and Harris's Digital Design and Computer Architecture (as you can see from my post history, this is not homework). I was going to make another question but am hoping it's OK to make the following comment/question. Suppose we are interested in making a combined signed vs. unsigned comparator (some signal comes into our block, \$sign\,ドル which is 1 if signed and 0 otherwise. To ensure I understand your answer, would we implement said block as follows: extend both operands by one bit, using a MUX to either... \$\endgroup\$EE18– EE182024年02月24日 22:35:02 +00:00Commented Feb 24, 2024 at 22:35
-
\$\begingroup\$ ... select 0 if \$sign = 0\$ (i.e. select \$sign\$) or else select from the MSB of the operand (sign extension) if \$sign = 1\$. Then we go in, do the \$N+1\$ bit subtraction \$A-B\$. Let's define our output as \$A>B\$. In terms of "processing the outputs", we take the complement of the MSB (the \$N+1\$th bit) of the output if \$sign = 0\$ or the usual result for the signed case (HH give this as Exercises 5.9 and 5.10), again under a MUX for the output from the block with \$sign\$ as the select. \$\endgroup\$EE18– EE182024年02月24日 22:37:56 +00:00Commented Feb 24, 2024 at 22:37
-
\$\begingroup\$ I have a lot of extra hardware around the subtractor here so I wondered if there was a more clever way to do it. \$\endgroup\$EE18– EE182024年02月24日 22:39:45 +00:00Commented Feb 24, 2024 at 22:39