byte (1 byte integer (whole number) representation for values between -127..128) short (2 byte integer (whole number) representation for values between -32767..32768) int (4 byte integer (whole number) representation) |
|
The value 3 (three dots) is represented as follows: Using 1 byte (8 bits): 00000011 Using 2 bytes (16 bits): 0000000000000011 Using 4 bytes (32 bits): 00000000000000000000000000000011 |
The value -3 is represented as follows: Using 1 byte (8 bits): 11111101 Using 2 bytes (16 bits): 1111111111111101 Using 4 bytes (32 bits): 11111111111111111111111111111101 |
|
Example:
00000011 is the representation of the value 3 in 8 bits If we convert the representaion into a 16 bit representation, the result is: 0000000000000011 (Because both representations represent the value 3 !!!) |
|
|
|
|
is called:
|
|
EXT.W Dn Sign extend a BYTE (6 bits) 2's complement representation to a WORD (16 bits) 2's complement representation in Data Register Dn EXT.L Dn Sign extend a WORD (16 bits) 2's complement representation to a LONG WORD (32 bits) 2's complement representation in Data Register Dn |
Note:
|
Examples:
(1) +----------+----------+----------+----------+ D0 = | 10101010 | 01010101 | 10101010 | 11111110 | +----------+----------+----------+----------+ ^^^^^^^^ This byte represents the value "-2" After "EXT.W D0", we will have: +----------+----------+----------+----------+ D0 = | 10101010 | 01010101 | 11111111 | 11111110 | +----------+----------+----------+----------+ ^^^^^^^^^^^^^^^^^^^ This WORD represents the (same) value "-2" ! (2) Continuing with the previous example: +----------+----------+----------+----------+ D0 = | 10101010 | 01010101 | 11111111 | 11111110 | +----------+----------+----------+----------+ ^^^^^^^^^^^^^^^^^^^ This WORD represents the value "-2" After "EXT.L D0", we will have: +----------+----------+----------+----------+ D0 = | 10101010 | 01010101 | 11111111 | 11111110 | +----------+----------+----------+----------+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This LONG WORD represents the (same) value "-2" ! |
|
|
byte ---> short ---> int ---> long |
int i; short s; byte b; The Java COMPILER translating the following assignment statements in a high level language will **automatically** use "EXT" assembler instructions: |
|
|
short s; byte b; |
int i; short s; byte b; The Java COMPILER truncate the representaion **ONLY** if you **beg** for it: |
Example:
s = 129; (Stored as: 0000000100000001 --- represents value 129) b = (byte) s; Jave will assign the lower 8 bits of s to b: b will contain the binary representation 00000001 which represent the value 1 and not the orignal value 129 |
|
int i, j; i = i + j; |
In assembler: (no conversion is needed !)
move.l i, d0 move.l j, d1 add.l d0,d1 move.l d1, i |
int i; short s; i = i + s; |
In assembler:
move.l i, d0 32 bits of d0 used to represent int move.w s, d1 16 bits of d1 used to represent short ext.l d1 same value is now represented by 32 bits in d1 add.l d0,d1 move.l d1, i |
int i; short s; s = (short) (s + i); // Casting means danger ! |
In assembler:
move.l i, d0 32 bits of d0 used to represent int move.w s, d1 16 bits of d1 used to represent short ext.l d1 same value is now represented by 32 bits in d1 add.l d0,d1 sum is represented by 32 bits in d1 move.w d1, s Used only 16 bits in d1 |
|
The (short) casting operation is to let you tell Java that you know it's dangerous and "please let me do it anyway".
|
(If you dare to program in assembler, you should be man enough to take on the responsibility to know what to do... You are not in Kansas anymore (to quote Dorothy))
int i; short s; byte b;
How to perform all possible conversion between int, short and byte:
s = b; ---> MOVE.B b, D0 EXT.W D0 MOVE.W D0, s b = s ---> MOVE.W s, D0 MOVE.B D0,b * Can result in overflow....
i = s; ---> MOVE.W s, D0 EXT.L D0 MOVE.L D0, i s = i; ---> MOVE.L i, D0 MOVE.W D0,s * Can result in overflow....
i = b; ---> MOVE.B b, D0 EXT.W D0 * First convert to WORD EXT.L D0 * THEN convert to LONG WORD ! MOVE.L D0, i b = i; ---> MOVE.L i, D0 MOVE.B D0,b * Can result in overflow....