-3

I want to know how a computer knows if a condition is true or false. For example, in assembly the instuctions

SUB EAX, EBX
JNZ not_equal

will check if the registers are not equal (Check if the zero flag ZF is 0).
ZF = 1 means that there is an electricity (this bit is on), but I dont understand how do the computer know how to do a logic (conditional) jump.
I am assuming that the answer is something related to the way the electricity works in the cpu, but I would like to know what exactly is.
Hope that the question is clear enough...
If not, please just tell me all you can about conditional instructions in a cpu.

Mor Cohen
  • 9
  • 1
  • The question seems too vague. What exactly do you want to know about how a conditional jump is implemented? Jumps work by manipulating the program counter (instruction pointer). Similarly, you can have other conditional arithmetic operations; for example, a conditional add. Such instructions are called "predicated" and are common in e.g. Itanium architecture. – Oleksandr R. Apr 28 '16 at 15:27
  • May I suggest: http://electronics.stackexchange.com/questions/21838/how-does-a-cpu-work?rq=1 – dim Apr 28 '16 at 15:28
  • To be clear, I have been looking for the thing that makes logical operations between 2 operands. The given link is talking about logical gates, and I think that is what I have been looking for! – Mor Cohen Apr 28 '16 at 15:30

3 Answers3

1

CPU logic is very complicated and varies between architectures but your question can be understood at the logic gate level. The CPU has a register called the instruction pointer (IP) which contains the address of the next instruction for the CPU to fetch from memory. Normally, after each instruction there are some logic circuits that increment the instruction pointer so that it points to the next instruction. When the CPU gets a jump instruction, instead of incrementing the instruction pointer, there are some other logic circuits (NAND gates) that check the state of the appropriate flags. If they determine that a jump needs to happen, instead of incrementing the instruction pointer, the address of the jump gets loaded into the instruction pointer. This makes the CPU fetch the next instruction from that new address. The address is usually given as part of the jump instruction.

So to answer your question, the way that the computer "knows" to do a jump is because the desginers of the chip designed the circuits to behave that way. If you want to learn more about how the internals of a CPU works, I highly recommend the book But How Do It Know?. It has a complete explanation of a simple CPU including all the logic circuits. Real world CPUs are much more complicated and include things like branch prediction and multithreading, but that book will give you a good idea of what's going on inside the silicon.

Robert Stiffler
  • 984
  • 5
  • 9
1

this one is not easily ansered in short. But I try:

The basic building block of a mcu is a "Logic Gate". These come in differrent flavours: AND / OR / NOT / XOR etc. see https://en.wikipedia.org/wiki/Logic_gate. The other one is a Memory cell.

Depending on the technique your cpu uses these can be build with transistors / mosfets etc.

For simplicity just imagine an AND gate to be one PNP transistor whose EMITTER only becomes high when there is current applied to both BASE and COLLECTOR.

Now to your program:

Your first instruction is sub which subtracts one value from another. If both values are the same, the target register only contains zero. So (for a 8-bit register) there are 8 memory cells containing zeros or which "don't contain current" / are "connected to ground".

Now using the logic gates you can OR these memory cells and end up with one output which is either high (if any of them is high) or low (if all of them are low.) This value is transfered (say connect output of this "wire" to input of memory cell) to the status register as Z-Bit.

Now to your second instruction: The conditional jump.

First the condition: This could be just one other gate which connects to Z-Bit memory cell.

Now the jump: The instruction pointer of your cpu is just another memory cell. But all we need is transfer the value of your jump target to that memory cell using some AND-gates.

So that's it?

Not really. It is much more complicated. We haven't talked about clocks, ALUs and much more stuff. But this should be the very basic operations so that you can imagine something which is at least not completely wrong ;)

EDIT: If you want to know more or just to prove that it is not so much more complicated have a look at this: https://hackaday.io/project/665-4-bit-computer-built-from-discrete-transistors

Scheintod
  • 187
  • 1
  • 10
0

You don't need to worry, at this level, how electricity works in a CPU, or whether a CPU 'knows' anything.

If you want to build a CPU from transistors, then knowing how electricity works would be a Good Thing.

At this level, presumably you are happy that a CPU has registers, can address memory, can decode instructions, and all of these can be considered to be vectors of 'bits', or binary logical values.

The Zero flag, or the Carry flag, is just another bit, that the address decoder takes into account when deciding where to fetch the next instruction from. If the instruction has a true bit in the 'check conditional' part of the instruction, and the CF or ZF is set, then the address decoder fetches the instruction at the branch. If not, it fetches the next instruction.

Neil_UK
  • 166,079
  • 3
  • 185
  • 408