【问题标题】:storing a value from an unsigned integer (2bytes long) to an unsigned char variable using bitwise operators?使用按位运算符将值从无符号整数(2 字节长)存储到无符号字符变量?
【发布时间】:2010-11-27 17:23:58
【问题描述】:

如果instruction 是1rxy,我如何将0x04 的值放入寄存器4? 1RXY-用内存地址XY处的值加载寄存器R

#include <stdio.h>

unsigned char r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,ra,rb,rc,rd,re,rf;

void reg_check(unsigned char reg);
void rxy1(unsigned char reg, unsigned char val);

int main(){
    unsigned char memloc1=0x14;
    unisgned char memloc2=0x04;

    unsigned char temp,reg,val_add;
    temp=(x && 0xFF00) >> 8;

    if (temp = 0xB){
        reg=(memloc1 &0x0F);
        val_add=memloc2;
        rxy1(reg,val_add);
    }

    return 0;
}
void reg_check(unsigned char reg){

}
void rxy1(unsigned char reg, unsigned char val){

实际的指令是 0x1404,它分为两个字节,memloc1 和 memloc2。根据 1rxy 的格式,这意味着将值“放在”内存位置 xy 到寄存器 r 中。

所以这里的寄存器 4 或 unsigned char r4 必须将值保存在内存位置 0x04 中,该位置将保存一些其他数字。

我的问题是如何通过确定 1"4"04 中的 "r" 或 1"r"xy 并将保存在 xy 位置的值放入 unsigned char 变量 r4

例如,如果内存位置0x04 持有0xFB

我希望这是有道理的。

[编辑] 示例

#include <stdio.h>
int main(){
    unsigned char r0,r2,r3,r4;
    unsigned char mem1=0x14;  //at lmemory address 00
    unsigned char mem2=0x04;  //at lmemory address 01



    unsigned char reg_val_store=mem1 & 0x0F;


    if( ((mem1= & 0xF0) >> 4) == 0x1){
        if (reg_val_store == 0x4){
            //then put value store at memory address "04" into register 4.
            //and just say for example "0xFD" was at memory location "04"
            //since register value is 4 from the instruction read in 0x1"4"04

            //i want to put 0xFD in the r4 unsigned char variable, how do i do this?
            r4=0xFD; // this is of course correct but the instruction read in changes and 
                // so does the register variable. how do i modify my code for this change?
        }
    }

    return 0;
}

【问题讨论】:

  • sil3nt,也许你应该问几个单独的问题,而不是一个你不断更新新问题的大帖子?像现在这样很难回答,因为我们必须通读许多已经处理过的问题,然后才能回答当前问题。
  • 对不起,Thomas,希望我相应地纠正了自己

标签: c char int bit-manipulation unsigned


【解决方案1】:

如果我理解正确,您想将 B4 放入内存 [0] 并将 04 放入内存 [1]。我说的对吗?

这样就可以了。

memory[0] = ((x & 0xFF00) >> 8 ); //Will put B4 in memory[0]
memory[1] = (x & 0xFF); //Will put 04 in memory[1]

我想,接下来你要在内存[0] 上分别检查 B 和 4,然后继续下一步。对吧?

(memory[0] & 0xF0) >> 4 // will give you 0xB
(memory[0] & 0x0F) //will give you 0x4

这是你要找的吗?

更新:对于你的阅读问题,你应该使用这个。

while (!feof(f))
{
    fscanf(f,"%X",&inst[i]);
    i++;
}

这会一直读取到 EOF,您可以在此循环之后使用 i 值来了解读取了多少条指令并将其放入变量 n_instr 中。然后对于循环 thro' 指令,你可以使用这个

while(loop<n_instr) //instead of just loop<80
{
        memory[j] = ((inst[loop] & 0xFF00) >> 8 );
        j=j+2;
        memory[k] = (inst[loop] & 0x00FF);
        k=k+2;

        loop++;
}

【讨论】:

  • :) 是的,这是因为我的指令是先读入一个 unsigned int,然后将一个字节提取到一个 unsigned char 中,然后从那里继续。我不太明白如何将最后一个字节分配给 unsigned char 并分别读取值以确定要采取的操作/指令。
  • 啊,读到 EOF 时,为什么 while (i
  • 我希望它不是错误的。你试过了吗? fscanf 返回读取的成功字段数,如果遇到则返回 EOF。
  • 我试过了,没有错误,但是内存中的所有值都改变了..?这就是为什么我之前切换到 while(i
  • 试试这个。只需重新排列 '(' 和 ')' while (i
猜你喜欢
  • 2013-05-23
  • 1970-01-01
  • 1970-01-01
  • 2012-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多