将十进制转换为基数为4的汇编(MIPS) [英] Convert decimal to base 4 assembly (MIPS)
问题描述
如何将十进制数组中的整数转换为以4为底的整数(有符号和无符号)?
How to convert integers in an array that are in decimal to base 4 (signed and unsigned)?
推荐答案
您可以使用以下算法:将数字除以所需的底数,直到商为零,然后使用余数作为相反的最终结果,例如:
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
其余为新底数的数字(按相反顺序):"113".
The remainders are the digits in the new base (in reverse order) : "113".
您的代码将需要两个代码块:
Your code will require two blocks :
- 使用一个块进行除法直到商为零,在此块中,将余数存储在堆栈中(推入).每个商是下一个部门的股利.
- 另一个块,用于弹出余数并将其存储在字符串中.其余的将以相反的顺序提取.
:对于负数,必须首先检测符号,如果符号为负,则必须获取数字的绝对值,例如:
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).
这篇关于将十进制转换为基数为4的汇编(MIPS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!