簡體   English   中英

使用按位運算符將值從無符號整數(長2個字節)存儲到無符號char變量?

[英]storing a value from an unsigned integer (2bytes long) to an unsigned char variable using bitwise operators?

如果指令為1rxy,如何將0x04的值放入寄存器4? 1RXY加載寄存器R,其值位於存儲器地址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){

實際指令為0x1404,該指令分為兩個字節,即memloc1和memloc2。 根據1rxy的格式,這意味着將值“放在”存儲位置xy到寄存器r中。

因此,這里的寄存器4或unsigned char r4必須將值保存在存儲位置0x04上,該位置將保存其他一些數字。

我的問題是如何通過確定1“ 4” 04中的“ r”或1“ r” xy並將放置在位置xy的值放入無符號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;
}

如果我理解正確,則要將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]

我認為,接下來您要分別檢查memory [0]上的B和4,然后繼續執行下一步。 對?

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

這是你想要的?

更新 :對於您的閱讀問題,您應該使用this。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM