使用按位运算符将无符号整数(2字节长)的值存储到unsigned char变量?
-
06-07-2019 - |
题
如果指令是1rxy? 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的格式,这意味着将值设置为“at”。寄存器中的存储位置xy。
所以这里寄存器4或 unsigned char r4
必须保存在内存位置0x04的值,这将保留一些其他数字。
我的问题是如何通过确定“r”来测试寄存器变量。或者在1“4”04中使用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;
}
解决方案
如果我理解正确,你想把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++;
}
不隶属于 StackOverflow