# I have a problem with understanding the attached ALU (6 control bits)

I have an Arithmetic Logic Unit, as shown below (I also included a CircuitJS simulation of it, so it will be much easier to understand the inner working of said ALU).

‘x’ and ‘y’ are the 4-bit input nibbles (operands). (The ALU accepts Natural Binary Code as well as U2 negative numbers [Unfortunately I don’t know why, but “it just works…” – my lecturer has not explained it thoroughly, probably because of the lack of time] ); ‘zx’ basically zeroes the entire ‘x’ nibble regardless of what is currently written to it; ‘nx’ negates the ‘x’ nibble (it, however, may be possible to firstly zero the ‘x’ nibble and then negate it to achieve a nibble consisting of ones – 1111).

The same logic applies to ‘zy’ and ‘ny’. The ‘f’ input controls the operation done by the ALU. If ‘f’ is equal to 0, the ALU performs a logical AND operation. If, however, it is equal to 1, then the ALU performs an arithmetical sum operation (there is a full adder inside the ALU). The ‘no’ input simply negates the output of the ALU.

Below I attached an example table consisting of input values and expected results. Now my question is: How can I work out the combination of the inputs ‘zx’, ‘nx’, ‘zy’, ‘ny’, ‘f’ and ‘no’ that will give me these results: -x, x-y, y-x, x+1, y+1? The answers are shown below, in the after-mentioned table.

I understand the logic behind the other combinations, they are pretty trivial to work out (besides the ‘x OR y’, that requires the De’Morgan’s theorem), but I have no idea, how one could work out the required combinations for the mentioned results. I checked the combinations shown in the attached table, and they all seem to work, but I cannot imagine that someone plugged random combinations, observed the output and, using his/her mind worked out what they must have done… It’s pretty hard to wrap my head around this stuff. Thanks for every, even little bit of help :).

The ALU in-question: The table with inputs and expected outputs: Here is the link to a paste that includes the code, which can be used in CircuitJS simulator: ALU simulation code

You can test the ALU by yourself by going to this website: CircuitJS, pressing FileImport From Text... and pasting the copied code.

• Hint: In mod-16 arithmetic, x+15 is the same as x-1. – The Photon Jan 12 '20 at 17:31
• Yes, I know. That's why I didn't colour this column in the attached table. The real trouble begins with these cases: -x, x-y, y-x, x+1, y+1... – KamilWitek Jan 12 '20 at 18:35
• Now think how to generate -x in two's complement. – The Photon Jan 12 '20 at 18:43
• Normally I have to invert all bits and add 1 to get the two's complement, but from the attached table and my experiments it seems that I can also subtract 1 and invert the value. Sadly, no one ever told me this. – KamilWitek Jan 12 '20 at 19:16
• @KamilWitek I haven't read through all your details above, nor thoughts or questions. But reading your above comment I'd suggest that you don't draw overly-broad conclusions from a 1-bit adder with lots of control inputs, as well as data. You may be interested to set up a 4-bit or 8-bit adder and see how that impacts your conclusions. – jonk Jan 12 '20 at 22:15

## 1 Answer

Important is to understand how negative numbers in binary work.

2 bit Two's complement (how to pass from positive 1 to negative 1):

var  binary   decimal
a    01         1
!a    10         undefined with 2 bits
!a+1  11         -1


3 bit two's complement (same as before)

var  binary   decimal
a    001         1
!a    110         -2
!a+1  111         -1


So from here we can see that just inverting the variable/output is the negative/inverse (multiplied by -1) representation of the output -1.

!x = -x-1
!(x+y) = -(x+y)-1 = -x-y-1
!(!x+y) = -(-x-1+y)-1 = x+1-y-1 = x-y
!(!x+!y) = -(-x-1-y-1)-1 = x+y+1


Here is an approach (with thought process) that might help you.

Starting from the output, working your way to the input you can see the following: Output is Q Before the last Mux lets call it Q'

• n0 selects is you want Q' or !Q'

Lets call the last muxer M and the before last M'

• f selects Output from the One bit ADDER or from the AND

Looking at what comes out of the initial MUXs on the left you can see that n(x/y) and z(x/y) select what gets input to the AND and the ADDER.

so given that you are looking for each of the following cases:

" 0-x, x-y, y-x, x+1, y+1 "

you can already conclude that f needs to select the output from the adder, so f=1

y+1, x+1, 0-x only involve one variable either x or y so the other one will be a fixed value either 0 or 1 (+5V)

y+1: --> zx = 1

x+1, 0-x : --> zy = 1

y+1 and x+1 seems as simple additions but you have no way of representing a +1, if you set nx or ny to 1 you will respresent a "-1" , so you can change x+1 to x-(-1)

so after solving the x-y or y-x you should be able to figure that out.

so x - y is adding x to the 2 complement negative representation of y, that is x+!y+1 , but where are you going to get that +1 from?

Think about what inverting the output of the adder will do...If you negate the output of the adder you will get the negative representation of the sum -1

e.g. 2+1 = 3 --> !3 = -4 --> -(2+1)-1

e.g. -2-1 = -3 --> !(-3) = 2 --> -(-2-1)-1

!(x - y) --> -(x-y)-1 = -x + y -1 = y-x-1 = y +(-x) -1

-x --> !x +1

y + !x +1 -1 --> y + !x

so, that defines that for x-y we want

y pass through , ny = zy = 0,

x inverted , nx = 1, zy =0

f = 1 , output of the adder

n0 = 1, adder output inverted

I believe you should be able to figure out the rest from here on.

Cheers, Pau

• Thanks, Your answer has really guided me into the right tracks (I think so), but I still had some problems. Thanks to Your answer, I know when to use ‘zx’, ‘zy’ and ‘f’, but I still had some problems with the rest. Basing on Your answer, I have done some experiments and I figured out this: !x = -x-1 and !(-x) = x-1 . I have observed that adding for example -1 to x we get !(x-1) = -x-1+1, so the signs change… Basing on this now I am able to solve the selected problems; for example: how to get ‘y-x’? I cannot do something like -, so I have to tinker with ‘n_’… Continued below – KamilWitek Jan 17 '20 at 16:54
• Applying it to y, I get: !y+x =(as shown above)= -y-1+x . By negating the output → ‘no’, I get !(-y-1+x) = !(x-y-1) =(as shown above)= -(x-y-1)-1 = -x+y+1-1 = -x+y = y-x. Is this the right way to solve these types of problems? (Sorry, that this may be different from Your answer, but my brain is simply not strong enough to completely understand what You have written, I am not a native English speaker, so this doesn’t help either). Thanks again for Your time and answer, have a good day :). Please excuse me, that posting this message took me so long, I had to think about this... – KamilWitek Jan 17 '20 at 16:54
• @KamilWitek : I'm glad you are starting to see it. I have updated my answer with perhaps a hint/better summary to help you understand. "y-x" : !(x+!y) = -(x-y-1)-1 = -x+y+1-1 = y-x There is no correct way to do it. You have to try to understand the logic behind the circuits, bit by bit and then take the concept from a more abstract perspective byte by byte. It takes time, so don't worry if you don't get it right away.. keep experimenting.. it is how you will learn and figure out how things work.. and maybe even discover new things! – Pau Coma Ramirez Jan 17 '20 at 20:54