Pseudo code for instruction description
Posted
by Claus
on Stack Overflow
See other posts from Stack Overflow
or by Claus
Published on 2010-05-24T14:55:33Z
Indexed on
2010/05/24
15:01 UTC
Read the original article
Hit count: 360
Hi,
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 && (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
© Stack Overflow or respective owner