3

I have been learning about programming languages and there is one question which bothers me all the time.

For example let's say that I programmed something which allows me to push a button every 5 seconds.

How does the Computer understand the waiting part(allows to push the button - waits 5 seconds and allows again)?

I already know that first higher programming languages are getting compiled into machine code so that the computer can run it. But if we take assembler for instance, which is very near to machine code, just human readble, there is no instruction for waiting.

The example which I have given with the waiting is just one example, there are much more things which I do not understand how the computer understands ;)

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Whosayin20
  • 81
  • 1
  • 10

2 Answers2

5

Cpu has a quartz timer crystal inside called cpu clock. When a current pass through it, it gives a presice frequency for that current.The Cpu can then use that frequency to keep the track of time.

So computer can understand “do something, wait for 5 seconds and then continue again”

for more info on quartz timer: https://en.m.wikipedia.org/wiki/Crystal_oscillator

Nipun Thennakoon
  • 3,586
  • 1
  • 18
  • 26
2

For short delays on simple CPUs (like microcontrollers) with a known fixed clock frequency, and no multitasking, and a simple one instruction per clock cycle design, you can wait in asm with a "delay loop". Here's the arduino source (for AVR microcontrollers) for an implementation:

https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/wiring.c#L120

As you can see, the behavior depends on the clock-frequency of the CPU. You wouldn't normally loop for 5 seconds, though (that's a long time to burn power). Computers normally have timer and clock chips that can be programmed to raise an interrupt at a specific time, so you can put the CPU to sleep and have it woken up on the next interrupt if there's nothing else to do. Delay loops are good (on microcontrollers) for very short delays, too short to sleep for or even to program a timer for.

You might want to get a little microcontroller-board (not necessarily arduino) to play around with. There you have way less "bloat" from an operating system or libraries and you're much closer to the hardware.

katzenversteher
  • 810
  • 6
  • 13
  • 1
    Desktop / laptop CPUs are different from microcontrollers: the clock frequency in a modern x86 isn't fixed, and superscalar / out-of-order execution makes delay loops extra hard. The core clock even stops when they go into low-power sleep (running no instructions until the next interrupt), but the time counter keeps ticking. Instead of using a delay loop, I'd probably check the time in the button-press handler, and compare it against the next-press-accept time that I calculated earlier. (Last time there was a successful button press.) – Peter Cordes Sep 05 '18 at 06:15
  • 1
    See [How much delay is generated by this assembly code in linux](https://stackoverflow.com/q/49924102) for an example of a delay-loop that works on a modern x86, running under a pre-emptive multi-tasking OS like Linux (so you can't count on your process having the CPU the whole time). You can spin on `rdtsc` or other get-time function or instruction, waiting for the right time. – Peter Cordes Sep 05 '18 at 06:17
  • You're right, I thought it might be good to start simple on a low level. I should have mentioned that modern computers and operating systems use more complex tricks. – katzenversteher Sep 05 '18 at 06:21
  • 1
    The part of the question about something that only lets you push a button every 5 seconds certainly could be asking about a physical device with a single button, with a microcontroller. But 5 seconds is unreasonably long for a delay loop. – Peter Cordes Sep 05 '18 at 06:26
  • 1
    I made a big edit to your answer to describe what it's linking to, and when delay loops are useful. It barely answered the question before, IMO. Feel free to re-edit it and put things into your own words, or keep my wording if you like. – Peter Cordes Sep 05 '18 at 06:33
  • Oh, I didn't even know that was possible. I like your changes, thank you! – katzenversteher Sep 05 '18 at 07:02