1

This code below takes in an two digit number (let's say 12) in ASCII and should return this number as the number 12. (NOT ASCII)

mov     $IBUFF, %eax    # IBUFF stores the ASCII number 12
movl    $NUMBER, %ecx   # NUMBER will i put the number 12 (NO ASCII)

movb    (%eax), %bl     # Moves the first byte (1) to %bl
subb    $48, %bl        # Decrease %bl so it is not longer a ASCII sign.
movb    %bl, (%ecx)     # Moves this byte into the first place in NUMBER

inc %eax                # Increase IBUFF so it points to the next integer 
inc %ecx                # Increase NUMBER so it points to the next empty space

movb    (%eax), %bl     # Same as above
subb    $48, %bl        # Same as above
movb    %bl, (%ecx)     # Same as above


mov NUMBER, %eax        # Move the number 12 from NUMBER to %eax (return register)

When i run this code the return value is 513, not 12. What am i doing wrong?

1 Answer 1

1

You need to accumulate the result (possibly in a register) by multiplying it by 10, and then adding the new digit. Basic base 10 representation. What you're doing ends up storing the resulting digits in base 256.

Sign up to request clarification or add additional context in comments.

1 Comment

@Bewn - No, but multiplying a number by 10 and then adding another number to it shouldn't be that hard...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.