在此代码中,不会打印出一对十六进制数字(应为lw和sw)。
#include <iostream>
#include <stdio.h>
int main()
{
//char ch[4096];
unsigned int asdf[]={0x022DA822, 0x12A70003, 0x8D930018, 0x02689820, 0xAD930018, 0x02697824, 0xAD8FFFF4,0x018C6020, 0x02A4A825, 0x158FFFF6, 0x8E59FFF0};
char* ch=(char*)asdf;
int br=sizeof(asdf);
//printf("%i\n",int(1) >> 5);
//while((br=read(0,ch,sizeof(ch)))>0) {
int l=br/4;
int* ins_array=(int*)ch;
for(short i=0x0; i<l;i++)
{
int ins=ins_array[i];
int opcode=ins & 0xFC000000;
opcode = opcode >> 26;
//printf("opcode: %i\n",opcode);
int addr=0x7A060+i*4;
switch(opcode)
{
case 0x00:
//add,sub
{
int s=(ins & 0x3E00000)>>21;
int t=(ins & 0x1F0000)>>16;
int d=(ins & 0xF800)>>11;
int tail=ins & 0x7FF;
switch(tail)
{
case 0x20:
//add
printf("%x: add $%i, $%i, $%i\n",addr,d,s,t);
break;
case 0x22:
//sub
printf("%x: sub $%i, $%i, $%i\n",addr,d,s,t);
break;
case 0x24:
//and
printf("%x: and $%i, $%i, $%i\n",addr,d,s,t);
break;
case 0x25:
//or
printf("%x: or $%i, $%i, $%i\n",addr,d,s,t);
break;
case 0x2A:
//slt
printf("%x: slt $%i, $%i, $%i\n",addr,d,s,t);
break;
}
break;
}
case 0x23: //lw
case 0x2B: //sw
case 0x04: //beq
case 0x05: //bne
{
int s=(ins & 0x3E00000)>>21;
int t=(ins & 0x1F0000)>>16;
short I=(ins & 0xFFFF);
short m=65536-I;
short n=(ins & 0xFFFF)<<2;
switch(opcode)
{
case 0x23:
if (I<63)
{
printf("%x: lw $%i, %i($%i)\n",addr,t,I,s);
}
else
{
printf("%x: lw $%i, -%i($%i)\n",addr,t,m,s);
}
break;
case 0x2B:
if (I<63)
{
printf("%x: sw $%i, %i($%i)\n",addr,t,I,s);
}
else
{
printf("%x: lw $%i, -%i($%i)\n",addr,t,m,s);
}
break;
case 0x04:
printf("%x: beq $%i, $%i, address %x\n",addr,s,t,n+addr);
break;
case 0x05:
printf("%x: bne $%i, $%i, address %x\n",addr,s,t,n+addr);
break;
}
break;
}
}
}
}
我的打印出来是这样的:
7a060: sub $21, $17, $13
7a064: beq $21, $7, address 7a070
7a06c: add $19, $19, $8
7a074: and $15, $19, $9
7a07c: add $12, $12, $12
7a080: or $21, $21, $4
7a084: bne $12, $15, address 7a05c
我想要的是打印出来像这样:
7a060: sub $21, $17, $13
7a064: beq $21, $7, address 7a070
7a068: lw $19, 24($12)
7a06c: add $19, $19, $8
7a070: sw $19, 24($12)
7a074: and $15, $19, $9
7a078: sw $15, -12($12)
7a07c: add $12, $12, $12
7a080: or $21, $21, $4
7a084: bne $12, $15, address 7a05c
7a088: lw $25, -16($18)
我该怎么做才能解决这个问题?
答案 0 :(得分:1)
这是你的问题:
int opcode=ins & 0xFC000000;
opcode = opcode >> 26;
opcode
需要是无符号的,否则你将进行算术移位而不是逻辑移位,如果设置了ins
的第31位,结果将为负。
答案 1 :(得分:1)
对有符号整数进行位移会导致出现问题。
int main()
{
std::cout << boolalpha << hex;
int i = 0x8D930018 & 0xfc000000;
int j = i >> 26;
std::cout << j << " == 0x23: " << (j == 0x23) << std::endl;
unsigned int i2 = 0x8D930018 & 0xfc000000;
unsigned int j2 = i2 >> 26;
std::cout << j2 << " == 0x23: " << (j2 == 0x23) << std::endl;
}
Output:
ffffffe3 == 0x23: false
23 == 0x23: true
您需要使用无符号整数或屏蔽掉您感兴趣的位,例如:与
opcode = (opcode >> 26) & 0b111111;