开发者

Pseudo code for instruction description

I am just trying to fiddle around what is the best and shortest way to describe two simple instructions with C-like pseudo code. The extract instruction is defined as follows:

extract rd, rs, imm

This instruction extracts the appropriate byte from the 32-bit source register rs and right justifies it in the destination register. The byte is specified by imm and thus can take the values 0 (for the least-significant byte) and 3 (for the most-significant byte).

rd = 0x0;  // zero-extend result, ie to make sure bits 31 to 8 are set to zero in the result
rd = (rs &&开发者_JAVA百科; (0xff << imm)) >> imm; // this extracts the approriate byte and stores it in rd

The insert instruction can be regarded as the inverse operation and it takes a right justified byte from the source register rs and deposits it in the appropriate byte of the destination register rd; again, this byte is determined by the value of imm

tmp = 0x0 XOR (rs << imm)) // shift the byte to the appropriate byte determined by imm
rd = (rd && (0x00 << imm)) // set appropriate byte to zero in rd
rd = rd XOR tmp            // XOR the byte into the destination register

This looks all a bit horrible, so I wonder if there is a little bit a more elegant way to describe this bahaviour in C-like style ;)

Many thanks, Claus


Extract:

rd = (rs >> (imm * 8)) & 0xff

Insert:

rd = (rd & ~(0xff << (imm * 8))) | ((rs & 0xff) << (imm * 8))  


First, I think that you mean '&' (bit operator AND) instead of '&&' (logical AND). The XOR bitwise operator in C is '^'

rd = 0x0;
rd = (rs & (0xff << imm)) >> imm;

tmp = 0x0 ^ (rs << imm));
rd = (rd & (0x00 << imm));
rd = rd ^ tmp;

Second, it is simpler to do something like :

rd = (rs >> (imm * 8)) & 0xFF;

and for the second part :

rd = (~(0xFF << (imm * 8)) & rd) | ((rs & 0xFF) << (imm * 8));

The first part create a mask with '1' bits everywhere but in the interesting byte the AND (&) set this byte to '0' and then you add your rs byte. It is not optimised but you see the point.

Edit : I just see Matthew answer. I've misread, imm is in bytes not bits, so as he points out a *8 is needed. I've corrected the formula. He is right too to mask rd with 0xFF to be sure that rs most significant bytes are not taken into account ...

my2c

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜