Convert decimal to base 4 assembly (MIPS)

2019-07-28 16:17发布

问题:

How to convert integers in an array that are in decimal to base 4 (signed and unsigned)?

回答1:

You can use the algorithm of dividing the number by the desired base repeatedly until the quotient is zero, using the remainders as the final result in reverse order, example :

       QUOTIENTS OF EACH DIVISION
            ▼     ▼     ▼
     23÷4 = 5÷4 = 1÷4 = 0
      3     1     1
      ▲     ▲     ▲
 REMAINDERS OF EACH DIVISION

The remainders are the digits in the new base (in reverse order) : "113".

Your code will require two blocks :

  • One block to make the divisions until the quotient is zero, in this block you store the remainders in stack (push). Each quotient is the dividend of the next division.
  • Another block to pop out the remainders and store them in a string. Remainders will be extracted in reverse order.

Edit : in case of negative numbers, the sign must be detected first, if the sign is negative it is necessary to get the absolute value of the number, example :

abs $t1, $t1

The sign must be re-applied to the result at the end (if necessary).



标签: assembly mips