我有一个32位指令,我希望分成四个字节。 假设说明如下:
yyyyyzzzzzzxxxxxx?????????该指令是一个由四个无符号整数组成的单词。 y表示操作码,并且 是为未使用的空间。 我正在研制一台大型机器。
我想要发生的是将值从z + w移到a 。
我从来没有在C工作过,但我曾试图这样做。
下面是我如何阅读该单词,以便我可以打印出每个字节:
unsigned int a, b, c, o; w = instruction << 24; z = instruction << 16; x = instruction << 8; y = instruction;这里我打印无符号值,只是为了检查结果是什么。
printf("%u\n", w); printf("%u\n", z); printf("%u\n", x); printf("%u\n", y); printf("\n"); regs[x] = instruction + instruction << 8;如果我在这之后打印出regs[x]的值,那么我可以看到我现在有一个值,但这是做这件事的正确方法吗? 当我这样做时,我是否设置了register = z + w ?
编辑Mabye我应该得到这样的位?
y = (inst >> 24) & 077; x = (inst >> 16) & 0xffff; z = (inst >> 8) & 0xffff; w = (inst) & 0xffff;然后这样做:
regs[y] = z + w;i have a 32 bit instruction that i wish to split into four bytes. Let say the instruction looks like this:
yyyyyzzzzzzxxxxxx?????????The instruction is a word that consists of four unsigned ints. y represents the operation code, and ??? are for the unused space. I am working on a big-endian machine.
What I would like to happen is to move the values from z + w to a.
I have never worked in C before but I have tried to do it like this.
Here is how I read the word, just so I ca print out each byte:
unsigned int a, b, c, o; w = instruction << 24; z = instruction << 16; x = instruction << 8; y = instruction;Here I print unsigned values, just to check what the result are.
printf("%u\n", w); printf("%u\n", z); printf("%u\n", x); printf("%u\n", y); printf("\n"); regs[x] = instruction + instruction << 8;if I print out the values of regs[x] after this, then I can see that I has a value now, but is this the correct way of doing it? When I do like this, do I set the register = z + w?
EDIT Mabye i should get the bits like this?
y = (inst >> 24) & 077; x = (inst >> 16) & 0xffff; z = (inst >> 8) & 0xffff; w = (inst) & 0xffff;and then do like this:
regs[y] = z + w;最满意答案
如果您只想使用位的位置和计数,则可以通过设置下一位和减少(1 << 10)-1来构建一个9位的位掩码。 所以你的价值观是
#define MASK(n) ((1<<(n+1))-1) unsigned int w = instruction & MASK(9); unsigned int x = (instruction >> 9) & MASK(6); unsigned int z = (instruction >> 15) & MASK(6); unsigned int y = (instruction >> 21) & MASK(5);所有值都下移。 所以如果你想结合z和w你将不得不
unsigned int zw = z<<9 | w;因为w包含9位,或者
unsigned int wz = w<<6 | z;因为z包含6位。
If you like to use only bit positions and counts you can build a bit mask of i.e. 9 bits with setting the next bit and decrement (1<<10)-1. So your values are
#define MASK(n) ((1<<(n+1))-1) unsigned int w = instruction & MASK(9); unsigned int x = (instruction >> 9) & MASK(6); unsigned int z = (instruction >> 15) & MASK(6); unsigned int y = (instruction >> 21) & MASK(5);all values are down shifted. So if you like to combine z and w you will have to
unsigned int zw = z<<9 | w;because w contains 9 bits, or
unsigned int wz = w<<6 | z;because z contains 6 bits.
更多推荐
发布评论