Fetching/decoding variables

Status
Not open for further replies.

Thread Starter

LiamCorcy

Joined Feb 14, 2019
4
Hello guys;

when I have something like *(x+1)=5; which at x's variable saved an address of memory(lets assume it's 100) ; then the PC will put 5 into address 101 .. and it's fine; but what I'm wondering about is, how does the pc know to when to fetch the variable x or not fetching it ? and when to assign instead of x its value or when not ? looks weird.
 

Papabravo

Joined Feb 24, 2006
12,768
It is the parentheses and the precedence of the operators +. * and =.
= is the lowest precedence and happens last
* is next to last because it is outside the parentheses
+ is first because it is inside the parentheses

Rewriting the expression in Reverse Polish Notation, as is done in HP calculators, we have
x 1 + * 5 =
which unambiguously shoes the order of operations without the need for parentheses.
 
Last edited:

djsfantasi

Joined Apr 11, 2010
5,955
It depends on the operation in which the variable x appears. But it should be clear.

The value is retrieved when it is needed for a calculation or function.

For example, in the expression (x+1), the value is needed in order to perform the calculation. When x appears to the left of an equals sign, it is assigned.

In the example sin(x), x is retrieved in order to calculate the sine.

In some cases, it may not be onbvious. The C statement x +=1, both retrieves and assigns the value of x. It becomes more clear when you realize that the above statement is shorthand for and equivalent to x=x+1.
 

Thread Starter

LiamCorcy

Joined Feb 14, 2019
4
It depends on the operation in which the variable x appears. But it should be clear.

The value is retrieved when it is needed for a calculation or function.
I'm totally with you, but who's manger of dealing with that? CPU or PROCESSOR? I mean lets assume I'm a human yeah know I must retrieve for ... , but as computers who's manager for that decisions? OS?
 

Papabravo

Joined Feb 24, 2006
12,768
I'm totally with you, but who's manger of dealing with that? CPU or PROCESSOR? I mean lets assume I'm a human yeah know I must retrieve for ... , but as computers who's manager for that decisions? OS?
It is the compiler, that understands both the syntax and the semantics of the statement. The OS has nothing to do with it, and the CPU is involved only because it is sequentially executing a stream of instructions that are output by the compiler or interpreter as a result of processing the statement.
 

djsfantasi

Joined Apr 11, 2010
5,955
Oh, this gets complicated. Let’s see if I can summarize the operation of a computer.

The CPU only knows how to do a few operations. Add, subtract, multiply, divide, retrieve a memory location, store to a memory location, etc... the language used to invoke these operations is called machine language. It encodes each basic operation of the CPU into a binary pattern.

Numbers are difficult to read when writing software. So the Assembler was born. The assembler interprets defined abbreviations or mnemonics into its machine language binary pattern.

The OS runs these programs, and manages several sub-programs which control the peripheral devices. Like a keyboard or a USB port.

Reading a page of mnemonics isn’t easy either. High level languages were developed which were closer to natural language and simplified many operations. Think For loop. In Assembly, the For loop would take many instructions.

The CPU cannot understand a high level language, as it cannot understand Assembly. So another piece of software, the compiler, interprets the language into machine language.

So finally to address your question. The software determines when to fetch or assign the value of x. Ultimately, the software goes through all of these cycles, to tell the CPU what to do.
 
Status
Not open for further replies.
Top