About.
This article is a modest attempt for answering following two questions:
1. Do computers think?
2. How decisions are made in a Computer Program?
... there's a lot more of related knowledge, answers provided here are just a short introduction to the topic.
Beings & Phenomena.
Buddhist definition is as follows:
def. Beings have Mind, Phenomena don't have Mind. That's how these differ.
Computation & Calculation.
Computers do not think, only calculate & compute.
Calculation is a Mathematical / not only algebra / operation that takes input value(s) and produces result(s),
For example:
- 2 + 2 = 4,
- not true = false,
- cos 60o = 0,5.
Computation is calculation with side effects, as for example:
- Printing an image on a Printer,
- Sending a message via the Internet,
- Saving a file on a USB Pendrive.
Conditional instruction 'if' semantics.
Let's see how 'conditional instruction if ' works internally.
Let's see how processor instruction with assembler mnemonic jne may work.
/ 'jne' is mnemonic for 'jump if not equal' /.
We should also be aware that processors have 'registers', a low-capacity, low amount, high access speed memory cells.
Code:
101. cmp val1 val2;
102. jne :my_section;
103. jmp :my_section_equal;
104. ...
105. ...
(...)
:my_section: 205. ...
(...)
:my_section_equal: 320. ...
(...)
Above code is easy to explain:
1. At line 101, jne compares val1 with val2.
If are equal, 'eq register' / or 'zero flag register' / stores 1 for 'true', otherwise it stores 0 for 'false'.
2. At line 102, if val1 not equal val2, then jne jumps to memory address to which :my_section label points: line 205.
Otherwise, code at line 103 is executed in a next processor step.
How it works?
This 'magic' of conditional instruction is explained with few simple tricks:
1. Compare eq.
eq = (val1 - val2 == 0);
This is logical expression with a boolean value, which in low level evaluates to 1 for 'true', or 0 for 'false'.
This 0 or 1 value is stored in 'eq register'.
2. Jump if not equal.
We consider here: 'instruction pointer register', or in short: 'ip register'.
If 'eq register' has stored 0 - that is, when compare's result is false, then 'ip register' is filled with number, with memory address to which :my_section label points: to line 205.
Otherwise, 'ip register' is increased by 1, to line 103.
3. Next instruction.
Processor continues with next instruction, as pointed by instruction pointer register ... either executing instruction at line 205 or executing instruction at line 103.
3a. Line 205.
Processor continues with instruction at this memory address.
3b. Line 103.
... a 'jmp' instruction is executed, storing in 'ip register' a value of 320.
/ 'jmp' is a mnemonic for unconditional 'jump' instruction /.
4. Summary.
Depending on compare operation's value, we end up jumping to line 103 and then to line pointed by :my_section_equal label, or to line pointed by :my_section label.
That's it, this is conditional branch. We end up executing different code depending on whether val1 is equal with val2 or not.
See also, if You wish: Conditional Instruction & Expressions.
P.S. Please note that memory address and assembly language line are different things.. but are related, and work in a similar way.
No comments:
Post a Comment