0

I'm developing my own virtual 4-bit ALU. I'd like to create calculator (+, -, *, /, %, only integers, maybe more bits). My target is to understand it thoroughly, not to build an efficient device.

Currently I've implemented 4 functions:

  1. Adding.
  2. Adding one.
  3. Positive to negative.
  4. Subtracting.

In all functions, the 5th bit of a result is ignored. I would like to tell the ALU whether the result should be signed or not. What is the best way to do that?

My ideas:

  1. Using 3 more functions (1st, 2nd and 3rd with unsigned integer as a result).
  2. Using one more input byte (to tell whether to ignore the 5th bit or not).

Which one of those are better? Do you know even better method?

2 Answers2

0

You don't need to add more functions if you use addition or subtraction only.

It's common to interpret the numbers different in case of signedness. Normally the highest bit is used to determine if a number is negative or not. In your ALU you can have numbers from 0 to 15 (unsigned) or -8 to 7 (signed) but this depends on how you interpret them. The logic of an addition or substraction is the same and not needed.

For example:

  0111 (7)
 +0001 (1)
 =====
  1000 (8)

or

  1110 (-2)
 +0010 (2)
 =====
  0000 (0)

As you can see the addition works the same way for positive number as for negative. The 5th bit that is unused at the moment can be used as carry for the next addition (to add numbers with higher bit count).

Feyd
  • 38
  • 3
0

While in a register a value is just a set of bits.

One option is that your operators assume that the operands are of certain formats. For example:
- ADD could assume signed big-endian
- UADD could assume unsigned little-endian

Another would be to have a bit in one register set to signify that the ALU is operating in a signed manner or an unsigned manner.

It can also be noted that for addition and subtraction it doesn't actually necessarily matter that the ALU knows if the values are signed or not.

   Binary   :  Signed Meaning  :  Unsigned Meaning
----------------------------------------------------
   1111     :  -1              :  15
   0111     :   7              :   7
1111 - 0111 :  -1 - 7          :  15 - 7
   1000     :  -8              :   8

For addition or subtraction, knowing if the values are signed or not is not needed.

You just need to decide how to handle an overflow.
- 1111 + 0111 = 10110
- In a 4bit ALU that would be 0110, but with an overflow

MatBailie
  • 83,401
  • 18
  • 103
  • 137