I'm trying to translate the following program to x86 assembly ( AT&T ).
#include <stdio.h>
int main()
{
   int n = 123;
   int reverse = 0;
   while (n != 0)
   {
      reverse = reverse * 10;
      reverse = reverse + n%10;
      n       = n/10;
   }
   printf("%d\n", reverse);
   return 0;
}
It's supposed to print 321.
However, with the code below, I'm getting a 0 instead. Can anybody give me a clue about what I'm doing wrong here? ( I pasted just the relevant section below. I'm sure that initialization and printing are working fine. You can see the whole thing here)
  movl  $123, %esi    # int n
  movl  $0, %edi    # int reverse
  movl $10, %ebx    # divisor
L1:     # while n != 0
cmpl $0, %esi
je L2
# reverse = reverse * 10
imul $10, %edi
# reverse = reverse + n % 10
movl $0, %edx
movl %edi, %eax
idivl %ebx
addl %edx, %edi
# n = n / 10
movl %esi, %eax
movl $0, %edx
idivl %ebx
movl %eax, %esi
jmp L1
L2:  # end while
movl %edi, %eax
Maybe I'm not yet perfectly understanding what the idivl command is supposed to do. I understand that it divides %edx:%eax by %ebx and stores the quotient in %eax and the remainder in %edx.
 
     
    