How does a CPU know if an address in RAM contains an integer, a pre-defined CPU instruction, or any other kind of data? -
the reason gets me confused addresses hold sequence of 1's , 0's. how cpu differentiate, let's say, 00000100
(integer) 00000100
(cpu instruction)?
first of all, different commands have different values (opcodes). that's how cpu knows what do.
finally, questions remains: what's command, what's data?
modern pcs working von neumann-architecture ( https://en.wikipedia.org/wiki/john_von_neumann) data , opcodes stored in same memory space. (there architectures seperating between these 2 data types, such harvard architecture)
explaining everything in detail totally beyond scope of stackoverflow, amount of characters per post not sufficent.
to answer question few words possible (everyone working on level kill me shortcuts in explanation):
- data in memory stored @ addresses.
- each cpu advice consisting of 3 different addresses (not values - addresses!):
- adress what do
- adress value
- adress an additional value
so, assuming addition should performed, , have 3 adresses available in memory, application store (in case of 5+7
) (i used "verbs" instructions)
adress | stored value 1 | add 2 | 5 3 | 7
finally cpu receives instruction 1 2 3
, means add 5 7
(these things order-sensitive! [command] [v1] [v2])... , things getting complicated.
the cpu move these values (actually not values, adresses of values) registers , processing it. exact registers choose depend on datatype, datasize , opcode.
in case of command #1 #2 #3
, cpu first read these memory addresses, knowing add 5 7
desired.
based on opcode add
cpu know:
- put address
#2
r1 - put address
#3
r2 - read memory-value stored @ address stored in r1
- read memory-value stored @ address stored in r2
- add both values
- write result somewhere in memory
- store address of put result r3
- store address stored in r3 memory-address stored in r1.
note simplified. cpu needs exact instructions on whether handling value or address. in assembly done using
- eax (means value stored in register eax)
- [eax] (means value stored in memory @ adress stored in register eax)
the cpu cannot perform calculations on values stored in memory, quite busy moving values memory registers , registers memory.
i.e. if have
eax = 0x2
and in memory
0x2 = 110011
and instruction
mov ebx, [eax]
this means: move value, stored @ address, stored in eax
register ebx
. finally
ebx = 110011
(this happening everytime cpu single calculation!. memory -> register -> memory)
finally, demanding application can read predefined memory address #2, resulting in address #2568 , knows, outcome of calculation stored @ adress #2568. reading adress result in value 12
(5+7)
this tiny tiny example of whats going on. more detailed introduction this, refer http://www.cs.virginia.edu/~evans/cs216/guides/x86.html
one cannot grasp amount of data movement , calculations done simple addition of 2 values. doing cpu (on paper) take several minutes calculate "5+7", since there no "5" , no "7" - hidden behind address in memory, pointing bits, resulting in different values depending on bits @ adress 0x1 instructing...
Comments
Post a Comment