将32位指令拆分为字节并使用c将结果移动到另一个地址(split 32 bit instruction into bytes and move result to another address us

编程入门 行业动态 更新时间:2024-10-27 15:29:37
将32位指令拆分为字节并使用c将结果移动到另一个地址(split 32 bit instruction into bytes and move result to another address using c)

我有一个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.

更多推荐

本文发布于:2023-07-28 22:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1309777.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字节   指令   地址   split   bit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!