在ASM中使用SHRD编程接收信号SIGSEGV,分段故障

时间:2014-05-21 03:00:55

标签: c gcc assembly inline-assembly att

我正在尝试将eax向右移动2,目前它是0x037f并且我想将其转换为0x0003。

我正在使用Cygwin,它编译得很好但是当我用gdb打开它时,一旦我到达第7行,我就会得到标题错误。

我已经尝试将语法更改为(%eax,2,0)(我想将其左移2并填充0)我也尝试了只有2,它的值为8因为我不喜欢不知道它是按字节还是按位移动。

     asm("   push    %%eax                           \n"
         "   push    %%ebx                           \n" 
         "   push    %[nIn]                          \n"
         "   fstcw   %[trueCWIn]                     \n"     //store FPU CW into trueCW
         "   mov     %[trueCWOut], %%eax             \n"     //store old FPU CW into tempVar
         "   mov     %%eax, %[tempVarIn]             \n"     //store old FPU CW into tempVar
         "   shrd    %%eax,8                         \n"     //shift FPU CW right to bytes
         "   add     %[roundModeOut], %%eax          \n"     //adding rounding modifier
                                                            //shift left 2 bytes
                                                            //add in restore mask (0x007F)
         "   frndint                                 \n"     //do rounding calculation
                                                            //store result into n
         "   fldcw   %[trueCWIn]                     \n"     //restoring the FPU CW to normal
         "   pop     %%ebx                           \n"
         "   pop     %%eax                           \n"

         :   [trueCWOut]     "=m"    (trueCW),
             [tempCWOut]     "=m"    (tempCW),
             [maskOut]       "=m"    (mask),
             [tempVarOut]    "=m"    (tempVar),
             [roundModeOut]  "=m"    (roundMode),
             [nOut]          "=m"    (n)

         :   [trueCWIn]      "m"     (trueCW),
             [tempCWIn]      "m"     (tempCW),
             [maskIn]        "m"     (mask),
             [tempVarIn]     "m"     (tempVar),
             [roundModeIn]   "m"     (roundMode),
             [nIn]           "m"     (n)
         :"eax", "ebx"
    );

1 个答案:

答案 0 :(得分:1)

我找到了答案,这是一个简单的语法问题。

shr $8, %%eax

之前,eax是0x037F。 这将eax移位8位,然后使其成为0x0003。