HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

How is a program executed at the CPU level?

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
thelevelprogramexecutedhowcpu

Problem

I know this is a very common question. But I have a different angle in my mind. I will just try to articulate it here.

From what I know, every instruction that a CPU executes, is in machine language and all CPU can do is do some arithmetic operations thanks to ALU and to its transistors (if we go at hardware level).

However this is easier to type than to comprehend it. So if all CPU does is adding, subtracting, etc., then how is a program, say a JAVA program saying print Hello World, executed with these arithmetic operations?

I mean how is this program converted into something that is just an addition for the CPU?

P.S. If this question is not applicable for this website then I apologise.

-----Part TWO-----

Okay. Thanks to all for answering this fast and with this enthusiasm. I thought its better to modify my question a bit than to go and comment to all answers and ask them again.

So here it is.

First, all have answered specifically w.r.t example of Hello World. This is my fault. I should have kept this generic. Hello world example brings in question of output devices and how its processing is not just limited to the CPU, which is rightfully brought up in your answers.

Also many of you brought to my notice that CPU does more than just addition. I agree with that. I just didn't write that and assumed it all the way.
From what I understand, this is the process:

-
read the instruction from memory (using data and address buses and program counter stuff)

  • store data in register inside CPU



  • Now ALU does arithmetic operations, of course after decoding the instruction, or take a jump if its an if like instruction



  • And then communicating with other resources if needed like with output device and so on. Processes beyond this are trivial for now.



So in step3 where CPU decodes an instruction and decides to do an arithmetic operation(here we are assuming that there is no other operation to be done like jump the current instruction..since arithmetic operations a

Solution

You can try taking a simple program and compiling it to native machine code. (Java normally compiles to JVM code, but Andrew Tennenbaum has a book where he describes how to design a CPU that runs that natively, so it will do.) On GCC, for example, you give the compiler the -S switch.

This will tell you that anything tricky, like I/O, is implemented by calling the operating system. While you could download the source to the Linux kernel and do the same to it, what's going on under the hood is: everything is manipulating the state of the computer's memory, for example the list of running processes, or else talking to hardware by using special memory addresses that control it or using special CPU instructions like in and out on the x86. Generally, though, only special programs called device drivers will talk to particular hardware, and the OS will send requests to use hardware to the right driver.

Specifically, if you print, "hello, world!" your compiler will turn that into a set of instructions that loads the string into a particular location (for example, loading the address of the string in memory to the %rdi register) and calling a library function with the call instruction. This library function might find the length of the string with a loop, then call the system call write() to write that number of bytes from the string to file descriptor number 1, which is standard output. At that point, the OS looks up what that process' file number 1 is and decide what writing to it means. If writes to standard output get printed on your screen, there will be some process like copying the bytes to a buffer, which is then read by your terminal program, which tells the windowing system which letters to put where in what font. The windowing system decides exactly what that should look like, and tells a device driver to put the pixels on the screen, which it does by changing video memory.

Context

StackExchange Computer Science Q#47410, answer score: 7

Revisions (0)

No revisions yet.