2 complement and ALU calculations

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

2 complement and ALU calculations

AlejandroFB
Hello, I already posted this in coursera week 2 forums, but there seems to have less interaction and week 2 ends tomorrow so I copied here to get more insights.

I understand how 2's complement works but still I have some doubs about how we use the representation in the ALU. Considering the same ALU we have built but with 4 bits instead of 16. If we use 2's complement representation as the system for the whole ALU and eventually CPU:

0100 - 1001 = 4 - (-9) = 13 = 0111

0100 + 1001 = 4 + (-9) = -5 = 1011

To do x-y I thought you would change it to x+(-y) and then for -y get the 2's complement representation of the abs value of y but the ALU does not perform any of this logic internally. That would also mean that the input Y is not represented already in 2's complement. Thus, my question:

Does the ALU perform any 2's complement representation conversions or we have to assume that all binary numbers in the ALU (inputs/outputs) and eventually CPU are already in 2's complement and the ALU operates and calculates its operations in that representation?

Thanks,
Alejandro.
Reply | Threaded
Open this post in threaded view
|

Re: 2 complement and ALU calculations

cadet1620
Administrator
Yes, all the ALU inputs and outputs are always 2's-complement.

The ALU does not internally do any explicit 2's-complement operations. The "negation" done by the nx, ny and no control signals is bit-wise Not.

To compute x-y, for instance, the combination nx=1, f=1, no=1 computes
   not( not(x) + y).
Using the definition of 2's-complement, -x = not(x)+1, you can prove that the above is equal to x-y.

There is also a worksheet that can help you see how the ALU works.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: 2 complement and ALU calculations

AlejandroFB
Thanks for the reply cadet1620, I needed to confirm these thoughts with someone with more experience.
Reply | Threaded
Open this post in threaded view
|

Re: 2 complement and ALU calculations

cadet1620
Administrator
Keep asking questions!

An interesting thing that I've learned over the years is how often simply asking a question can get you out of a mental rut. Something about reorganizing one's thoughts to communicate them helps to clarify them.

--Mark