简体   繁体   中英

Program received signal SIGSEGV, Segmentation fault using SHRD in ASM

I'm trying to shift eax right by 2, currently it is 0x037f and I want to get it to 0x0003.

I'm using Cygwin, it compiles fine but when I open it with gdb, once I get to line 7 I get the title error.

I've tried changing the syntax to (%eax, 2, 0) (I want to shift it left by 2 and fill with 0's) I also tried with just a 2, it has a value of 8 because I don't know if its shifting by bytes or bits.

     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"
    );

I found the answer, it was a simple syntax problem.

shr $8, %%eax

Before, eax was 0x037F. This shifted eax 8 bits, which then made it 0x0003.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM