How do you define “if” for computers?

419 views

I’m not a programmer, how do you get from binary 1s and 0s to “if x occurs complete y” How do you tell the computer to wait for a future input?

In: 2

11 Answers

Anonymous 0 Comments

The CPU is hard-wired to read instructions and execute them. It does that because it’s designed to do that using physics. Programming consists of designing the instructions for the CPU to read. One kind of instruction might be “wait for X” which tells the CPU to wait until X happens.

In reality there are no “wait for X” instructions, but there are “wait for *something*” instructions and then the program can look at which kind of thing happened, and make the appropriate response.

There are multiple ways that can be achieved. A simple one is for the CPU to have a “something happened” ire that connects to all the peripheral devices (mouse, keyboard etc). When something happens that the CPU/program might need to react to, the peripheral device signals that wire, then the CPU somehow activates* the part of the program that deals with things happening, and checks each device in turn: “Ooh, something happened. Was a key pressed? No. Did the mouse move? No. Did the hard disk finally finish reading that data I asked for *ten milliseconds ago*? No. Is the screen refreshing? Ooh, the screen is refreshing. I’d better do that stuff I need to do when the screen refreshes.”

* The “somehow” is called an *interrupt*. One of the instructions in the program says “hey, whenever something happens, go to step 385744” or whatever. The CPU remembers that number, and when something happens it goes straight to step 385744 in the program, *interrupting* whatever it was doing before that.

You are viewing 1 out of 11 answers, click here to view all answers.