Thursday, April 21, 2011

MIPS: How do parse out digits of a number? Basic question but yet hard.

I am currently learing MIPS for a class and wrote the below sample code.

# UNTITLED PROGRAM

.data   # Data declaration section

.text

       main:      # Start of code section

               li $t1, 72
           move $a0, $t1
           li $v0,1

       exit:
              li $v0, 10
              syscall

# END OF PROGRAM

As shown the value of '72' is stored in register $t1. Now, how do I parse out the '7' and '2' out?

From stackoverflow
  • Divide by 10, the modulo is your next digit, repeat until the result of division is 0. This assuming you're not working with floating point numbers.

  • Not a mips expert... but:

    72 / F = 7 "7"

    7 * F = 70

    72 - 70 = 2 "2"

    You can apply this logic using F * (number of total digits - 1) to get each individual digit.

    -r

0 comments:

Post a Comment