ihtkwot
ihtkwot

Reputation: 1252

Integer Problem in MIPS assembly

Using MIPS assembly if I prompt a user to input an integer how can I then take that integer and break it up into it's requisite parts?

Example:

                 # User inputs a number
li  $v0, 5      # read value of n
    syscall

I then store the value in $v0 in a temporary register, say $t0, and need to break it up into each part that makes it up. So, 308 has to be broken up into: 3, 0, and 8. I need to do this so that I can then square each of these parts and add them together.

The input value from the user has to be input as an integer.

thanks, nmr

Upvotes: 0

Views: 2157

Answers (2)

Tom
Tom

Reputation: 45114

This is @Don's answer, with a twist

$t0 contains the user input. (asume unsigned)

li   $t1,10
DIVU $t0,$t1 //divide by 10

mfhi $t2 //t2 contains the division result
mflo $t3 //t3 containts the division remainder

use beq, bgt to do the comparisons.

Some help

http://www.mrc.uidaho.edu/mrc/people/jff/digital/MIPSir.html

Upvotes: 1

Don
Don

Reputation: 4673

Divide by 10, use the remainder to get the 8, if quotient is non-zero, divide by 10 again and use then remainder to to the zero, if quotient is non-zero repeat.

Upvotes: 2

Related Questions