Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么,在我的c++;mips反汇编程序,有什么东西没有打印出来吗?_C++_Mips - Fatal编程技术网

C++ 为什么,在我的c++;mips反汇编程序,有什么东西没有打印出来吗?

C++ 为什么,在我的c++;mips反汇编程序,有什么东西没有打印出来吗?,c++,mips,C++,Mips,在这段代码中,一对十六进制数(应该是lw和sw)没有打印出来 #include <iostream> #include <stdio.h> int main() { //char ch[4096]; unsigned int asdf[]={0x022DA822, 0x12A70003, 0x8D930018, 0x02689820, 0xAD930018, 0x02697824, 0xAD8FFFF4,0x018C6020, 0x02A4A825,

在这段代码中,一对十六进制数(应该是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)
我应该如何解决此问题?

这是您的问题:

int opcode=ins & 0xFC000000;
opcode = opcode >> 26;

opcode
需要无符号,否则将进行算术移位而不是逻辑移位,如果设置了
ins
的第31位,结果将是负数。

有符号整数的位移位有可能导致问题

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

当然,您可以尝试简化此操作!作为第一个观察,我在
asdf
中的任何地方都看不到
0x23
,因此不清楚为什么您希望看到
lw
指令。0x8D930018必须是lw,因为8D在二进制中是1000 1101。LW、SW、BEQ和BNE是I格式,您考虑前6位数字来确定这是什么。看到100011,向右移动两个点,这是0010011,这是0x23。这很复杂,只要看一下就知道了。或者找到一些方法来简化它,或者在调试器中单步执行并查看发生了什么。如果你一步也做不到,把所有的事情都记录下来,然后从中找出答案。
opcode = (opcode >> 26) & 0b111111;