使用按位运算符将无符号整数(2 个字节长)的值存储到无符号 char 变量?

发布于 2024-08-05 02:54:50 字数 1966 浏览 7 评论 0原文

如果 指令是1rxy? 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的格式,这意味着将值“at”存储位置xy放入寄存器r中。

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

我的问题是如何通过确定 1"4"04 中的“r”或 1"r"xy 并将位置 xy 保存的值放入无符号字符变量 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;
}

How do I put the value of 0x04 in register 4 if the instruction was 1rxy?
1RXY-Load register R with the value at memory address XY

#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){

The actual instruction is 0x1404, this split into two bytes, memloc1 and memloc2. According to the format of 1rxy, which means to put value "at" memory location xy in register r.

so here register 4 or unsigned char r4 would have to hold the value at memory location 0x04 which would hold some other number.

My question is how do I test for the register variable by determining the "r" or 1"r"xy in 1"4"04 and placing the value held at the location xy into the unsigned char variable r4

for example if memory location 0x04 held 0xFB.

I hope this makes sense.

[edit]
Example

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

养猫人 2024-08-12 02:54:50

如果我理解正确的话,你想把 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 中。然后为了循环通过指令,你可以使用这个

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++;
}

If i understand correctly, you want to put B4 in memory[0] and 04 in memory[1]. Am i right?

This will do that.

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

I think, next you want to check B and 4 seperately on memory[0] and then proceed to your next step. Right?

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

Is this what you are looking for?

Update: For your reading problem, you should be using this .

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

This reads till EOF and you could use i value after this loop to know how many instructions are read and put it in a variable say n_instr. And then for looping thro' instructions you could use this

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++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文