Assembly 如何在MIPS中将十进制的基整数转换为4进制的整数?
嘿,朋友们,我是mips新手,我正在尝试将链接列表中的数字转换为基数4并打印出来,但我真的不知道怎么做 我听说过一种方法,将数字除以4,然后将每个数字的剩余部分推到某个堆栈中,但我不确定该怎么做,或者应该使用哪个堆栈 我想把列表中的每个数字都打印出来作为基数4 我很想得到一些帮助或指导如何做 这是我到目前为止的代码(我也在打印数字的和,以及正数字的和除以4)Assembly 如何在MIPS中将十进制的基整数转换为4进制的整数?,assembly,mips,base,mars-simulator,Assembly,Mips,Base,Mars Simulator,嘿,朋友们,我是mips新手,我正在尝试将链接列表中的数字转换为基数4并打印出来,但我真的不知道怎么做 我听说过一种方法,将数字除以4,然后将每个数字的剩余部分推到某个堆栈中,但我不确定该怎么做,或者应该使用哪个堆栈 我想把列表中的每个数字都打印出来作为基数4 我很想得到一些帮助或指导如何做 这是我到目前为止的代码(我也在打印数字的和,以及正数字的和除以4) 这些数字已经用基数2编码了,所以你不需要真正地对它进行除法,每两位就形成一个基数4的数字。您可以使用shift+mask(通过和)每次提取
这些数字已经用基数2编码了,所以你不需要真正地对它进行除法,每两位就形成一个基数4的数字。您可以使用shift+mask(通过
和)每次提取两位,并将其转换为数字。最后一个数字也是“除以4”的余数,所以您可以测试最后一个数字是否为0,然后添加到s1
。负值使这些假设更加复杂(例如-4是11…1100)。由于这些值基本上已经可以在不进行计算的情况下以基数4打印出来,您可以简单地从左到右打印表单,而无需堆栈,只需跳过前导零(如果整个值为零,请确保最后输出零)。或者,如果可以使用前导零打印固定长度,则无需跳过它们。您应该先尝试编写这个代码,因为它更简单,然后添加前导零。
.data
num1: .word -1 , num3
num2: .word 17 , 0
num3: .word 32 , num5
num4: .word -6 , num2
num5: .word 1972 , num4
.globl main
.text
main:
la $t1,num1
li $s0,0 # sum of the linked list
li $s1,0 # sum of divided by 4 and postivie in the list
li $t3,0 # temp remainder for the div by 4
li $t4,4
li $t5,0 # counter how many words we enterted the stack
sumloop:
beqz $t1,exit
lw $t0,0($t1)
add $s0,$s0,$t0 # adding to the sum the value in the current node
lw $t1,4($t1)
### need your help here :)
blez $t0,sumloop # if negative or zero dont add to s1
div $t0,$t4
mfhi $t3
bnez $t3,sumloop # if the value isnt divided by 4 without remainder jump to sumloop
add $s1,$s1,$t0
j sumloop
exit:
move $a0,$s0
li $v0,1
syscall
li $a0,'\n'
li $v0,11
syscall
move $a0,$s1
li $v0,1
syscall