Assembly Language for Beginners

(nextflipdebug2) #1

3.20. PACKING 12-BIT VALUES INTO ARRAY


array[array_idx+1]=array[array_idx+1]|val_highest_nibble;
array[array_idx+2]=val_lowest_byte;
}
else
{
// this is even element
// put value into leftmost and middle bytes:

// decompose value to be stored:
uint8_t val_highest_byte=val>>4;
uint8_t val_lowest_nibble=val&0xF;

array[array_idx]=val_highest_byte;

// clear high 4 bits in the middle byte:
array[array_idx+1]=array[array_idx+1]&0xF;
array[array_idx+1]=array[array_idx+1]|val_lowest_nibble<<4;
};
};


int main()
{
int i;


// test
for (i=0; i<0x1000; i++)
{
put_to_array(i, i);
};

for (i=0; i<0x1000; i++)
{
assert(get_from_array(i)==i);
};
//put_to_array(0x1000, 1); // will fail due to assert()

// print triples:
for (int i=0;i<0x1000/2;i++)
printf ("0x%02X%02X%02X\n",array[i3],array[i3+1],array[i*3+2]);
};


During test, all 12-bit elements are filled with values in 0..0xFFF range. And here is a dump of all triplets,
each line has 3 bytes:


0x000001
0x002003
0x004005
0x006007
0x008009
0x00A00B
0x00C00D
0x00E00F
0x010011
0x012013
0x014015


...


0xFECFED
0xFEEFEF
0xFF0FF1
0xFF2FF3
0xFF4FF5
0xFF6FF7
0xFF8FF9
0xFFAFFB
0xFFCFFD
0xFFEFFF


Here is also GDB byte-level output of 300 bytes (or 100 triplets) started at 512/2*3, i.e., it’s address where

Free download pdf