Bit arithmetic - concatenate two results in one bit
-
29-05-2021 - |
Domanda
I'm doing a homework in MARS simulator (Assembly) and I'm stuck on one part.
We have to load into a register a 32 bits word.
The bits from 0 to 7 represent the blue color, the bits 8 to 15 the green color and bits 16 to 23 the red color. The remaining bits are set to zero.
For example, Yellow color is 0x00ffff00.
[31] [24]|[23] [16]|[15] [8] | [7] [0]
+----+- -+----+----+- ... -+----+----+- ... -+----+----+- ... -+----+
| 0 | ... | 0 | RED COLOR | GREEN COLOR | BLUE COLOR |
+----+- -+----+----+- ... -+----+----+- ... -+----+----+- ... -+----+
i.e. BGR0 color format in memory order, or 0RGB in native-endian machine words.
The homework is divided into three tasks and I'm on the last one. We have a 64 by 64 pixel display (each pixel is 4*4 so total width and height are 256). Red color is always set to 0, green is 4 * the number of the line and blue is 4 * the number of the column. Imagine we are at the 2 line and 3 row, green value would be 2*4 and blue value 3*4. So, in this example, in hex, 12 would be 0xc and 8 0x08 and the number that should be load into the register would be 0x00000c08.
So my first question is how can I use bit algebra to concatenate the result of the 2 multiplications?
And my second question is this: Imagine we are on the last pixel row and column: 64 * 4 and 64 * 4. The result is 256 and we can't use only 8 bits to represent that number so probably I should not use pixels 1 to 64 but 0 to 63. Right?
Soluzione
So you have 0x08 and 0x0C and you want 0x0C08 ?
Look up bit shifting
In C like languages it would something like
(0x0C << 8) | 0x08
(0x0C << 8) gives 0x0C00