Convert decimal to base 4 assembly (MIPS)

这一生的挚爱 提交于 2019-12-24 10:15:04

问题


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).



来源:https://stackoverflow.com/questions/41065452/convert-decimal-to-base-4-assembly-mips

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!