3

Part of an assignment for my systems programming class asks us to write an assembly module that returns the number of digits in a 32-bit integer given a base in the range of 2 to 36 inclusive. Everything works fine except for one case. When I use the maximum signed positive 32-bit value 0x7FFFFFFF and a base of 2, the code returns 32 instead of 31 as the number of digits.

As a solution, I thought about handling this case by including instructions to check if the num argument is equal to 0x7FFFFFFF and returning 31 if so. However, I'm not sure if that approach would be correct.

I'm using a "do-while loop" type algorithm to solve the problem.

Function prototype: int numOfDigits( long num, int base );

.global numOfdigits

.section ".text"

numOfDigits:

    save   %sp, -96, %sp

    clr    %l0         ! digits = 0

    mov    %i0, %o0    ! num is first argument to .div
    mov    %i1, %o1    ! base is second argument to .div

loop:

    call   .div        ! %o0 = %o0 / %o1
    nop

    inc    %l0         ! ++digits

    cmp    %o0, 0      ! while ( num != 0 )
    bne    loop
    nop

    mov    %l0, %i0    ! copy digits value into return register

    ret
    restore
ffhaddad
  • 1,653
  • 13
  • 16
  • 1
    You seem to increment "digits" unconditionally and before escaping the loop. I'd say, your return value has a constant bias of 1. – Aki Suihkonen Apr 16 '13 at 05:43
  • Just a thought -- base 2 is binary right? You have 32 bit integer, right? In binary, or base 2, you should have 32 digits right? Just askin'? ah -- its signed. – David Tansey Apr 16 '13 at 05:43
  • @AkiSuihkonen That is true because ALL values for num will have at least one digit--even zero. – ffhaddad Apr 16 '13 at 05:47
  • @DavidTansey Correct, but if you have the value 0x7FFFFFFF and you keep dividing by 2 until the quotient is 0, the return value should be 31. Try with a smaller case. – ffhaddad Apr 16 '13 at 05:49
  • If it's signed, shouldn't the max be 31 for all cases (as the 32nd bit is the sign). So if 0xFFFFFFFF returns 32, it's also wrong? – TheMerovingian Apr 16 '13 at 06:52
  • Also, is 0 considered to be 0 digits or 1 digit? – TheMerovingian Apr 16 '13 at 06:57
  • Well, for 0xFFFFFFFF (-2^31), the number of digits in base 2 should be 32. However, for 0x7FFFFFFF (2^31 - 1), the number of digits should be 31. For the second question, yes 0 is considered 1 digit. That's the reason for the for always performing at least one iteration. – ffhaddad Apr 16 '13 at 15:05
  • BTW 0xFFFFFFFF returns 32 as expected and all other input values return the appropriate result. The ONLY case that doesn't work is 0x7FFFFFFF which is returning 32 instead of 31. – ffhaddad Apr 16 '13 at 15:17
  • I believe `.div` is allowed to change `%o1` so you'd better reload that in the loop. Other than that, this code returns `31` for me, using `qemu-sparc`. – Jester Apr 16 '13 at 16:15
  • UPDATE: On a different machine, my code runs as expected. I don't know if this is the machine itself or the OS. Using GDB the instructions are executing the same on both machines. – ffhaddad Apr 20 '13 at 15:27

0 Answers0