十进制到4转换组件MIPS

时间:2016-12-11 02:42:26

标签: assembly mips

我试图将数组中的整数从十进制转换为基数4,使用除以4的除法。

我写的代码是:

li $t0, 0   #array index - initialize to zero 
li $v0, 4
la $a0, msg4 #msg4 = The numbers in the array as signed numbers in base 4 are:
syscall

base4Signed:
lb $t1, array($t0)   #load array integer, t0 is the index of the array
li $t4, 4            #use for divide
li $t6, 0            #counter of stack
beq $t0, 10, end

division:
div $t1, $t4         #divide the integer by 4
mflo $t3             #save the quotient in t3
mfhi $t5             #save the reminder in t5
addi $sp, $sp, -1    #push stack
sb $t5, 0($sp)       #store reminder
addi $t6, $t6, 1     #counter + 1
move $t1, $t3        #use t3 = quotient for next division
beqz $t3, print      #quotient = 0
j division

print:
li $v0, 1
la $a0, 0($sp)
syscall
addi $sp, $sp, 1
addi $t6, $t6, -1
bnez $t6, print
addi $t0, $t0, 1
beqz $t6, base4Signed

end:

我得到了一些奇怪的结果。

该阵列:100,0,120,6-,2,45,67,89,12-,23

应转换为:1210,0,1320,12-,2,231,1003,1121,30-,113(签名)

和:113,3332,231,1003,1121,30,2130,0,1320,12(未签名)

我做错了什么?

1 个答案:

答案 0 :(得分:0)

你应该改变第二步" print"加载字节而不是加载地址 你应该写: lb $ a0,0($ sp)