Inline x86 asm for dividing by 2 in C

Jonathan DInh

Im trying to convert something in C

int div
div = div/2;

into inline ATT x86 assembly using right shift (SAR)

asm("sar %0" : "=r"(div));

but I couldn't get it to work. Any insights would be greatly appreciated

David Wohlferd

By using =r, you are telling the compiler that the existing value in div is overwritten by the asm. To tell it you are both reading and writing the value of div in the asm, use +r (see https://gcc.gnu.org/onlinedocs/gcc/Modifiers.html):

asm("sar %0" : "+r"(div));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related