我试图生成一个整数序列:1,2,3,6,9,18,27,54,81,依此类推。如果它是奇数,则通过将值添加到自身来计算该序列,如果它是偶数则通过将值添加到它来计算。 我已经多次浏览过我的代码了。我甚至试图在那里放置数字来手工测试。但是,我无法弄清楚我的程序有什么问题。机器仍然无法正确读取。请赐教我这个。我非常感谢你的帮助。非常感谢你。
这是我的代码:
.text
.globl main
main:
add $t0,$zero,$zero # initial index
add $t1,$zero,1000 # terminate loop condition
addi $at, $at, 1 #1
add $v0,$at, $at #2
add $v1,$v0,$at #3
L: andi $a0,$v1,0x01 # check if 1 -> odd, 0 --> even
beq $a0,$zero,isEven # check to see if it is even
add $a1, $v1,$v1 # if it is odd, add to itself
isEven: add $a1, $v1,$v0 # add value of previous one
add $v0,$zero,$v1 #v0 =3
add $v1,$zero,$a1 #6
addi $t0, $t0,1 # increase index
bne $t0,$t1,L
答案 0 :(得分:0)
以下是MIPS中的工作解决方案:
.data
space: .asciiz ", "
.text
li $t1, 1 #Start at 1
li $t2, 1 #Store last value
li $t4, 1000 #terminate at 1000
li $t7, 2 #For division (divide by 2)
loop:
li $v0, 1
add $a0, $t1, $zero #print $t1
syscall
div $t1, $t7 #divide $t1 by 2
mfhi $t5 #get the remainder
move $t6, $t1 #hang on to what $t1 was originally
beqz $t5, even #if the remainder is 0, it's even
add $t1,$t1,$t1 #it's odd, so add to itself
move $t2, $t6 #store what $t1 was originally to last value
bgt $t1, $t4, done #if the number is over 1000, we're done
li $v0, 4 #Print a comma and space
la $a0, space
syscall
j loop
even:
add $t1,$t1,$t2 #set $t1 = $t1 + Last value ($t2)
move $t2, $t6 #store what $t1 was originally to last value
bgt $t1, $t4, done #if the number is over 1000, we're done
li $v0, 4 #Print a comma and space
la $a0, space
syscall
j loop
done: