int main() {
int a = 3, b = 4, sum;
sum = a + b;
return 0;
}
To understand how this code runs in a computer, we need to look at three main parts: memory, address and data buses, and the processor or CPU. In this example, we are going to talk about three phases.
- When we compile a file, we create an executable file, and when we run this file, the memory reserves space for "a", "b", and "sum", and stores 3 for "a" and 4 for "b".
- When we run the file, the CPU generates a logical address for the variable "a". and this logical address allows access to the physical address. The CPU then sends the logical address via the address bus.
- The MMU (Memory Management Unit) receives the address and changes the logical address to a physical address. The CPU then retrieves the data from memory via the data bus.
- The data is stored in a register, EAX. The CPU then requests the second data, which is the variable "b", by generating a logical address and sending it via the address bus.
- The MMU converts the logical address to a physical address, and the data is sent from memory via the data bus and stored in the register, EBX.
- Note : EAX and EBX are just examples; it depends on your CPU where the data is stored. But in general, data is stored in registers.
- Once the CPU has the data for "a" and "b", the ALU (Arithmetic Logic Unit) performs the operation "a + b" and determines the result is 7.
- After the ALU calculates the result, the CPU asks for the address to store the result. It sends the data (7) and the logical address of "sum" via the address bus and data bus. The data is then stored in the variable "sum".