I am taking a course of system tools and architecture in my University and the first class is about how CPU and RAM talks and how CPU process the data. As explained by the Prof, CPU has an ALU (Arithmetic Logic unit) which performs arithmetic such as additions and comparisons. But he did not explain how it does it.
I did some searching and found this link which directs to a youtube video that explains how addition is performed - well explained for beginners. Even this link explains that CPU directs arithmetic processing to ALU but does not deal with how ALU performs it.
My question is about how an ALU does comparison. As humans, we know 5 is less than 7.
But how does an ALU know this; is it hard-coded in some way? I am sure it must use some logic for comparisons: what is that logic?
-128 < 1
, but-128 - 1
wraps to127
. That's why, after FLAGS are set by sub or cmp,jl
is taken if SF != OF on x86 for example. (felixcloutier.com/x86/jcc). So sign of result, unless signed-overflow happened, then the opposite. – Enswathe