我想编写一个将十进制转换为二进制的程序。为此我会使用一个数组。请查看下面评论的行并告诉我为什么它可能不起作用,因为spim模拟器说试图执行非指令
.text
.globl __start
__start:
la $a0,prompt
li $v0,4
syscall
li $v0,5
syscall
move $t1,$v0
li $t0,0
la $t2,bin
#add $t2,$t2,124
lop:
div $t1,$t1,2
mflo $v0
sw $v0,($t2) #right here
add $t2,$t2,4
add $t0,$t0,1
ble $t0,32,lop
j dnn
ads:
la $a0,spc
li $v0,4
syscall
j back
dnn:
la $t0,bin
li $t3,1
la $a0,pr2
li $v0,4
syscall
add $t0,$t0,124
lp2:
lw $t1,($t0)
sub $t0,$t0,4
add $t3,$t3,1
move $a0,$t1
li $v0,5
syscall
div $t4,$t3,4
mflo $t4
ble $t4,0,ads
back:
ble $t3,33,lp2
li $v0,10
syscall
.data
prompt: .asciiz "Enter decimal: "
spc: .asciiz " "
pr2: .asciiz "\nbinary value:\n"
bin: .space 128
编辑:我做了一些更改,比如现在所有的'添加'都是'addi'和'.align 2'来自.data
在垃圾传播模拟器(PCSpim)中我发现了一些有趣的东西:
[0x00400020] 0x3c011001 lui $1, 4097 [bin] ; 15: la $t2,bin
[0x00400024] 0x342a0022 ori $10, $1, 34 [bin]
[0x00400028] 0x34010002 ori $1, $0, 2 ; 17: div $t1,$t1,2
[0x0040002c] 0x0121001a div $9, $1
[0x00400030] 0x00004812 mflo $9
[0x00400034] 0x00001012 mflo $2 ; 18: mflo $v0
[0x00400038] 0xad420000 sw $2, 0($10) ; 19: sw $v0,($t2)
为什么mflo会做两次?