C Programming Registers Structure

0

Trying to create a registers structure on a 64 bit machine and extract it. There are 7 registers in total, 2 registers are 4 bits while 3 registers are 8 bits and last 2 are 16 bits. How would I be able to create a registers structure and extract it. Using these functions.

RegisterFrame create_registers(...?) {???}
??? extract_registers(RegisterFrame, ...) {???}

My first guess was using pack and unpack RegisterFrame pack(uint64_t a1,uint64_t a2, uint64_t b0, uint64_t b1, uint64_t b2, uint64_t c1, uint64_t c2); RegisterFrame unpack(uint64_t a1,uint64_t *a2, uint64_t *b0, uint64_t *b1, uint64_t *b2, uint64_t *c1, uint64_t *c2; But this was wrong cause we were required to use bit operations:

I thought

RegisterFrame create_register(uint64_t a1,uint64_t a2, uint64_t b0, uint64_t b1, uint64_t b2, uint64_t c1, uint64_t c2){
uint64_t retval = 0x0, tempa1, tempa2, tempb0, tempb1, tempb2, tempc1, tempc2
tempa1 = (a1&0xffffffff)
tempa2 = (a2&0xffffffff) << 4
tempb0 = (b0&0xffffffff) << 8
tempb1 = (b1&0xffffffff) << 8
tempb2 = (b2&0xffffffff) << 8
tempc1 = (c1&0xffffffff) << 16
tempc2 = (c2&0xffffffff) << 16
retval = tempa1|tempa2|tempb0|tempb1|tempb2|tempc1|tempc2;

Any help will be appreciated!

c
function
hex
asked on Stack Overflow Feb 27, 2020 by Jack • edited Feb 27, 2020 by Jack

1 Answer

0

Your question is not very clear. The definition of RegisterFrame is not showed neither was the output of the last try you thought.

...but assuming that you are trying to 'pack' values from different sizes into a 'uint64_t' variable, your tentative was few issues:

First: 0xffffffff is a 32bit value, not a 64bit value.

Second: lets say tempa contains 4bits, tempb contains 8bits and tempc contains 16bits, but they are all uint64_t variables.

Third: if you want to pack {tempa0,tempa1,tempb0,tempb1,tempb2,tempc0,temp1} your mistake not masking the actual size you wanted (32bits for all the cases instead 4,8,16bits) and not rotating correctly (if you rotate both values <<4 and then use bit-wise or | you are basically 'ORing' the values between bit[7:4], for instance)

probably (not tested) what you wanted is something like the code bellow:

RegisterFrame create_register(uint64_t a1,uint64_t a2, uint64_t b0, uint64_t b1, uint64_t b2, uint64_t c1, uint64_t c2){ uint64_t retval = 0x0, tempa1, tempa2, tempb0, tempb1, tempb2, tempc1, tempc2 tempa1 = (a1&0xf) << 0 tempa2 = (a2&0xf) << 4 tempb0 = (b0&0xff) << 8 tempb1 = (b1&0xff) << 16 tempb2 = (b2&0xff) << 24 tempc1 = (c1&0xffff) << 32 tempc2 = (c2&0xffff) << 48 retval = tempa1|tempa2|tempb0|tempb1|tempb2|tempc1|tempc2; return retval; }


User contributions licensed under CC BY-SA 3.0