1

I guess this is just an extension of this.

Now, the compiler defines the datatypes to be integers, signed and unsigned, and floating points so that the processor can use them appropriately.

But say, a processor has an address. Now, a very basic add instruction in RTN would be like
ADD R1, R2 ie R1 <- R1 + R2. Now say register R1 has just been loaded in with a memory address that contained a 32 bit floating point and R2 has an integer.

I want to know, how and when does the processor precisely know, and where is it told by the program(in the instruction format?) exactly that R1 is a floating point, and R2 is an integer?

and one more, say I randomly give out a memory address that I'm allowed to access, and ask the processor to fetch me it's contents. Now, how does the processor know that if that location has a floating point or an integer. So, how does it exactly treat the contents?

Community
  • 1
  • 1
nimbudew
  • 958
  • 11
  • 28
  • 1
    It doesn't. If you have 32-bits of data, there's no intrinsic way to know whether those 32-bits should be interpreted as an integer or as a floating point number. – Damien_The_Unbeliever Jan 01 '13 at 19:28
  • Okay, then it just treats everything like a 2's complement integer. Then only the compiler can generate instructions that it's a floating point and stuff when WE define the datatypes? – nimbudew Jan 01 '13 at 19:29
  • 4
    No, it doesn't even treat them as 2's complement. It executes an instruction on the operands. – Femaref Jan 01 '13 at 19:30
  • There are different instructions (and registers) for integer and floating point arithmetic. The processor can never be asked to add a float and an int. – Damien_The_Unbeliever Jan 01 '13 at 19:31
  • Also, what you're talking about seems to be rather language- than CPU- or architecture-dependent. –  Jan 01 '13 at 19:32

2 Answers2

3

It knows, because you tell it. On ia32 processors, the ADD instruction adds integers. FADD adds floating point numbers (see instruction reference). To add a float to an integer, the float must be converted to an integer, or the integer into a float.

Phil Frost
  • 3,668
  • 21
  • 29
  • OH! i guess that solves everything! It's all instruction dependent. The processor get to know abt how to operate on the operands and the type of the operand from the instruction. Thanks! – nimbudew Jan 01 '13 at 19:34
1

It doesn't. A processor simply executes the instruction on the registers or memory addresses specified.

Femaref
  • 60,705
  • 7
  • 138
  • 176
  • Then how does the processor perform floating point arithmetic? and in the above case, say i wanted the result to be right, ie float + integer. then, how do i get the right result? – nimbudew Jan 01 '13 at 19:32
  • @jaskirat In that case, your compiler emits code that a. converts the integer to a float, then b. it tells the ALU to add the two floats. –  Jan 01 '13 at 19:32
  • It performs floating point arithmetic when you use the appropiate instruction. You can't tell the processor to add an int and a float. as the processor has no idea what an int or a float is. Any conversion between the two happens before you actually execute an addition. – Femaref Jan 01 '13 at 19:33
  • Ok! Yeah, i got it. It's all in the instruction. But one more, if i just randomly fetch a memory word, what should I expect? an integer? and in 2's complement, if the architecture is storing that way? – nimbudew Jan 01 '13 at 19:36
  • @jaskirat the "2's complement" is not a way for storing data. It's a method for expressing negative numbers. You have RAW BITS, 8 or 16 or 32 or 64 or however many the register supports. –  Jan 01 '13 at 19:37
  • @jaskirat - if you *ask* for 32 bits, you'll *receive* 32 bits. How they should be *interpreted* is entirely within the realms of your own head. – Damien_The_Unbeliever Jan 01 '13 at 19:39
  • ok, so when i fetch the word, I get the raw bits. Then, there is no way for me or the processor to tell that it's a float or an int. Am I right? – nimbudew Jan 01 '13 at 19:40