.section .data
inp_buf: .byte 0

.section .text

.globl main
#    /*; System call to read from standard input into buffer
#    ;            eax= code of syscall (read = 3)
#    ; Arguments: ebx= file descriptor (0 is stdin)
#    ;            ecx= pointer to buffer
#    ;            edx= maximum number of bytes to read
#    ; Return value: eax= number of bytes read*/
main:
    # Read into buffer
    mov $3, %eax
    mov $0, %ebx
    mov $inp_buf, %ecx
    mov $1, %edx
    int $0x80

    # Transform from ascii to int
    sub $0x30, (inp_buf)

    mov (inp_buf), %esi

loop:
    # Read new value
    mov $3, %eax
    mov $0, %ebx
    mov $inp_buf, %ecx
    mov $1, %edx
    int $0x80
    # if non number, jump to end
    cmp $0xA, (inp_buf)
    je end
    # Substract 0x30
    sub $0x30, (inp_buf)
    # Multiply esi by 10
    imul $10, %esi
    # Add value to esi
    add (inp_buf), %esi
    jmp loop
end:# Print result

    add $1, %esi
    mov %esi, %eax

    xorl %esi, %esi

loop2:
    movl $0, %edx
    movl $10, %ebx
    divl %ebx
    addl $48, %edx
    push %edx
    incl %esi
    cmpl $0, %eax
    jz   next
    jmp loop2

next:
    cmpl $0, %esi
    jz   exit
    decl %esi
    movl $4, %eax
    movl %esp, %ecx
    movl $1, %ebx
    movl $1, %edx
    int  $0x80
    addl $4, %esp
    jmp  next

exit:
