The online racing simulator
[OT] How does the CPU know what datatype a varable is.
This is going to sound like a really odd question to ask, but how does the computer know when a data is say an int vs a char. What tells computers that an int is an int (besides it's size). How does the computer know that that datatype is ment to be handled as an interager on the CPU Register level? Is it simply where you place them in the registers, for example ea, eb, ec, etcetera?
#3 - kaynd
In any kind of program (ok mayby not on high level-user friendly programing suites) you define what kind of data you are going to give. But this is way too easy to not be understood by someone who is interested about how a cpu handles that.

Maybe I don't get your question?
#4 - Juls
The instructions the CPU has to perform are specified as a binary code.
Instructions that deal with ints are different from instructions dealing with floats.

So the CPU does not have to recognize what data is. It just execute instructions "stupidly". You can for example store an int in memory, and ask the CPU to perform a float multiply using this int...of course the result will be a total mess as the CPU tries to read the int as a float, but it will run.

This is at CPU level. When you program using a "typed" development language, you have to specify what type a variable is, but you do that to tell the compiler what CPU instructions it should generate at compilation time.
So when you multiply two variables in your code, compiler will generate different instructions for the CPU depending on variable types.
Edit: too slow
So to put it plainly, the CPU does not know, the program does.
#7 - wien
Quote from Dygear :So to put it plainly, the CPU does not know, the program does.

Yes. Or more accurately, the compiler translates operations on ints and chars to the correct assembly instructions for operating on 4 byte and 1 byte integers respectively (on most platforms).
So, the program then get this information just from the compile time knowledge? (In Some, or All Cases)

Or can information be gathered about the data type of a variable from the stack / heap. (In Some Cases.)
#9 - wien
In statically typed languages all type information is available at compile time, so the compiler won't have to figure out any type information at run-time. It knows what instructions to use by the type you give your variables. Dynamic (script) languages like JavaScript and Python obviously work differently as they don't have static types to rely on. In these languages the interpreter has to check what type different variables have before it does any work on them.

The easiest way to figure out this stuff is to look at the disassembly of the code the compiler generates. In Visual Studio (C++) you can do this by setting a breakpoint by the code you're curious about and when execution breaks there, press ALT+8. Not sure how other IDEs do this.

[COLOR=DimGray] int var1(1), var2(2);[/COLOR]
mov dword ptr [var1],1
mov dword ptr [var2],2
[COLOR=DimGray] int result1(var1 + var2);[/COLOR]
mov eax,dword ptr [var1]
add eax,dword ptr [var2]
mov dword ptr [result1],eax

As you can see, when working on ints all mov instructions operate on dwords (4 bytes). These values are moved from memory, into registers. The addition is done, and the result is moved back to memory.

[COLOR=DimGray] char var3(3), var4(4);[/COLOR]
mov byte ptr [var3],3
mov byte ptr [var4],4
[COLOR=DimGray] char result2(var3 + var4);[/COLOR]
movsx eax,byte ptr [var3]
movsx ecx,byte ptr [var4]
add eax,ecx
mov byte ptr [result2],al

The char code looks similar, but all mov instructions now operate on single byte pointers instead. Another difference is the movsx instruction, which in addition to working like a normal mov also converts the single byte var3 and var4 to 4-byte signed integers in the eax and ecx registers. Addition is then done on these 4-byte integers and the first byte of the result is put back in result2.
http://en.wikipedia.org/wiki/Type_system

Quote :Assigning data types (typing) gives meaning to collections of bits. Types usually have associations either with values in memory or with objects such as variables. Because any value simply consists of a set of bits in a computer, hardware makes no distinction even between memory addresses, instruction code, characters, integers and floating-point numbers. Types inform programs and programmers how they should treat those bits.

#12 - Woz
Quote from Dygear :So, the program then get this information just from the compile time knowledge? (In Some, or All Cases)

Or can information be gathered about the data type of a variable from the stack / heap. (In Some Cases.)

The program only know what type of data it expects to be at a given location. You chould put a 3byte string (3 bytes + null termination in C style languages) into the memory location the code expects to be an int32 (4 byte int) and it would be processed as an int 32, not a string.

Not many languages store meta data to describe what the variable type is when you are working in base value types, takes up too much memory, the code is there to tell it what to do.

As for the CPU, it just does the instructions it is told to do blindly.

FGED GREDG RDFGDR GSFDG