Jonathan DInh
Jonathan DInh

Reputation: 31

Inline x86 asm for dividing by 2 in C

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

Upvotes: 0

Views: 189

Answers (1)

David Wohlferd
David Wohlferd

Reputation: 7483

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

Upvotes: 3

Related Questions