In: Electrical Engineering
I have a question about machine languages. Why do values that are being compare have to be either signed or unsigned? Why cant one value be signed while the other is unsigned in machine language? If you don't understand what I mean by machine language, I am referring to programming language where instructions such as load and store is used. Thank you.
Q - "I have a question about machine languages. Why do values that are being compare have to be either signed or unsigned? Why cant one value be signed while the other is unsigned in machine language?"
A - It is because of the fact that, unlike C and other languages, in machine languages, there is no concept of data type (signed datatype or unsigned datatype). So, the machine language uses same instructions for both signed and unsigned cases. Therefore, if you want to get correct results, either compare signed values or compare unsigned values; otherwise, you will get wrong results in many cases.
Example ==>
Suppose you have a 4-bit processor ==>
And there are two registers A and B; and ==>
A = 1011 in binary
B = 1101 in binary
If A is signed, then A = -5 in decimal
If A is unsigned, then A = 11 in decimal
If B is signed, then B = -3 in decimal
If B is unsigned, then B = 13 in decimal
Suppose, you want to perform addition operation on registers A and B and you gave the following command ==>
ADD A,B
The result of this command will be 1000 in binary and carry (C) bit is set, i.e., carry(C) =1.
Now, let's cross-check.
(i) Signed A + Signed B = -5 + (-3) = -8 decimal = 1000 binary and C=1
(ii) Signed A + Unsigned B = -5+13 = 8 decimal = 1000 binary and C=0
(iii) Unsigned A + Signed B = 11-3 = 8 decimal = 1000 binary and C=0
(iv) Unsigned A + Unsigned B = 11+13 = 24 decimal = 1000 binary and C=1
You can see here that only (i) and (iv) matches with the desired result.