-1

I'm working on interpreting some IA-32 assembly code. This is what it reads:

    .data
    .align 4
A:  .long 10, 40, 20, 50, 30
    .text
main:
    movl $0x75, %eax
    sarl $3, %eax

I know that it's an arithmatic right shift, but I couldn't find any examples. Could someone help break this down for me? I'm not sure what needs to be done to a number for a "right shift". Thanks.

donut juice
  • 257
  • 6
  • 19
  • 1
    It's on [wikipedia](https://en.wikipedia.org/wiki/Arithmetic_shift) – harold Dec 01 '15 at 19:10
  • 1
    see http://stackoverflow.com/tags/x86/info for a link to the insn ref manuals. You'll find precise definitions of exactly what every instruction does. Also, everyone please vote for ia-32 as a synonym for the x86 tag at: http://stackoverflow.com/tags/x86/synonyms. – Peter Cordes Dec 02 '15 at 02:44

1 Answers1

1

A right shift by N bits is the same as an integer division by 2^N. So you're dividing eax by 2^3, i.e. 8.

In this case a logical right shift would've yielded the same result, since the most significant bit of eax is zero (the 32-bit binary represenation of 0x75 is 00000000000000000000000001110101).

Michael
  • 57,169
  • 9
  • 80
  • 125
  • awesome answer, thanks! I saw the edit too. So I would simply shift it right by 3? This would yield `00000000000000000000000000001110` is this correct? – donut juice Dec 01 '15 at 19:14
  • Right, you'd get 1110 (0xE / 14). – Michael Dec 01 '15 at 19:21
  • 2
    @Michael - More technically, arithmetic right shift is division by 2^N with rounding toward negative infinity, not zero. – owacoder Dec 01 '15 at 19:52
  • arithmetic shift is a right-shift that shifts in copies of the sign-bit, instead of zeros. compilers use it to emulate signed division by a power of 2, but it takes multiple instructions to round the same way. It's arguably better to arithmetic-shift than to divide, to chop the number line into even-size pieces. Supercat has commented about this before, but I can't find the comment right now. – Peter Cordes Dec 02 '15 at 02:47