Wednesday, June 17, 2009
The quiz show
I just started watching this interested JDrama.Its called "The quiz show".It has a similar concept with liar game , both work on psychology.Guest are invited to the quiz show every episode to expose their dark secrets in front of a live broadcast and audience.The MC has lost his memory and the director seems to know everything :)
Started with abit of revision yesterday.I was pretty curious about signed and unsigned yesterday so I dropped Dr fred an email.He ended up telling me to post it in the forum instead.I am glad I asked that question because I have trouble understanding it although I know when to apply it according to situation.
Quoted Dr fred
Your question relates to instruction encoding.
Immediate values need to fit into an instruction along with all the other information about what an instruction is supposed to be doing.
addi, addui, subi, subui, etc. only have 16 bits available to store the number. However, we usually need the value to be turned into a 32 bit value before it is used. So the question is, what value should be placed in the top 16 bits of the number?
The unsigned instructions, addui, subui, etc. just place 0s in the top 16 bits.
The signed instructions addi, subi, etc assume that the 16 bit value is a 2's complement number and duplicate the sign bit, either all 1s for negative numbers or all 0s for positive numbers. This means that a 16-bit 2's complement number gets converted into the equivalent 32-bit 2's complement number.
This is only an issue if the immediate value in an instruction has bit 15 set, the sign bit if it were a 2's complement number.
So addi r1,r2,4 is the same as addui r1,r2,4 but addi r1,r2,65535 is NOT the same as addui r1,r2,65535.
In an instruction 65535 is what you get if all 16 bits are 1s.
Treated as an unsigned value this is 65535 but treated as a signed value it is -1.
So addi r1,r2,65535 is the same as writing addi r1,r2,-1
When it comes to choosing which one to use, if you want a negative value between -1 and -32768, use the signed instructions, addi, subi, etc. If you want a positive number between 0 and 32767 it does not matter. If you want a positive number between 32768 and 65535 you must use the unsigned instructions, addui, subi, etc.
Posted by Ern at 3:07 PM